From 4cc3387e97a996e2beefab4eda0fbf5f5602758c Mon Sep 17 00:00:00 2001 From: wood chen Date: Sun, 23 Feb 2025 04:55:56 +0800 Subject: [PATCH] Remove development-mode test login functionality - Strip out hardcoded test user creation and session generation - Prepare for OAuth 2.0 authentication in production environment - Simplify login handler to focus on OAuth authentication --- backend/handlers/auth.go | 40 ---------------------------------------- 1 file changed, 40 deletions(-) diff --git a/backend/handlers/auth.go b/backend/handlers/auth.go index be49c08..a31c9b0 100644 --- a/backend/handlers/auth.go +++ b/backend/handlers/auth.go @@ -56,46 +56,6 @@ func GetAuthStatus(c *gin.Context) { } func Login(c *gin.Context) { - // 开发环境下使用测试账号 - if gin.Mode() != gin.ReleaseMode { - db := c.MustGet("db").(*sql.DB) - - // 创建测试用户(如果不存在) - var count int - err := db.QueryRow("SELECT COUNT(*) FROM user WHERE username = 'admin'").Scan(&count) - if err != nil || count == 0 { - _, err = db.Exec("INSERT INTO user (username, email, role) VALUES (?, ?, ?)", - "admin", "admin@test.com", "admin") - if err != nil { - c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create test user"}) - return - } - } - - // 获取用户ID - var userID uint - err = db.QueryRow("SELECT id FROM user WHERE username = 'admin'").Scan(&userID) - if err != nil { - c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to get user"}) - return - } - - // 创建会话 - sessionID := generateSessionID() - expiresAt := time.Now().Add(24 * time.Hour) - _, err = db.Exec("INSERT INTO session (id, user_id, expires_at) VALUES (?, ?, ?)", - sessionID, userID, expiresAt) - if err != nil { - c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create session"}) - return - } - - // 设置cookie - c.SetCookie("session", sessionID, int(24*time.Hour.Seconds()), "/", "aimodels-prices.q58.club", true, true) - c.JSON(http.StatusOK, gin.H{"message": "Logged in successfully"}) - return - } - // 生产环境使用 OAuth 2.0 clientID := os.Getenv("OAUTH_CLIENT_ID") redirectURI := os.Getenv("OAUTH_REDIRECT_URI")