diff --git a/server/internal/controllers/oauth.go b/server/internal/controllers/oauth.go index f9f2185..1d16b50 100644 --- a/server/internal/controllers/oauth.go +++ b/server/internal/controllers/oauth.go @@ -4,20 +4,12 @@ import ( "AREA/internal/controllers/oauth" "AREA/internal/models" "AREA/internal/pkg" - "AREA/internal/utils" "errors" - "fmt" "net/http" "github.com/gin-gonic/gin" ) -func googleCallback(c *gin.Context, token *models.Token) { - fmt.Println("GOOGLE SERVICE HANDLER") -} - - - type Token struct { Token string `json:"token" binding:"required"` } @@ -63,8 +55,7 @@ func OAuth(c *gin.Context) { return } - tokenString := utils.NewToken(c, user.Email) - c.JSON(http.StatusOK, gin.H{"username": user.Username, "email": user.Email, "jwt": tokenString}) + c.JSON(http.StatusOK, gin.H{"username": user.Username, "email": user.Email, "jwt": user.Token}) } /*func OAuthBind(c *gin.Context) { diff --git a/server/internal/controllers/oauth/microsoft.go b/server/internal/controllers/oauth/microsoft.go index 435bb14..1bd7bef 100644 --- a/server/internal/controllers/oauth/microsoft.go +++ b/server/internal/controllers/oauth/microsoft.go @@ -3,6 +3,7 @@ package oauth import ( "AREA/internal/models" "AREA/internal/pkg" + "AREA/internal/utils" "encoding/json" "errors" "fmt" @@ -18,27 +19,31 @@ type MicrosoftResponse struct { DisplayName string `json:"displayName"` } -func createAccount(response MicrosoftResponse, token *models.Token) (*models.User) { +func createAccount(c *gin.Context, response MicrosoftResponse, token *models.Token) (*models.User) { var user models.User - //user.Token = token.Value user.Email = token.Email user.Username = response.DisplayName + + user.Token = utils.NewToken(c, user.Email) + pkg.DB.Create(&user) token.UserID = user.ID pkg.DB.Create(&token) return &user } -func getBindedAccount(response MicrosoftResponse, token *models.Token) (*models.User, error) { - var user models.User +func getBindedAccount(c *gin.Context, response MicrosoftResponse, token *models.Token) (*models.User, error) { err := pkg.DB.Where("email = ? AND service_id = 3", token.Email).First(token).Error if errors.Is(err, gorm.ErrRecordNotFound) { - return createAccount(response, token), nil + return createAccount(c, response, token), nil } else { + var user models.User pkg.DB.Where("id = ?", token.UserID).First(&user) pkg.DB.Where("email = ?", token.Email).First(token).Update("value", token.Value) + user.Token = utils.NewToken(c, user.Email) + pkg.DB.UpdateColumns(&user) return &user, nil } } @@ -68,5 +73,5 @@ func MicrosoftCallback(c *gin.Context, token *models.Token) (*models.User, error var response MicrosoftResponse json.Unmarshal([]byte(b), &response) token.Email = response.Mail - return getBindedAccount(response, token) + return getBindedAccount(c, response, token) }