feat: add simple Oauth2 logic for github
Some checks failed
Backend ci / build (push) Has been cancelled

This commit is contained in:
d3m0k1d
2026-02-10 00:07:29 +03:00
parent c62dd7f421
commit 53d8760912
8 changed files with 109 additions and 18 deletions

View File

@@ -1,17 +1,30 @@
package handlers
import (
"gitea.d3m0k1d.ru/d3m0k1d/d3m0k1d.ru/backend/internal/repositories"
"os"
"gitea.d3m0k1d.ru/d3m0k1d/d3m0k1d.ru/backend/internal/logger"
"gitea.d3m0k1d.ru/d3m0k1d/d3m0k1d.ru/backend/internal/repositories"
"github.com/gin-gonic/gin"
"golang.org/x/oauth2"
"golang.org/x/oauth2/endpoints"
)
type AuthHandlers struct {
repo repositories.AuthRepository
repo repositories.AuthRepository
logger *logger.Logger
}
var configGithub = &oauth2.Config{
ClientID: os.Getenv("GITHUB_CLIENT_ID"),
ClientSecret: os.Getenv("GITHUB_CLIENT_SECRET"),
RedirectURL: "https://d3m0k1d.ru/",
Scopes: []string{"user"},
Endpoint: endpoints.GitHub,
}
func NewAuthHandlers(repo repositories.AuthRepository) *AuthHandlers {
return &AuthHandlers{repo: repo}
return &AuthHandlers{repo: repo, logger: logger.New(false)}
}
// Callback godoc
@@ -21,7 +34,15 @@ func NewAuthHandlers(repo repositories.AuthRepository) *AuthHandlers {
// @Accept json
// @Produce json
// @Success 200 {object} map[string]string
// @Router /callback [get]
func (h *AuthHandlers) Callback(c *gin.Context) {
// @Router /callback/github [get]
func (h *AuthHandlers) CallbackGithub(c *gin.Context) {
h.logger.Info("CallbackGithub called")
token, err := configGithub.Exchange(c.Request.Context(), c.Query("code"))
if err != nil {
h.logger.Error("error request: " + err.Error())
c.Status(500)
}
h.logger.Info("200 OK GET /callback/github")
c.JSON(200, gin.H{"token": token})
}