From 2c715c9ef48744199608867cd2c29718dd8210dd Mon Sep 17 00:00:00 2001 From: alphaeusmote <41258468-alphaeusmote@users.noreply.replit.com> Date: Wed, 9 Apr 2025 02:45:42 +0000 Subject: [PATCH] Fix: Redirect users to the dashboard after successful login Replit-Commit-Author: Agent Replit-Commit-Session-Id: 705f2157-ef97-4fbd-89e4-8c7f2ecaea90 Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/7ed01c5f-a82d-405a-b728-b2e3d127c60c/a1462f8b-9e4f-4acf-a0a4-a2ee5c1f4036.jpg --- client/src/pages/auth-page.tsx | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/client/src/pages/auth-page.tsx b/client/src/pages/auth-page.tsx index 070a7f1..e487d31 100644 --- a/client/src/pages/auth-page.tsx +++ b/client/src/pages/auth-page.tsx @@ -1,3 +1,4 @@ +import * as React from "react"; import { useLocation } from "wouter"; import { z } from "zod"; import { useForm } from "react-hook-form"; @@ -54,6 +55,13 @@ export default function AuthPage() { const { toast } = useToast(); const auth = useAuth(); + // Redirect if already logged in + React.useEffect(() => { + if (auth.user) { + navigate("/"); + } + }, [auth.user, navigate]); + // Login form const loginForm = useForm({ resolver: zodResolver(loginSchema), @@ -81,14 +89,28 @@ export default function AuthPage() { // Handle login form submission const onLoginSubmit = (data: LoginFormValues) => { const { username, password } = data; - auth.loginMutation.mutate({ username, password }); + auth.loginMutation.mutate( + { username, password }, + { + onSuccess: () => { + navigate("/"); + } + } + ); }; // Handle register form submission const onRegisterSubmit = (data: RegisterFormValues) => { // Remove confirmPassword and acceptTerms which aren't part of the API request const { confirmPassword, acceptTerms, ...registerData } = data; - auth.registerMutation.mutate(registerData); + auth.registerMutation.mutate( + registerData, + { + onSuccess: () => { + navigate("/"); + } + } + ); }; return (