Skip to content

Commit

Permalink
[No Card] - Add Refresh Token in this Template (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
adiatma85 authored Apr 25, 2024
1 parent ae02ba9 commit f9dcba4
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 10 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/adiatma85/new-go-template
go 1.21

require (
github.com/adiatma85/own-go-sdk v0.1.11
github.com/adiatma85/own-go-sdk v0.1.12
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751
github.com/swaggo/files v0.0.0-20210815190702-a29dd2bc99b2
github.com/swaggo/gin-swagger v1.3.2
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ github.com/PuerkitoBio/purell v1.1.1 h1:WEQqlqaGbrPkxLJWfBwQmfEAE1Z7ONdDLqrN38tN
github.com/PuerkitoBio/purell v1.1.1/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0=
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 h1:d+Bc7a5rLufV/sSk/8dngufqelfh6jnri85riMAaF/M=
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE=
github.com/adiatma85/own-go-sdk v0.1.11 h1:9FB6YZR5YyuvcqGmOB6sIqf4hQSMwCv1g8zbKI1Jo1M=
github.com/adiatma85/own-go-sdk v0.1.11/go.mod h1:o04374NSub2BSVk3tdfAxAlysR2+jOKYZWw50/cHXK0=
github.com/adiatma85/own-go-sdk v0.1.12 h1:TLoGpGZhY3CMlROMvrpknO5EoDT1t4Z75grs6zNaRE4=
github.com/adiatma85/own-go-sdk v0.1.12/go.mod h1:o04374NSub2BSVk3tdfAxAlysR2+jOKYZWw50/cHXK0=
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 h1:JYp7IbQjafoB+tBA3gMyHYHrpOtNuDiK/uB5uXxq5wM=
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
Expand Down
7 changes: 4 additions & 3 deletions src/business/entity/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,10 @@ type UserLoginRequest struct {
}

type UserLoginResponse struct {
Email string `json:"email"`
DisplayName string `json:"displayName"`
AccessToken string `json:"accessToken"`
Email string `json:"email"`
DisplayName string `json:"displayName"`
AccessToken string `json:"accessToken"`
RefreshToken string `json:"refreshToken"`
}

type ChangePasswordRequest struct {
Expand Down
51 changes: 47 additions & 4 deletions src/business/usecase/user/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ type Interface interface {
GetSelfProfile(ctx context.Context) (entity.User, error)
SelfDelete(ctx context.Context) error
ChangePassword(ctx context.Context, changePasswordReq entity.ChangePasswordRequest) error
RefreshToken(ctx context.Context) (entity.UserLoginResponse, error)

// Improvement kedepannya
// CheckPassword(ctx context.Context, params entity.UserCheckPasswordParam, userParam entity.UserParam) (entity.HTTPMessage, error)
// ChangePassword(ctx context.Context, passwordChangeParam entity.UserChangePasswordParam, userParam entity.UserParam) (entity.HTTPMessage, error)
// Activate(ctx context.Context, selectParam entity.UserParam) error
// RefreshToken(ctx context.Context, param entity.UserRefreshTokenParam) (entity.RefreshTokenResponse, error)
}

type InitParam struct {
Expand Down Expand Up @@ -84,6 +84,9 @@ func (u *user) CreateWithoutAuthInfo(ctx context.Context, req entity.CreateUserP

// Hash the password in here
req.Password, err = u.getHashPassowrd(req.Password)
if err != nil {
return result, err
}

return u.user.Create(ctx, req)
}
Expand Down Expand Up @@ -195,10 +198,17 @@ func (u *user) SignInWithPassword(ctx context.Context, req entity.UserLoginReque
return entity.UserLoginResponse{}, err
}

// Create the JWT Refresh token in here
refreshToken, err := u.jwtAuth.CreateRefreshToken(user.ConvertToAuthUser())
if err != nil {
return entity.UserLoginResponse{}, err
}

result := entity.UserLoginResponse{
Email: user.Email,
DisplayName: user.DisplayName,
AccessToken: accessToken,
Email: user.Email,
DisplayName: user.DisplayName,
AccessToken: accessToken,
RefreshToken: refreshToken,
}

return result, nil
Expand Down Expand Up @@ -266,6 +276,9 @@ func (u *user) ChangePassword(ctx context.Context, changePasswordReq entity.Chan
IsActive: true,
},
})
if err != nil {
return err
}

if u.checkHashPassword(userDn.Password, changePasswordReq.OldPassword) {
return errors.NewWithCode(codes.CodeUnauthorized, "credential does not match")
Expand All @@ -287,3 +300,33 @@ func (u *user) ChangePassword(ctx context.Context, changePasswordReq entity.Chan

return u.user.Update(ctx, updateParam, selectParam)
}

func (u *user) RefreshToken(ctx context.Context) (entity.UserLoginResponse, error) {
var (
result entity.UserLoginResponse
)

jwtUser, err := u.jwtAuth.GetUserAuthInfo(ctx)
if err != nil {
return result, err
}

// Generate access token in here
accessToken, err := u.jwtAuth.CreateAccessToken(jwtUser.User)
if err != nil {
return result, err
}

// Generate refresh token in here
refreshToken, err := u.jwtAuth.CreateRefreshToken(jwtUser.User)
if err != nil {
return result, err
}

result = entity.UserLoginResponse{
AccessToken: accessToken,
RefreshToken: refreshToken,
}

return result, nil
}
20 changes: 20 additions & 0 deletions src/handler/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,23 @@ func (r *rest) SignInWithPassword(ctx *gin.Context) {

r.httpRespSuccess(ctx, codes.CodeSuccess, authInfo, nil)
}

// @Summary Sign In With Refresh Token
// @Description This endpoint will sign in user with refresh token
// @Tags Auth
// @Security BearerAuth
// @Produce json
// @Success 200 {object} entity.HTTPResp{data=entity.UserLoginResponse{}}
// @Failure 400 {object} entity.HTTPResp{}
// @Failure 404 {object} entity.HTTPResp{}
// @Failure 500 {object} entity.HTTPResp{}
// @Router /auth/v1/refresh-token [GET]
func (r *rest) RefreshToken(ctx *gin.Context) {
authInfo, err := r.uc.User.RefreshToken(ctx.Request.Context())
if err != nil {
r.httpRespError(ctx, err)
return
}

r.httpRespSuccess(ctx, codes.CodeSuccess, authInfo, nil)
}
1 change: 1 addition & 0 deletions src/handler/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ func (r *rest) Register() {
// auth api
authv1 := r.http.Group("/auth/v1", commonPublicMiddlewares...)
authv1.POST("/login", r.SignInWithPassword)
authv1.GET("/refresh-token", r.VerifyUser, r.RefreshToken)

// private api
v1 := r.http.Group("/v1/", commonPrivateMiddlewares...)
Expand Down

0 comments on commit f9dcba4

Please sign in to comment.