Skip to content

Commit adf2d09

Browse files
committed
userID in ai chat
1 parent eb7a90a commit adf2d09

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

backend/apis/message/api.go

+9-4
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,8 @@ func ListChatRecords(c *fiber.Ctx) (err error) {
142142

143143
// ListMyChatRecords @ListMyChatRecords
144144
// @Router /api/chats/{id}/records/me [get]
145-
// @Summary list my records
146-
// @Description list my records
145+
// @Summary list my records in a chat
146+
// @Description list my records in a chat
147147
// @Tags ChatRecord
148148
// @Accept json
149149
// @Produce json
@@ -259,6 +259,11 @@ func ListMyRecords(c *fiber.Ctx) (err error) {
259259
// @param Authorization header string true "Bearer和token空格拼接"
260260
func MossChat(c *websocket.Conn) {
261261
var err error
262+
userID, err := LoadUserFromWs(c)
263+
if err != nil {
264+
_ = c.WriteJSON(common.Unauthorized("Unauthorized"))
265+
return
266+
}
262267
defer func() {
263268
if err != nil {
264269
utils.Logger.Error(
@@ -289,7 +294,7 @@ func MossChat(c *websocket.Conn) {
289294
log.Println("Received message from client:", requestMessage)
290295
go addRecord(AddRecordsRequest{
291296
CreatedAt: time.Now(),
292-
UserID: 0,
297+
UserID: userID,
293298
RoomID: "moss",
294299
Type: "toMoss",
295300
ToID: 0,
@@ -339,7 +344,7 @@ func MossChat(c *websocket.Conn) {
339344
UserID: 0,
340345
RoomID: "moss",
341346
Type: "fromMoss",
342-
ToID: 0,
347+
ToID: userID,
343348
Message: mossResponse.Output,
344349
})
345350
return

backend/models/auth.go

+11-3
Original file line numberDiff line numberDiff line change
@@ -160,15 +160,23 @@ func GetUserIDFromWs(c *websocket.Conn) (int, error) {
160160
if !ok {
161161
return 0, common.Unauthorized()
162162
}
163+
userID := int(id.(float64))
164+
var userJwtSecret UserJwtSecret
165+
err = DB.Take(&userJwtSecret, userID).Error
166+
if err != nil {
167+
return 0, common.Unauthorized("Unauthorized")
168+
}
169+
CheckJWTToken(token, userJwtSecret.Secret)
163170
return int(id.(float64)), nil
164171
}
165172

166-
func LoadUserFromWs(c *websocket.Conn) (*User, error) {
173+
func LoadUserFromWs(c *websocket.Conn) (int, error) {
167174
userID, err := GetUserIDFromWs(c)
168175
if err != nil {
169-
return nil, err
176+
return 0, err
170177
}
171-
return LoadUserByID(userID)
178+
return userID, nil
179+
//LoadUserByID(userID)
172180
}
173181

174182
// parseJWT extracts and parse token

0 commit comments

Comments
 (0)