token 简化

This commit is contained in:
2024-12-02 11:11:40 +08:00
parent 51c8a7fe2e
commit 06070edded
5 changed files with 15 additions and 31 deletions

View File

@@ -68,26 +68,9 @@ func LogRequest(next http.Handler) http.Handler {
var user_id int
if token := r.Header.Get("token"); token != "" {
fmt.Println("token:", token)
rows, err := mysqlConnection.Database.Query("SELECT user_id FROM web_auth WHERE token = ? LIMIT 1", token)
if err != nil {
log.Println("查询失败:", err)
return
}
defer rows.Close()
if rows.Next() {
err = rows.Scan(&user_id)
if err != nil {
log.Println("扫描失败:", err)
return
}
}
user_id = api.ParseToken(token)
}
ctx := context.WithValue(r.Context(), "user_id", user_id)
fmt.Println("user_id:", user_id)
next.ServeHTTP(w, r.WithContext(ctx))
next.ServeHTTP(w, r.WithContext(context.WithValue(r.Context(), "user_id", user_id)))
})
}