Refactor JWT service and token DTO to replace TenantID with ClientID for improved clarity and consistency
This commit is contained in:
parent
2d20f892e7
commit
6aa45f1522
|
|
@ -10,7 +10,6 @@ const (
|
|||
type TokenResponse struct {
|
||||
AccessToken string `json:"access_token"`
|
||||
RefreshToken string `json:"refresh_token"`
|
||||
Role string `json:"role"`
|
||||
}
|
||||
|
||||
type RefreshTokenRequest struct {
|
||||
|
|
|
|||
|
|
@ -1,15 +1,21 @@
|
|||
package auth
|
||||
|
||||
import (
|
||||
"github.com/Caknoooo/go-gin-clean-starter/modules/user/controller"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/samber/do"
|
||||
)
|
||||
|
||||
func RegisterRoutes(server *gin.Engine, injector *do.Injector) {
|
||||
// Auth routes akan ditambahkan nanti ketika auth controller sudah dibuat
|
||||
userController := do.MustInvoke[controller.UserController](injector)
|
||||
|
||||
authRoutes := server.Group("/api/v1/auth")
|
||||
{
|
||||
// authRoutes.POST("/refresh-token", authController.RefreshToken)
|
||||
_ = authRoutes // untuk menghindari unused variable
|
||||
authRoutes.POST("/register", userController.Register)
|
||||
authRoutes.POST("/login", userController.Login)
|
||||
authRoutes.POST("/verify-email", userController.VerifyEmail)
|
||||
authRoutes.POST("/send-verification-email", userController.SendVerificationEmail)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,21 +12,20 @@ import (
|
|||
)
|
||||
|
||||
type UserTokenInfo struct {
|
||||
TenantID string `json:"tenant_id"`
|
||||
ClientID string `json:"client_id"`
|
||||
UserID string `json:"user_id"`
|
||||
}
|
||||
|
||||
type JWTService interface {
|
||||
GenerateAccessToken(tenantId string, userId string, role string) string
|
||||
GenerateAccessToken(clientId string, userId string) string
|
||||
GenerateRefreshToken() (string, time.Time)
|
||||
ValidateToken(token string) (*jwt.Token, error)
|
||||
GetUserIDByToken(token string) (*UserTokenInfo, error)
|
||||
}
|
||||
|
||||
type jwtCustomClaim struct {
|
||||
TenantID string `json:"tenant_id"`
|
||||
ClientID string `json:"client_id"`
|
||||
UserID string `json:"user_id"`
|
||||
Role string `json:"role"`
|
||||
|
||||
jwt.RegisteredClaims
|
||||
}
|
||||
|
|
@ -55,11 +54,10 @@ func getSecretKey() string {
|
|||
return secretKey
|
||||
}
|
||||
|
||||
func (j *jwtService) GenerateAccessToken(tenantId string, userId string, role string) string {
|
||||
func (j *jwtService) GenerateAccessToken(clientId string, userId string) string {
|
||||
claims := jwtCustomClaim{
|
||||
TenantID: tenantId,
|
||||
ClientID: clientId,
|
||||
UserID: userId,
|
||||
Role: role,
|
||||
RegisteredClaims: jwt.RegisteredClaims{
|
||||
ExpiresAt: jwt.NewNumericDate(time.Now().Add(j.accessExpiry)),
|
||||
Issuer: j.issuer,
|
||||
|
|
@ -108,9 +106,9 @@ func (j *jwtService) GetUserIDByToken(token string) (*UserTokenInfo, error) {
|
|||
|
||||
claims := tToken.Claims.(jwt.MapClaims)
|
||||
userId := fmt.Sprintf("%v", claims["user_id"])
|
||||
tenantId := fmt.Sprintf("%v", claims["tenant_id"])
|
||||
clientId := fmt.Sprintf("%v", claims["client_id"])
|
||||
return &UserTokenInfo{
|
||||
UserID: userId,
|
||||
TenantID: tenantId,
|
||||
ClientID: clientId,
|
||||
}, nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue