feat(profile): add endpoint to retrieve linked OAuth accounts for the user

- Implemented the GinProfileProviders function to fetch and return the current user's linked OAuth accounts.
- Added error handling for authentication and provider retrieval, ensuring proper responses for unauthorized access and server errors.
- Updated the user routing to include a new GET endpoint for accessing linked OAuth providers.
This commit is contained in:
Max 2026-04-03 10:28:07 +08:00
parent 413fd71841
commit 93641dea58
2 changed files with 46 additions and 2 deletions

View file

@ -430,3 +430,46 @@ func buildProfileUpdateRequest(data map[string]interface{}) ProfileUpdateRequest
return req return req
} }
// GinProfileProviders returns the current user's linked OAuth accounts
func GinProfileProviders(c *gin.Context) {
authInfo := authorized.GetInfo(c)
if authInfo == nil || authInfo.UserID == "" {
response.RespondWithError(c, response.StatusUnauthorized, &response.ErrorResponse{
Code: response.ErrInvalidClient.Code,
ErrorDescription: "User not authenticated",
})
return
}
provider, err := getUserProvider()
if err != nil {
response.RespondWithError(c, response.StatusInternalServerError, &response.ErrorResponse{
Code: response.ErrServerError.Code,
ErrorDescription: "Failed to get user provider",
})
return
}
accounts, err := provider.GetUserOAuthAccounts(c.Request.Context(), authInfo.UserID)
if err != nil {
log.Error("Failed to get OAuth accounts for user %s: %v", authInfo.UserID, err)
response.RespondWithError(c, response.StatusInternalServerError, &response.ErrorResponse{
Code: response.ErrServerError.Code,
ErrorDescription: "Failed to retrieve linked accounts",
})
return
}
if accounts == nil {
accounts = []maps.MapStrAny{}
}
for i := range accounts {
if email, ok := accounts[i]["email"].(string); ok && email != "" {
accounts[i]["email"] = MaskEmail(email)
}
}
response.RespondWithSuccess(c, response.StatusOK, accounts)
}

View file

@ -250,8 +250,9 @@ func attachSubscription(group *gin.RouterGroup, oauth types.OAuth) {
// User profile management // User profile management
func attachProfile(group *gin.RouterGroup, oauth types.OAuth) { func attachProfile(group *gin.RouterGroup, oauth types.OAuth) {
group.GET("/profile", oauth.Guard, GinProfileGet) // Get user profile group.GET("/profile", oauth.Guard, GinProfileGet) // Get user profile
group.PUT("/profile", oauth.Guard, GinProfileUpdate) // Update user profile group.PUT("/profile", oauth.Guard, GinProfileUpdate) // Update user profile
group.GET("/profile/providers", oauth.Guard, GinProfileProviders) // Get linked OAuth providers
} }
// User management (CRUD) // User management (CRUD)