Skip to content

Commit

Permalink
session接口触发时主动刷新accessToken
Browse files Browse the repository at this point in the history
  • Loading branch information
gcslaoli committed Oct 18, 2023
1 parent 9279ac9 commit d67338b
Showing 1 changed file with 28 additions and 6 deletions.
34 changes: 28 additions & 6 deletions api/session.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package api

import (
backendapi "chatgpt-mirror-server/backend-api"
"chatgpt-mirror-server/config"
"chatgpt-mirror-server/modules/chatgpt/model"
"net/http"
"time"

"github.com/cool-team-official/cool-admin-go/cool"
"github.com/gogf/gf/v2/encoding/gjson"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
Expand All @@ -23,10 +28,27 @@ func Session(r *ghttp.Request) {
return
}
officialSession := gjson.New(record["officialSession"].String())

officialSession.Set("accessToken", userToken.String())
officialSession.Set("user.email", "[email protected]")
officialSession.Set("user.name", expireTime)
officialSession.Remove("refreshCookie")
r.Response.WriteJsonExit(officialSession)
getSessionUrl := config.CHATPROXY(ctx) + "/getsession"
refreshCookie := officialSession.Get("refreshCookie").String()
sessionVar := g.Client().SetHeader("authkey", config.AUTHKEY(ctx)).PostVar(ctx, getSessionUrl, g.Map{
"username": record["email"].String(),
"password": record["password"].String(),
"authkey": config.AUTHKEY(ctx),
"refreshCookie": refreshCookie,
})
sessionJson := gjson.New(sessionVar)
if sessionJson.Get("accessToken").String() == "" {
g.Log().Error(ctx, "get session error", sessionJson)
r.Response.WriteStatus(http.StatusUnauthorized)
return
}
cool.DBM(model.NewChatgptSession()).Where("email=?", record["email"].String()).Update(g.Map{
"officialSession": sessionJson.String(),
})
backendapi.AccessTokenCache.Set(ctx, userToken.String(), sessionJson.Get("accessToken").String(), 10*24*time.Hour)
sessionJson.Set("accessToken", userToken.String())
sessionJson.Set("user.email", "[email protected]")
sessionJson.Set("user.name", expireTime)
sessionJson.Remove("refreshCookie")
r.Response.WriteJsonExit(sessionJson)
}

0 comments on commit d67338b

Please sign in to comment.