refactor login json response
This commit is contained in:
parent
cb14cd40a4
commit
f77288369a
79
api/user.go
79
api/user.go
@ -20,24 +20,30 @@ type createUserMerchantRequest struct {
|
||||
}
|
||||
|
||||
type userMerchantResponse struct {
|
||||
ID uuid.UUID `json:"id"`
|
||||
IndexID int64 `json:"index_id"`
|
||||
Email string `json:"email"`
|
||||
Fullname string `json:"fullname"`
|
||||
CreatedAt sql.NullTime `json:"created_at"`
|
||||
UpdatedAt sql.NullTime `json:"updated_at"`
|
||||
Outlet db.Merchant `json:"outlet"`
|
||||
ID uuid.UUID `json:"id"`
|
||||
IndexID int64 `json:"index_id"`
|
||||
Email string `json:"email"`
|
||||
Fullname string `json:"fullname"`
|
||||
CreatedAt sql.NullTime `json:"created_at"`
|
||||
UpdatedAt sql.NullTime `json:"updated_at"`
|
||||
OutletID uuid.UUID `json:"outlet_id"`
|
||||
OutletIndexID int64 `json:"outlet_index_id"`
|
||||
OutletName string `json:"outlet_name"`
|
||||
OutletOwnerID uuid.UUID `json:"outlet_owner_id"`
|
||||
}
|
||||
|
||||
func newUserMerchantResponse(user db.GetUserByEmailRow, merchant db.Merchant) userMerchantResponse {
|
||||
func newUserMerchantResponse(user db.GetUserByEmailRow) userMerchantResponse {
|
||||
return userMerchantResponse{
|
||||
ID: user.ID,
|
||||
IndexID: user.IndexID,
|
||||
Email: user.Email,
|
||||
Fullname: user.Fullname,
|
||||
CreatedAt: sql.NullTime{Valid: true, Time: user.CreatedAt.Time},
|
||||
UpdatedAt: sql.NullTime{Valid: true, Time: user.UpdatedAt.Time},
|
||||
Outlet: merchant,
|
||||
ID: user.ID,
|
||||
IndexID: user.IndexID,
|
||||
Email: user.Email,
|
||||
Fullname: user.Fullname,
|
||||
CreatedAt: sql.NullTime{Valid: true, Time: user.CreatedAt.Time},
|
||||
UpdatedAt: sql.NullTime{Valid: true, Time: user.UpdatedAt.Time},
|
||||
OutletID: user.ID_2,
|
||||
OutletIndexID: user.IndexID_2,
|
||||
OutletName: user.Name,
|
||||
OutletOwnerID: user.OwnerID,
|
||||
}
|
||||
}
|
||||
|
||||
@ -75,13 +81,16 @@ func (server *Server) createUserMerchant(ctx *gin.Context) {
|
||||
}
|
||||
|
||||
res := userMerchantResponse{
|
||||
ID: user.OwnerProfile.ID,
|
||||
IndexID: user.OwnerProfile.IndexID,
|
||||
Email: user.OwnerProfile.Email,
|
||||
Fullname: user.OwnerProfile.Fullname,
|
||||
CreatedAt: sql.NullTime{Valid: true, Time: user.OwnerProfile.CreatedAt.Time},
|
||||
UpdatedAt: sql.NullTime{Valid: true, Time: user.OwnerProfile.UpdatedAt.Time},
|
||||
Outlet: user.OutletProfile,
|
||||
ID: user.OwnerProfile.ID,
|
||||
IndexID: user.OwnerProfile.IndexID,
|
||||
Email: user.OwnerProfile.Email,
|
||||
Fullname: user.OwnerProfile.Fullname,
|
||||
CreatedAt: sql.NullTime{Valid: true, Time: user.OwnerProfile.CreatedAt.Time},
|
||||
UpdatedAt: sql.NullTime{Valid: true, Time: user.OwnerProfile.UpdatedAt.Time},
|
||||
OutletID: user.OutletProfile.ID,
|
||||
OutletIndexID: user.OutletProfile.IndexID,
|
||||
OutletName: user.OutletProfile.Name,
|
||||
OutletOwnerID: user.OutletProfile.OwnerID,
|
||||
}
|
||||
|
||||
ctx.JSON(http.StatusOK, res)
|
||||
@ -92,13 +101,17 @@ type userLoginRequest struct {
|
||||
Password string `json:"password" binding:"required"`
|
||||
}
|
||||
|
||||
type userLoginResponse struct {
|
||||
type userTokenResponse struct {
|
||||
SessionID uuid.UUID `json:"session_id"`
|
||||
AccesToken string `json:"access_token"`
|
||||
AccessTokenExpiresAt time.Time `json:"access_token_expires_at"`
|
||||
RefreshToken string `json:"refresh_token"`
|
||||
RefreshTokenExpiresAt time.Time `json:"refresh_token_expires_at"`
|
||||
UserMerchantResponse userMerchantResponse
|
||||
}
|
||||
|
||||
type userLoginResponse struct {
|
||||
UserTokenResponse userTokenResponse `json:"user_token"`
|
||||
UserMerchantResponse userMerchantResponse `json:"user"`
|
||||
}
|
||||
|
||||
func (server *Server) loginUser(ctx *gin.Context) {
|
||||
@ -118,12 +131,6 @@ func (server *Server) loginUser(ctx *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
outlet, err := server.store.GetMerchantByUserId(ctx, user.ID)
|
||||
if err != nil {
|
||||
ctx.JSON(http.StatusInternalServerError, errorResponse(err))
|
||||
return
|
||||
}
|
||||
|
||||
err = util.CheckPassword(req.Password, user.Password)
|
||||
if err != nil {
|
||||
ctx.JSON(http.StatusUnauthorized, errorResponse(err))
|
||||
@ -132,7 +139,7 @@ func (server *Server) loginUser(ctx *gin.Context) {
|
||||
|
||||
accessToken, accessPayload, err := server.tokenMaker.CreateToken(
|
||||
user.Email,
|
||||
outlet.ID.String(),
|
||||
user.ID_2.String(),
|
||||
server.config.TokenDuration,
|
||||
)
|
||||
|
||||
@ -143,7 +150,7 @@ func (server *Server) loginUser(ctx *gin.Context) {
|
||||
|
||||
refreshToken, refreshTokenPayload, err := server.tokenMaker.CreateToken(
|
||||
user.Email,
|
||||
outlet.ID.String(),
|
||||
user.ID_2.String(),
|
||||
server.config.RefreshTokenDuration,
|
||||
)
|
||||
|
||||
@ -162,13 +169,17 @@ func (server *Server) loginUser(ctx *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
res := userLoginResponse{
|
||||
tokenResponse := userTokenResponse{
|
||||
SessionID: session.ID,
|
||||
AccesToken: accessToken,
|
||||
AccessTokenExpiresAt: accessPayload.ExpiredAt,
|
||||
RefreshToken: refreshToken,
|
||||
RefreshTokenExpiresAt: refreshTokenPayload.ExpiredAt,
|
||||
UserMerchantResponse: newUserMerchantResponse(user, outlet),
|
||||
}
|
||||
|
||||
res := userLoginResponse{
|
||||
UserTokenResponse: tokenResponse,
|
||||
UserMerchantResponse: newUserMerchantResponse(user),
|
||||
}
|
||||
|
||||
ctx.JSON(http.StatusOK, res)
|
||||
|
Loading…
Reference in New Issue
Block a user