Skip to content

Commit

Permalink
Merge pull request #459 from bete7512/apple_outh_fix
Browse files Browse the repository at this point in the history
Apple outh fix
  • Loading branch information
lakhansamani authored Aug 24, 2024
2 parents 9336d3e + a5e5d2e commit 70f2de7
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions server/handlers/oauth_callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ import (
)

// OAuthCallbackHandler handles the OAuth callback for various oauth providers
type AppleUserInfo struct {
Email string `json:"email"`
Name struct {
FirstName string `json:"firstName"`
LastName string `json:"lastName"`
} `json:"name"`
}

func OAuthCallbackHandler() gin.HandlerFunc {
return func(ctx *gin.Context) {
provider := ctx.Param("oauth_provider")
Expand Down Expand Up @@ -80,7 +88,10 @@ func OAuthCallbackHandler() gin.HandlerFunc {
case constants.AuthRecipeMethodLinkedIn:
user, err = processLinkedInUserInfo(ctx, oauthCode)
case constants.AuthRecipeMethodApple:
user, err = processAppleUserInfo(ctx, oauthCode)
user_ := AppleUserInfo{}
userRaw := ctx.Request.FormValue("user")
err = json.Unmarshal([]byte(userRaw), &user_)
user, err = processAppleUserInfo(ctx, oauthCode, &user_)
case constants.AuthRecipeMethodDiscord:
user, err = processDiscordUserInfo(ctx, oauthCode)
case constants.AuthRecipeMethodTwitter:
Expand All @@ -101,7 +112,13 @@ func OAuthCallbackHandler() gin.HandlerFunc {
ctx.JSON(400, gin.H{"error": err.Error()})
return
}

if user == nil{
ctx.JSON(
500,
gin.H{"error": "Something Went Wrong. Please Try Again."},
)
return
}
existingUser, err := db.Provider.GetUserByEmail(ctx, refs.StringValue(user.Email))
log := log.WithField("user", user.Email)
isSignUp := false
Expand Down Expand Up @@ -569,7 +586,7 @@ func processLinkedInUserInfo(ctx context.Context, code string) (*models.User, er
return user, nil
}

func processAppleUserInfo(ctx context.Context, code string) (*models.User, error) {
func processAppleUserInfo(ctx context.Context, code string, user_ *AppleUserInfo) (*models.User, error) {
var user = &models.User{}
oauth2Token, err := oauth.OAuthProviders.AppleConfig.Exchange(ctx, code)
if err != nil {
Expand Down Expand Up @@ -618,8 +635,10 @@ func processAppleUserInfo(ctx context.Context, code string) (*models.User, error
user.FamilyName = &familyName
}
}
user.GivenName = &user_.Name.FirstName
user.FamilyName = &user_.Name.LastName

return nil, err
return user, err
}

func processDiscordUserInfo(ctx context.Context, code string) (*models.User, error) {
Expand Down

0 comments on commit 70f2de7

Please sign in to comment.