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