forked from feixuei/chatgpt-mirror-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsession.go
31 lines (27 loc) · 840 Bytes
/
session.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package api
import (
"net/http"
"github.com/gogf/gf/v2/encoding/gjson"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
)
func Session(r *ghttp.Request) {
ctx := r.GetCtx()
userToken := r.Session.MustGet("userToken")
record, expireTime, err := ChatgptSessionService.GetSessionByUserToken(ctx, userToken.String())
if err != nil {
g.Log().Error(ctx, err)
r.Response.WriteStatus(http.StatusUnauthorized)
return
}
if record.IsEmpty() {
g.Log().Error(ctx, "session is empty")
r.Response.WriteStatus(http.StatusUnauthorized)
return
}
officialSession := gjson.New(record["officialSession"].String())
officialSession.Set("accessToken", userToken.String())
officialSession.Set("user.email", "[email protected]")
officialSession.Set("user.name", expireTime)
r.Response.WriteJsonExit(officialSession)
}