Update backend #6
@@ -6,6 +6,7 @@ import (
|
||||
|
||||
"gitea.d3m0k1d.ru/d3m0k1d/d3m0k1d.ru/backend/internal/logger"
|
||||
"gitea.d3m0k1d.ru/d3m0k1d/d3m0k1d.ru/backend/internal/repositories"
|
||||
"gitea.d3m0k1d.ru/d3m0k1d/d3m0k1d.ru/backend/internal/storage"
|
||||
"github.com/gin-gonic/gin"
|
||||
"golang.org/x/oauth2"
|
||||
"golang.org/x/oauth2/endpoints"
|
||||
@@ -89,28 +90,30 @@ func (h *AuthHandlers) CallbackGithub(c *gin.Context) {
|
||||
c.JSON(500, gin.H{"error": "get request failed to github", "details": err.Error()})
|
||||
return
|
||||
}
|
||||
var ghUser struct {
|
||||
ID int `json:"id"`
|
||||
Login string `json:"login"`
|
||||
Email string `json:"email"`
|
||||
AvatarURL string `json:"avatar_url"`
|
||||
}
|
||||
var ghUser storage.UserReg
|
||||
err = json.NewDecoder(resp.Body).Decode(&ghUser)
|
||||
if err != nil {
|
||||
h.logger.Error("Decode failed: " + err.Error())
|
||||
c.JSON(500, gin.H{"error": "decode failed", "details": err.Error()})
|
||||
return
|
||||
}
|
||||
isreg, err := h.repo.IsRegistered(c.Request.Context(), ghUser.ID)
|
||||
isreg, err := h.repo.IsRegistered(c.Request.Context(), ghUser.GithubID)
|
||||
if err != nil {
|
||||
c.JSON(500, gin.H{"error": "database error", "details": err.Error()})
|
||||
return
|
||||
}
|
||||
if isreg {
|
||||
c.JSON(200, gin.H{"user": ghUser})
|
||||
return
|
||||
|
||||
reg := h.repo.Register(c.Request.Context(), ghUser)
|
||||
if reg != nil {
|
||||
c.JSON(500, gin.H{"error": "database error", "details": reg.Error()})
|
||||
err := h.repo.Register(c.Request.Context(), ghUser)
|
||||
if err != nil {
|
||||
c.JSON(500, gin.H{"error": "database error", "details": err.Error()})
|
||||
h.logger.Error("Database eer in gh callback handler")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
h.logger.Info("200 OK - token received")
|
||||
c.JSON(200, gin.H{"user": ghUser})
|
||||
}
|
||||
|
||||
@@ -1,34 +1,34 @@
|
||||
package storage
|
||||
|
||||
type Post struct {
|
||||
ID int `db:"id"`
|
||||
Title string `db:"title"`
|
||||
Content string `db:"content"`
|
||||
CreatedAt string `db:"created_at"`
|
||||
ID int `db:"id" json:"id"`
|
||||
Title string `db:"title" json:"title"`
|
||||
Content string `db:"content" json:"content"`
|
||||
CreatedAt string `db:"created_at" json:"created_at"`
|
||||
}
|
||||
|
||||
type PostReq struct {
|
||||
ID int `json:"id"`
|
||||
Title string `json:"title"`
|
||||
Content string `json:"content"`
|
||||
ID int `db:"id" json:"id"`
|
||||
Title string `db:"title" json:"title"`
|
||||
Content string `db:"content" json:"content"`
|
||||
}
|
||||
|
||||
type PostCreate struct {
|
||||
Title string `json:"title"`
|
||||
Content string `json:"content"`
|
||||
Title string `db:"title" json:"title"`
|
||||
Content string `db:"content" json:"content"`
|
||||
}
|
||||
|
||||
type User struct {
|
||||
ID int `db:"id"`
|
||||
Email string `db:"email"`
|
||||
GithubID string `db:"github_id"`
|
||||
GithubLogin string `db:"github_login"`
|
||||
AvatarURL string `db:"avatar_url"`
|
||||
ID int `db:"id" json:"id"`
|
||||
Email string `db:"email" json:"email"`
|
||||
GithubID int `db:"github_id" json:"github_id"`
|
||||
GithubLogin string `db:"github_login" json:"github_login"`
|
||||
AvatarURL string `db:"avatar_url" json:"avatar_url"`
|
||||
}
|
||||
|
||||
type UserReg struct {
|
||||
Email string `db:"email"`
|
||||
GithubID string `db:"github_id"`
|
||||
GithubLogin string `db:"github_login"`
|
||||
AvatarURL string `db:"avatar_url"`
|
||||
Email string `db:"email" json:"email"`
|
||||
GithubID int `db:"github_id" json:"github_id"`
|
||||
GithubLogin string `db:"github_login" json:"github_login"`
|
||||
AvatarURL string `db:"avatar_url" json:"avatar_url"`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user