Skip to content
This repository has been archived by the owner on Sep 2, 2024. It is now read-only.

Commit

Permalink
feat: oauth before login (#418)
Browse files Browse the repository at this point in the history
  • Loading branch information
im-adithya authored Jun 12, 2024
1 parent e3085fa commit 3bbfde9
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions alby/alby_oauth_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,24 +72,27 @@ func (svc *albyOAuthService) CallbackHandler(ctx context.Context, code string) e
}
svc.saveToken(token)

me, err := svc.GetMe(ctx)
if err != nil {
svc.logger.WithError(err).Error("Failed to fetch user me")
// remove token so user can retry
svc.config.SetUpdate(accessTokenKey, "", "")
return err
}

existingUserIdentifier, err := svc.GetUserIdentifier()
if err != nil {
svc.logger.WithError(err).Error("Failed to get alby user identifier")
return err
}

// setup Alby account on first time login
// save the user's alby account ID on first time login
if existingUserIdentifier == "" {
// fetch and save the user's alby account ID. This cannot be changed.
me, err := svc.GetMe(ctx)
if err != nil {
svc.logger.WithError(err).Error("Failed to fetch user me")
// remove token so user can retry
svc.config.SetUpdate(accessTokenKey, me.Identifier, "")
return err
}

svc.config.SetUpdate(userIdentifierKey, me.Identifier, "")
} else if me.Identifier != existingUserIdentifier {
// remove token so user can retry with correct account
svc.config.SetUpdate(accessTokenKey, "", "")
return errors.New("Alby Hub is connected to a different alby account. Please log out of your Alby Account at getalby.com and try again.")
}

return nil
Expand Down

0 comments on commit 3bbfde9

Please sign in to comment.