From 079df30deb5349c7dcdc235c8d194670bfd65476 Mon Sep 17 00:00:00 2001 From: xdzqyyds <2292136545@qq.com> Date: Thu, 28 Nov 2024 20:40:05 +0800 Subject: [PATCH] fix some little bug --- server/api/v1/login.go | 112 ++++++++++++++--------------- server/daemon/redis.go | 83 ++++++++++------------ webapp/components/login.tsx | 137 ++++++++++++++++++------------------ webapp/lib/api.ts | 10 +-- 4 files changed, 165 insertions(+), 177 deletions(-) diff --git a/server/api/v1/login.go b/server/api/v1/login.go index 89d7239..7078da6 100644 --- a/server/api/v1/login.go +++ b/server/api/v1/login.go @@ -1,56 +1,56 @@ -package v1 - -import ( - "context" - "encoding/json" - "log" - "net/http" - - "github.com/go-chi/render" -) - -type LoginRequest struct { - Username string `json:"username"` - Password string `json:"password"` -} - -type LoginResponse struct { - Success bool `json:"success"` - Message string `json:"message"` -} - -func (h *Handler) HandleLogin(w http.ResponseWriter, r *http.Request) { - var loginReq LoginRequest - - // Decode JSON request body - if err := json.NewDecoder(r.Body).Decode(&loginReq); err != nil { - w.WriteHeader(http.StatusBadRequest) - render.JSON(w, r, LoginResponse{Success: false, Message: "Invalid request"}) - return - } - - // Redis hash key - userStorageKey := "user_storage" - - // get password - ctx := context.Background() - passwordKey := loginReq.Username + ".password" - log.Printf("Attempting to fetch password for key: %s\n", passwordKey) // 增加调试日志 - password, err := h.rdb.HGet(ctx, userStorageKey, passwordKey).Result() - if err != nil { - log.Printf("Failed to find password for key: %s, error: %v\n", passwordKey, err) // 增加调试日志 - w.WriteHeader(http.StatusUnauthorized) - render.JSON(w, r, LoginResponse{Success: false, Message: "User not found"}) - return - } - - // verify password - if password != loginReq.Password { - log.Printf("Password mismatch: expected %s, got %s\n", password, loginReq.Password) // 增加调试日志 - w.WriteHeader(http.StatusUnauthorized) - render.JSON(w, r, LoginResponse{Success: false, Message: "Invalid password"}) - return - } - - render.JSON(w, r, LoginResponse{Success: true, Message: "Login successful"}) -} +package v1 + +import ( + "context" + "encoding/json" + "log" + "net/http" + + "github.com/go-chi/render" +) + +type LoginRequest struct { + Username string `json:"username"` + Password string `json:"password"` +} + +type LoginResponse struct { + Success bool `json:"success"` + Message string `json:"message"` +} + +func (h *Handler) HandleLogin(w http.ResponseWriter, r *http.Request) { + var loginReq LoginRequest + + // Decode JSON request body + if err := json.NewDecoder(r.Body).Decode(&loginReq); err != nil { + w.WriteHeader(http.StatusBadRequest) + render.JSON(w, r, LoginResponse{Success: false, Message: "Invalid request"}) + return + } + + // Redis hash key + userStorageKey := "user_storage" + + // get password + ctx := context.Background() + passwordKey := loginReq.Username + log.Printf("Attempting to fetch password for key: %s\n", passwordKey) + password, err := h.rdb.HGet(ctx, userStorageKey, passwordKey).Result() + if err != nil { + log.Printf("Failed to find password for key: %s, error: %v\n", passwordKey, err) + w.WriteHeader(http.StatusUnauthorized) + render.JSON(w, r, LoginResponse{Success: false, Message: "User not found"}) + return + } + + // verify password + if password != loginReq.Password { + log.Printf("Password mismatch: expected %s, got %s\n", password, loginReq.Password) + w.WriteHeader(http.StatusUnauthorized) + render.JSON(w, r, LoginResponse{Success: false, Message: "Invalid password"}) + return + } + + render.JSON(w, r, LoginResponse{Success: true, Message: "Login successful"}) +} diff --git a/server/daemon/redis.go b/server/daemon/redis.go index 0cbcced..a94d2a9 100644 --- a/server/daemon/redis.go +++ b/server/daemon/redis.go @@ -1,44 +1,39 @@ -package daemon - -import ( - "context" - "log" - - "github.com/redis/go-redis/v9" -) - -func clearRedis(rdb *redis.Client) { - ctx := context.Background() - if err := rdb.FlushDB(ctx).Err(); err != nil { - log.Printf("Failed to clear Redis database: %v\n", err) - } else { - log.Println("Redis database cleared") - } -} - -func initUserData(rdb *redis.Client) { - ctx := context.Background() - - // Redis hash - userStorageKey := "user_storage" - - // users_storage - users := map[string]map[string]string{ - "a": {"password": "aaa"}, - "b": {"password": "bbb"}, - "c": {"password": "ccc"}, - "d": {"password": "ddd"}, - } - - // add to Redis - for user, fields := range users { - for key, value := range fields { - fullKey := user + "." + key - if err := rdb.HSet(ctx, userStorageKey, fullKey, value).Err(); err != nil { - log.Printf("Failed to set user %s data: %v\n", user, err) - } else { - log.Printf("Set Redis key: %s, value: %s\n", fullKey, value) - } - } - } -} +package daemon + +import ( + "context" + "log" + + "github.com/redis/go-redis/v9" +) + +func clearRedis(rdb *redis.Client) { + ctx := context.Background() + if err := rdb.FlushDB(ctx).Err(); err != nil { + log.Printf("Failed to clear Redis database: %v\n", err) + } else { + log.Println("Redis database cleared") + } +} + +func initUserData(rdb *redis.Client) { + ctx := context.Background() + + // Redis hash + userStorageKey := "user_storage" + + // users storage with key as username and value as password + users := map[string]string{ + "a": "aaa", + "b": "bbb", + "c": "ccc", + "d": "ddd", + } + + // add to Redis + for user, password := range users { + if err := rdb.HSet(ctx, userStorageKey, user, password).Err(); err != nil { + log.Printf("Failed to set user %s data: %v\n", user, err) + } + } +} diff --git a/webapp/components/login.tsx b/webapp/components/login.tsx index 5a45e8f..e348013 100644 --- a/webapp/components/login.tsx +++ b/webapp/components/login.tsx @@ -1,69 +1,68 @@ -import { useState } from 'react' -import { useAtom } from 'jotai' -import { userIdAtom, userPasswordAtom, isLoggedInAtom } from '../store/atom' -import Join from '../components/join' -import { login } from '../lib/api' - -export default function Login() { - const [userId, setUserId] = useAtom(userIdAtom) - const [password, setPassword] = useAtom(userPasswordAtom) - const [isLoggedIn, setIsLoggedIn] = useAtom(isLoggedInAtom) - const [error, setError] = useState(null) - - const handleLogin = async () => { - if (userId === '' || password === '') { - setError('User ID and password cannot be empty') - return - } - - try { - const response = await login(userId, password) - if (response.success) { - setError(null) - setIsLoggedIn(true) - } else { - setError(response.message || 'Login failed') - } - } catch { - setError('Network error or server unavailable') - } - } - - if (isLoggedIn) { - return - } - - return ( -
-
- setUserId(e.target.value)} - /> - - setPassword(e.target.value)} - /> - - {error && ( -

- {error} -

- )} - - -
-
- ) -} +import { useState } from 'react' +import { useAtom } from 'jotai' +import { userIdAtom, userPasswordAtom, isLoggedInAtom } from '../store/atom' +import Join from '../components/join' +import { login } from '../lib/api' + +export default function Login() { + const [userId, setUserId] = useAtom(userIdAtom) + const [password, setPassword] = useAtom(userPasswordAtom) + const [isLoggedIn, setIsLoggedIn] = useAtom(isLoggedInAtom) + const [error, setError] = useState(null) + + const handleLogin = async () => { + if (userId === '' || password === '') { + setError('User ID and password cannot be empty') + return + } + try { + const response = await login(userId, password) + if (response.success) { + setError(null) + setIsLoggedIn(true) + } else { + setError(response.message || 'Login failed') + } + } catch { + setError('Network error or server unavailable') + } + } + + if (isLoggedIn) { + return + } + + return ( +
+
+ setUserId(e.target.value)} + /> + + setPassword(e.target.value)} + /> + + {error && ( +

+ {error} +

+ )} + + +
+
+ ) +} diff --git a/webapp/lib/api.ts b/webapp/lib/api.ts index 22ca6df..b358db0 100644 --- a/webapp/lib/api.ts +++ b/webapp/lib/api.ts @@ -118,19 +118,13 @@ async function delStream(roomId: string, streamId: string): Promise { } async function login(username: string, password: string): Promise<{ success: boolean; message: string }> { - const response = await fetch('/login/', { + return (await fetch('/login/', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ username, password }), - }) - - if (!response.ok) { - return { success: false, message: 'Login failed' } - } - - return response.json() + })).json() } export {