feat: add profile auth identity binding flow
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/Wei-Shaw/sub2api/internal/handler/dto"
|
||||
"github.com/Wei-Shaw/sub2api/internal/pkg/response"
|
||||
middleware2 "github.com/Wei-Shaw/sub2api/internal/server/middleware"
|
||||
@@ -41,7 +43,8 @@ type UpdateProfileRequest struct {
|
||||
|
||||
type userProfileResponse struct {
|
||||
dto.User
|
||||
AvatarURL string `json:"avatar_url,omitempty"`
|
||||
AvatarURL string `json:"avatar_url,omitempty"`
|
||||
Identities service.UserIdentitySummarySet `json:"identities"`
|
||||
}
|
||||
|
||||
// GetProfile handles getting user profile
|
||||
@@ -59,7 +62,13 @@ func (h *UserHandler) GetProfile(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
response.Success(c, userProfileResponseFromService(userData))
|
||||
profileResp, err := h.buildUserProfileResponse(c.Request.Context(), subject.UserID, userData)
|
||||
if err != nil {
|
||||
response.ErrorFrom(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
response.Success(c, profileResp)
|
||||
}
|
||||
|
||||
// ChangePassword handles changing user password
|
||||
@@ -117,7 +126,44 @@ func (h *UserHandler) UpdateProfile(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
response.Success(c, userProfileResponseFromService(updatedUser))
|
||||
profileResp, err := h.buildUserProfileResponse(c.Request.Context(), subject.UserID, updatedUser)
|
||||
if err != nil {
|
||||
response.ErrorFrom(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
response.Success(c, profileResp)
|
||||
}
|
||||
|
||||
type StartIdentityBindingRequest struct {
|
||||
Provider string `json:"provider" binding:"required"`
|
||||
RedirectTo string `json:"redirect_to"`
|
||||
}
|
||||
|
||||
// StartIdentityBinding returns the backend authorize URL for starting a third-party identity bind flow.
|
||||
// POST /api/v1/user/auth-identities/bind/start
|
||||
func (h *UserHandler) StartIdentityBinding(c *gin.Context) {
|
||||
if _, ok := middleware2.GetAuthSubjectFromContext(c); !ok {
|
||||
response.Unauthorized(c, "User not authenticated")
|
||||
return
|
||||
}
|
||||
|
||||
var req StartIdentityBindingRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.BadRequest(c, "Invalid request: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
result, err := h.userService.PrepareIdentityBindingStart(c.Request.Context(), service.StartUserIdentityBindingRequest{
|
||||
Provider: req.Provider,
|
||||
RedirectTo: req.RedirectTo,
|
||||
})
|
||||
if err != nil {
|
||||
response.ErrorFrom(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
response.Success(c, result)
|
||||
}
|
||||
|
||||
// SendNotifyEmailCodeRequest represents the request to send notify email verification code
|
||||
@@ -183,7 +229,13 @@ func (h *UserHandler) VerifyNotifyEmail(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
response.Success(c, userProfileResponseFromService(updatedUser))
|
||||
profileResp, err := h.buildUserProfileResponse(c.Request.Context(), subject.UserID, updatedUser)
|
||||
if err != nil {
|
||||
response.ErrorFrom(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
response.Success(c, profileResp)
|
||||
}
|
||||
|
||||
// RemoveNotifyEmailRequest represents the request to remove a notify email
|
||||
@@ -219,7 +271,13 @@ func (h *UserHandler) RemoveNotifyEmail(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
response.Success(c, userProfileResponseFromService(updatedUser))
|
||||
profileResp, err := h.buildUserProfileResponse(c.Request.Context(), subject.UserID, updatedUser)
|
||||
if err != nil {
|
||||
response.ErrorFrom(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
response.Success(c, profileResp)
|
||||
}
|
||||
|
||||
// ToggleNotifyEmailRequest represents the request to toggle a notify email's disabled state
|
||||
@@ -255,16 +313,31 @@ func (h *UserHandler) ToggleNotifyEmail(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
response.Success(c, userProfileResponseFromService(updatedUser))
|
||||
profileResp, err := h.buildUserProfileResponse(c.Request.Context(), subject.UserID, updatedUser)
|
||||
if err != nil {
|
||||
response.ErrorFrom(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
response.Success(c, profileResp)
|
||||
}
|
||||
|
||||
func userProfileResponseFromService(user *service.User) userProfileResponse {
|
||||
func (h *UserHandler) buildUserProfileResponse(ctx context.Context, userID int64, user *service.User) (userProfileResponse, error) {
|
||||
identities, err := h.userService.GetProfileIdentitySummaries(ctx, userID, user)
|
||||
if err != nil {
|
||||
return userProfileResponse{}, err
|
||||
}
|
||||
return userProfileResponseFromService(user, identities), nil
|
||||
}
|
||||
|
||||
func userProfileResponseFromService(user *service.User, identities service.UserIdentitySummarySet) userProfileResponse {
|
||||
base := dto.UserFromService(user)
|
||||
if base == nil {
|
||||
return userProfileResponse{}
|
||||
}
|
||||
return userProfileResponse{
|
||||
User: *base,
|
||||
AvatarURL: user.AvatarURL,
|
||||
User: *base,
|
||||
AvatarURL: user.AvatarURL,
|
||||
Identities: identities,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user