Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
Signed-off-by: Mathieu <[email protected]>
  • Loading branch information
mathieu-brl authored and Mael-RABOT committed Dec 9, 2024
1 parent 71136d2 commit b095e6d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 16 deletions.
11 changes: 1 addition & 10 deletions server/internal/controllers/oauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
Expand Down Expand Up @@ -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) {
Expand Down
17 changes: 11 additions & 6 deletions server/internal/controllers/oauth/microsoft.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package oauth
import (
"AREA/internal/models"
"AREA/internal/pkg"
"AREA/internal/utils"
"encoding/json"
"errors"
"fmt"
Expand All @@ -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
}
}
Expand Down Expand Up @@ -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)
}

0 comments on commit b095e6d

Please sign in to comment.