7
7
"net/http"
8
8
"strconv"
9
9
"strings"
10
- "time"
11
10
12
11
jwt "github.com/appleboy/gin-jwt/v2"
13
12
"github.com/gin-gonic/gin"
@@ -114,10 +113,10 @@ func unbindOauth2(c *gin.Context) (any, error) {
114
113
// @Produce json
115
114
// @Param state query string true "state"
116
115
// @Param code query string true "code"
117
- // @Success 200 {object} model.LoginResponse
116
+ // @Success 200 {object} model.CommonResponse[any]
118
117
// @Router /api/v1/oauth2/callback [get]
119
- func oauth2callback (jwtConfig * jwt.GinJWTMiddleware ) func (c * gin.Context ) (* model. LoginResponse , error ) {
120
- return func (c * gin.Context ) (* model. LoginResponse , error ) {
118
+ func oauth2callback (jwtConfig * jwt.GinJWTMiddleware ) func (c * gin.Context ) (any , error ) {
119
+ return func (c * gin.Context ) (any , error ) {
121
120
callbackData := & model.Oauth2Callback {
122
121
State : c .Query ("state" ),
123
122
Code : c .Query ("code" ),
@@ -146,6 +145,7 @@ func oauth2callback(jwtConfig *jwt.GinJWTMiddleware) func(c *gin.Context) (*mode
146
145
}
147
146
148
147
var bind model.Oauth2Bind
148
+ state .Provider = strings .ToLower (state .Provider )
149
149
switch state .Action {
150
150
case model .RTypeBind :
151
151
u , authorized := c .Get (model .CtxKeyAuthorizedUser )
@@ -154,7 +154,7 @@ func oauth2callback(jwtConfig *jwt.GinJWTMiddleware) func(c *gin.Context) (*mode
154
154
}
155
155
user := u .(* model.User )
156
156
157
- result := singleton .DB .Where ("provider = ? AND open_id = ?" , strings . ToLower ( state .Provider ) , openId ).Limit (1 ).Find (& bind )
157
+ result := singleton .DB .Where ("provider = ? AND open_id = ?" , state .Provider , openId ).Limit (1 ).Find (& bind )
158
158
if result .Error != nil && result .Error != gorm .ErrRecordNotFound {
159
159
return nil , newGormError ("%v" , result .Error )
160
160
}
@@ -171,20 +171,20 @@ func oauth2callback(jwtConfig *jwt.GinJWTMiddleware) func(c *gin.Context) (*mode
171
171
return nil , newGormError ("%v" , result .Error )
172
172
}
173
173
default :
174
- if err := singleton .DB .Where ("provider = ? AND open_id = ?" , strings . ToLower ( state .Provider ) , openId ).First (& bind ).Error ; err != nil {
174
+ if err := singleton .DB .Where ("provider = ? AND open_id = ?" , state .Provider , openId ).First (& bind ).Error ; err != nil {
175
175
return nil , singleton .Localizer .ErrorT ("oauth2 user not binded yet" )
176
176
}
177
177
}
178
178
179
- tokenString , expire , err := jwtConfig .TokenGenerator (fmt .Sprintf ("%d" , bind .UserID ))
179
+ tokenString , _ , err := jwtConfig .TokenGenerator (fmt .Sprintf ("%d" , bind .UserID ))
180
180
if err != nil {
181
181
return nil , err
182
182
}
183
183
184
184
jwtConfig .SetCookie (c , tokenString )
185
185
c .Redirect (http .StatusFound , utils .IfOr (state .Action == model .RTypeBind , "/dashboard/profile?oauth2=true" , "/dashboard/login?oauth2=true" ))
186
186
187
- return & model. LoginResponse { Token : tokenString , Expire : expire . Format ( time . RFC3339 )}, nil
187
+ return nil , errNoop
188
188
}
189
189
}
190
190
0 commit comments