package routes import ( "fmt" "strings" "dk/internal/auth" "dk/internal/csrf" "dk/internal/database" "dk/internal/middleware" "dk/internal/password" "dk/internal/router" "dk/internal/template" "dk/internal/template/components" "dk/internal/users" "github.com/valyala/fasthttp" ) // RegisterAuthRoutes sets up authentication routes func RegisterAuthRoutes(r *router.Router) { // Guest routes guestGroup := r.Group("") guestGroup.Use(middleware.RequireGuest("/")) guestGroup.Get("/login", showLogin()) guestGroup.Post("/login", processLogin()) guestGroup.Get("/register", showRegister()) guestGroup.Post("/register", processRegister()) // Authenticated routes authGroup := r.Group("") authGroup.Use(middleware.RequireAuth("/login")) authGroup.Post("/logout", processLogout()) } // showLogin displays the login form func showLogin() router.Handler { return func(ctx router.Ctx, params []string) { loginTmpl, err := template.Cache.Load("auth/login.html") if err != nil { ctx.SetStatusCode(fasthttp.StatusInternalServerError) fmt.Fprintf(ctx, "Template error: %v", err) return } loginFormData := map[string]any{ "csrf_token": csrf.GetToken(ctx, auth.Manager), "csrf_field": csrf.HiddenField(ctx, auth.Manager), "error_message": "", } loginContent := loginTmpl.RenderNamed(loginFormData) pageData := components.NewPageData("Login - Dragon Knight", loginContent) if err := components.RenderPage(ctx, pageData, nil); err != nil { ctx.SetStatusCode(fasthttp.StatusInternalServerError) fmt.Fprintf(ctx, "Template error: %v", err) return } } } // processLogin handles login form submission func processLogin() router.Handler { return func(ctx router.Ctx, params []string) { // Validate CSRF token if !csrf.ValidateFormToken(ctx, auth.Manager) { ctx.SetStatusCode(fasthttp.StatusForbidden) ctx.WriteString("CSRF validation failed") return } // Get form values email := strings.TrimSpace(string(ctx.PostArgs().Peek("email"))) userPassword := string(ctx.PostArgs().Peek("password")) // Validate input if email == "" || userPassword == "" { showLoginError(ctx, "Email and password are required") return } // Authenticate user user, err := auth.Manager.Authenticate(email, userPassword) if err != nil { showLoginError(ctx, "Invalid email or password") return } // Create session and login middleware.Login(ctx, auth.Manager, user) // Transfer CSRF token from cookie to session for authenticated user if cookieToken := csrf.GetTokenFromCookie(ctx); cookieToken != "" { if session := csrf.GetCurrentSession(ctx); session != nil { csrf.StoreToken(session, cookieToken) } } // Redirect to dashboard ctx.Redirect("/dashboard", fasthttp.StatusFound) } } // showRegister displays the registration form func showRegister() router.Handler { return func(ctx router.Ctx, params []string) { registerTmpl, err := template.Cache.Load("auth/register.html") if err != nil { ctx.SetStatusCode(fasthttp.StatusInternalServerError) fmt.Fprintf(ctx, "Template error: %v", err) return } registerFormData := map[string]any{ "csrf_token": csrf.GetToken(ctx, auth.Manager), "csrf_field": csrf.HiddenField(ctx, auth.Manager), "error_message": "", "username": "", "email": "", } registerContent := registerTmpl.RenderNamed(registerFormData) pageData := components.NewPageData("Register - Dragon Knight", registerContent) if err := components.RenderPage(ctx, pageData, nil); err != nil { ctx.SetStatusCode(fasthttp.StatusInternalServerError) fmt.Fprintf(ctx, "Template error: %v", err) return } } } // processRegister handles registration form submission func processRegister() router.Handler { return func(ctx router.Ctx, params []string) { // Validate CSRF token if !csrf.ValidateFormToken(ctx, auth.Manager) { ctx.SetStatusCode(fasthttp.StatusForbidden) ctx.WriteString("CSRF validation failed") return } // Get form values username := strings.TrimSpace(string(ctx.PostArgs().Peek("username"))) email := strings.TrimSpace(string(ctx.PostArgs().Peek("email"))) userPassword := string(ctx.PostArgs().Peek("password")) confirmPassword := string(ctx.PostArgs().Peek("confirm_password")) // Validate input if err := validateRegistration(username, email, userPassword, confirmPassword); err != nil { showRegisterError(ctx, err.Error(), username, email) return } // Check if username already exists if _, err := users.GetByUsername(username); err == nil { showRegisterError(ctx, "Username already exists", username, email) return } // Check if email already exists if _, err := users.GetByEmail(email); err == nil { showRegisterError(ctx, "Email already registered", username, email) return } // Hash password hashedPassword, err := password.Hash(userPassword) if err != nil { showRegisterError(ctx, "Failed to process password", username, email) return } // Create user (this is a simplified approach - in a real app you'd use a proper user creation function) user := &users.User{ Username: username, Email: email, Password: hashedPassword, Verified: 1, // Auto-verify for now Auth: 1, // Enabled } // Insert into database if err := createUser(user); err != nil { showRegisterError(ctx, "Failed to create account", username, email) return } // Auto-login after registration authUser := &auth.User{ ID: user.ID, Username: user.Username, Email: user.Email, } middleware.Login(ctx, auth.Manager, authUser) // Transfer CSRF token from cookie to session for authenticated user if cookieToken := csrf.GetTokenFromCookie(ctx); cookieToken != "" { if session := csrf.GetCurrentSession(ctx); session != nil { csrf.StoreToken(session, cookieToken) } } ctx.Redirect("/", fasthttp.StatusFound) } } // processLogout handles logout func processLogout() router.Handler { return func(ctx router.Ctx, params []string) { // Validate CSRF token if !csrf.ValidateFormToken(ctx, auth.Manager) { ctx.SetStatusCode(fasthttp.StatusForbidden) ctx.WriteString("CSRF validation failed") return } middleware.Logout(ctx, auth.Manager) ctx.Redirect("/", fasthttp.StatusFound) } } // Helper functions func showLoginError(ctx router.Ctx, errorMsg string) { loginTmpl, err := template.Cache.Load("auth/login.html") if err != nil { ctx.SetStatusCode(fasthttp.StatusInternalServerError) fmt.Fprintf(ctx, "Template error: %v", err) return } var errorHTML string if errorMsg != "" { errorHTML = fmt.Sprintf(`