mirror of
https://github.com/Buriburizaem0n/nezha_domains.git
synced 2026-02-04 20:50:06 +00:00
fix: struct tag (#932)
* fix: struct tag * fix: struct tag * fix: strings conversion * do not write anything after redirection * fix table * fix select
This commit is contained in:
@@ -7,7 +7,6 @@ import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
jwt "github.com/appleboy/gin-jwt/v2"
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -114,10 +113,10 @@ func unbindOauth2(c *gin.Context) (any, error) {
|
||||
// @Produce json
|
||||
// @Param state query string true "state"
|
||||
// @Param code query string true "code"
|
||||
// @Success 200 {object} model.LoginResponse
|
||||
// @Success 200 {object} model.CommonResponse[any]
|
||||
// @Router /api/v1/oauth2/callback [get]
|
||||
func oauth2callback(jwtConfig *jwt.GinJWTMiddleware) func(c *gin.Context) (*model.LoginResponse, error) {
|
||||
return func(c *gin.Context) (*model.LoginResponse, error) {
|
||||
func oauth2callback(jwtConfig *jwt.GinJWTMiddleware) func(c *gin.Context) (any, error) {
|
||||
return func(c *gin.Context) (any, error) {
|
||||
callbackData := &model.Oauth2Callback{
|
||||
State: c.Query("state"),
|
||||
Code: c.Query("code"),
|
||||
@@ -146,6 +145,7 @@ func oauth2callback(jwtConfig *jwt.GinJWTMiddleware) func(c *gin.Context) (*mode
|
||||
}
|
||||
|
||||
var bind model.Oauth2Bind
|
||||
state.Provider = strings.ToLower(state.Provider)
|
||||
switch state.Action {
|
||||
case model.RTypeBind:
|
||||
u, authorized := c.Get(model.CtxKeyAuthorizedUser)
|
||||
@@ -154,7 +154,7 @@ func oauth2callback(jwtConfig *jwt.GinJWTMiddleware) func(c *gin.Context) (*mode
|
||||
}
|
||||
user := u.(*model.User)
|
||||
|
||||
result := singleton.DB.Where("provider = ? AND open_id = ?", strings.ToLower(state.Provider), openId).Limit(1).Find(&bind)
|
||||
result := singleton.DB.Where("provider = ? AND open_id = ?", state.Provider, openId).Limit(1).Find(&bind)
|
||||
if result.Error != nil && result.Error != gorm.ErrRecordNotFound {
|
||||
return nil, newGormError("%v", result.Error)
|
||||
}
|
||||
@@ -171,12 +171,12 @@ func oauth2callback(jwtConfig *jwt.GinJWTMiddleware) func(c *gin.Context) (*mode
|
||||
return nil, newGormError("%v", result.Error)
|
||||
}
|
||||
default:
|
||||
if err := singleton.DB.Where("provider = ? AND open_id = ?", strings.ToLower(state.Provider), openId).First(&bind).Error; err != nil {
|
||||
if err := singleton.DB.Where("provider = ? AND open_id = ?", state.Provider, openId).First(&bind).Error; err != nil {
|
||||
return nil, singleton.Localizer.ErrorT("oauth2 user not binded yet")
|
||||
}
|
||||
}
|
||||
|
||||
tokenString, expire, err := jwtConfig.TokenGenerator(fmt.Sprintf("%d", bind.UserID))
|
||||
tokenString, _, err := jwtConfig.TokenGenerator(fmt.Sprintf("%d", bind.UserID))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -184,7 +184,7 @@ func oauth2callback(jwtConfig *jwt.GinJWTMiddleware) func(c *gin.Context) (*mode
|
||||
jwtConfig.SetCookie(c, tokenString)
|
||||
c.Redirect(http.StatusFound, utils.IfOr(state.Action == model.RTypeBind, "/dashboard/profile?oauth2=true", "/dashboard/login?oauth2=true"))
|
||||
|
||||
return &model.LoginResponse{Token: tokenString, Expire: expire.Format(time.RFC3339)}, nil
|
||||
return nil, errNoop
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user