feat: full redy blog and admin panel
This commit is contained in:
@@ -3,7 +3,6 @@ package handlers
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
|
||||
"strings"
|
||||
|
||||
"gitea.d3m0k1d.ru/d3m0k1d/d3m0k1d.ru/backend/internal/auth"
|
||||
@@ -16,15 +15,17 @@ import (
|
||||
)
|
||||
|
||||
type AuthHandlers struct {
|
||||
repo repositories.AuthRepository
|
||||
logger *logger.Logger
|
||||
config *oauth2.Config
|
||||
repo repositories.AuthRepository
|
||||
logger *logger.Logger
|
||||
config *oauth2.Config
|
||||
frontendURL string
|
||||
}
|
||||
|
||||
func NewAuthHandlers(repo repositories.AuthRepository) *AuthHandlers {
|
||||
clientID := os.Getenv("GITHUB_CLIENT_ID")
|
||||
clientSecret := os.Getenv("GITHUB_CLIENT_SECRET")
|
||||
redirectURL := os.Getenv("REDIRECT_URL")
|
||||
frontendURL := os.Getenv("FRONTEND_URL")
|
||||
|
||||
if clientID == "" || clientSecret == "" {
|
||||
panic("GITHUB_CLIENT_ID and GITHUB_CLIENT_SECRET must be set")
|
||||
@@ -32,10 +33,14 @@ func NewAuthHandlers(repo repositories.AuthRepository) *AuthHandlers {
|
||||
if redirectURL == "" {
|
||||
redirectURL = "http://localhost:8080/api/v1/callback/github"
|
||||
}
|
||||
if frontendURL == "" {
|
||||
frontendURL = "https://d3m0k1d.ru"
|
||||
}
|
||||
|
||||
return &AuthHandlers{
|
||||
repo: repo,
|
||||
logger: logger.New(false),
|
||||
repo: repo,
|
||||
logger: logger.New(false),
|
||||
frontendURL: frontendURL,
|
||||
config: &oauth2.Config{
|
||||
ClientID: clientID,
|
||||
ClientSecret: clientSecret,
|
||||
@@ -75,7 +80,7 @@ func (h *AuthHandlers) CallbackGithub(c *gin.Context) {
|
||||
code := c.Query("code")
|
||||
if code == "" {
|
||||
h.logger.Error("missing code")
|
||||
c.Redirect(302, "https://d3m0k1d.ru/login?error=missing_code")
|
||||
c.Redirect(302, h.frontendURL+"/login?error=missing_code")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -84,7 +89,7 @@ func (h *AuthHandlers) CallbackGithub(c *gin.Context) {
|
||||
token, err := h.config.Exchange(c.Request.Context(), code)
|
||||
if err != nil {
|
||||
h.logger.Error("Exchange failed: " + err.Error())
|
||||
c.Redirect(302, "https://d3m0k1d.ru/login?error=auth_failed")
|
||||
c.Redirect(302, h.frontendURL+"/login?error=auth_failed")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -92,7 +97,7 @@ func (h *AuthHandlers) CallbackGithub(c *gin.Context) {
|
||||
resp, err := client.Get("https://api.github.com/user")
|
||||
if err != nil {
|
||||
h.logger.Error("Get failed: " + err.Error())
|
||||
c.Redirect(302, "https://d3m0k1d.ru/login?error=github_api_failed")
|
||||
c.Redirect(302, h.frontendURL+"/login?error=github_api_failed")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -100,14 +105,14 @@ func (h *AuthHandlers) CallbackGithub(c *gin.Context) {
|
||||
err = json.NewDecoder(resp.Body).Decode(&ghUser)
|
||||
if err != nil {
|
||||
h.logger.Error("Decode failed: " + err.Error())
|
||||
c.Redirect(302, "https://d3m0k1d.ru/login?error=decode_failed")
|
||||
c.Redirect(302, h.frontendURL+"/login?error=decode_failed")
|
||||
return
|
||||
}
|
||||
|
||||
isreg, err := h.repo.IsRegistered(c.Request.Context(), ghUser.GithubID)
|
||||
if err != nil {
|
||||
h.logger.Error("Database check failed: " + err.Error())
|
||||
c.Redirect(302, "https://d3m0k1d.ru/login?error=database_error")
|
||||
c.Redirect(302, h.frontendURL+"/login?error=database_error")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -116,7 +121,7 @@ func (h *AuthHandlers) CallbackGithub(c *gin.Context) {
|
||||
id, err = h.repo.Register(c.Request.Context(), ghUser)
|
||||
if err != nil {
|
||||
h.logger.Error("Registration failed: " + err.Error())
|
||||
c.Redirect(302, "https://d3m0k1d.ru/login?error=registration_failed")
|
||||
c.Redirect(302, h.frontendURL+"/login?error=registration_failed")
|
||||
return
|
||||
}
|
||||
} else {
|
||||
@@ -124,7 +129,7 @@ func (h *AuthHandlers) CallbackGithub(c *gin.Context) {
|
||||
user, err := h.repo.GetUserByGithubID(c.Request.Context(), ghUser.GithubID)
|
||||
if err != nil {
|
||||
h.logger.Error("Failed to fetch user: " + err.Error())
|
||||
c.Redirect(302, "https://d3m0k1d.ru/login?error=user_fetch_failed")
|
||||
c.Redirect(302, h.frontendURL+"/login?error=user_fetch_failed")
|
||||
return
|
||||
}
|
||||
id = user.ID
|
||||
@@ -144,13 +149,13 @@ func (h *AuthHandlers) CallbackGithub(c *gin.Context) {
|
||||
jwtToken, err := auth.GenerateJWT(user)
|
||||
if err != nil {
|
||||
h.logger.Error("JWT generation failed: " + err.Error())
|
||||
c.Redirect(302, "https://d3m0k1d.ru/login?error=token_failed")
|
||||
c.Redirect(302, h.frontendURL+"/login?error=token_failed")
|
||||
return
|
||||
}
|
||||
|
||||
h.logger.Info("Authentication successful for user: " + ghUser.GithubLogin)
|
||||
|
||||
c.Redirect(302, "https://d3m0k1d.ru/auth/callback#token="+jwtToken)
|
||||
c.Redirect(302, h.frontendURL+"/auth/callback#token="+jwtToken)
|
||||
}
|
||||
|
||||
// GetSession godoc
|
||||
|
||||
Reference in New Issue
Block a user