token 简化
This commit is contained in:
		
							
								
								
									
										21
									
								
								bin/main.go
									
									
									
									
									
								
							
							
						
						
									
										21
									
								
								bin/main.go
									
									
									
									
									
								
							@@ -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)))
 | 
			
		||||
	})
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user