package auth import ( "nixcn-cms/internal/exception" "nixcn-cms/pkgs/authtoken" "nixcn-cms/utils" "github.com/gin-gonic/gin" "github.com/spf13/viper" ) func Refresh(c *gin.Context) { var req struct { RefreshToken string `json:"refresh_token"` } if err := c.ShouldBindJSON(&req); err != nil { errorCode := new(exception.Builder). SetStatus(exception.StatusUser). SetService(exception.ServiceAuth). SetEndpoint(exception.EndpointAuthServiceRefresh). SetType(exception.TypeCommon). SetOriginal(exception.CommonErrorInvalidInput). SetError(err). Build() utils.HttpResponse(c, 400, errorCode) return } JwtTool := authtoken.Token{ Application: viper.GetString("server.application"), } accessToken, err := JwtTool.RefreshAccessToken(c, req.RefreshToken) if err != nil { errorCode := new(exception.Builder). SetStatus(exception.StatusUser). SetService(exception.ServiceAuth). SetEndpoint(exception.EndpointAuthServiceRefresh). SetType(exception.TypeSpecific). SetOriginal(exception.AuthRefreshInvalidToken). SetError(err). Build() utils.HttpResponse(c, 401, errorCode) return } refreshToken, err := JwtTool.RenewRefreshToken(c, req.RefreshToken) if err != nil { errorCode := new(exception.Builder). SetStatus(exception.StatusServer). SetService(exception.ServiceAuth). SetEndpoint(exception.EndpointAuthServiceRefresh). SetType(exception.TypeSpecific). SetOriginal(exception.AuthRefreshRenewFailed). SetError(err). Build() utils.HttpResponse(c, 500, errorCode) return } tokenResp := struct { AccessToken string `json:"access_token"` RefreshToken string `json:"refresh_token"` }{accessToken, refreshToken} errorCode := new(exception.Builder). SetStatus(exception.StatusServer). SetService(exception.ServiceAuth). SetEndpoint(exception.EndpointAuthServiceRefresh). SetType(exception.TypeCommon). SetOriginal(exception.CommonSuccess). Build() utils.HttpResponse(c, 200, errorCode, tokenResp) }