diff --git a/src/app/context/AuthContext.tsx b/src/app/context/AuthContext.tsx index b686e4d..2f8cda4 100644 --- a/src/app/context/AuthContext.tsx +++ b/src/app/context/AuthContext.tsx @@ -83,7 +83,7 @@ interface AuthState { refreshToken: string | null; isLoading: boolean; login: (email: string, password: string) => Promise<{ success: boolean; error?: string }>; - signup: (email: string, password: string, fullName: string, role: string) => Promise<{ success: boolean; error?: string }>; + signup: (email: string, password: string, fullName: string, role: string) => Promise<{ success: boolean; error?: string; confirmationRequired?: boolean }>; loginWithGoogle: () => Promise<{ success: boolean; error?: string }>; logout: () => void; fetchWithAuth: (url: string, options?: RequestInit) => Promise; @@ -308,14 +308,21 @@ export function AuthProvider({ children }: { children: ReactNode }) { } const { tokens, user: userProfile } = json.data; - setToken(tokens.access_token); - setRefreshToken(tokens.refresh_token); - setUser(userProfile); - if (typeof window !== "undefined") { - localStorage.setItem("ozymorlab_token", tokens.access_token); - localStorage.setItem("ozymorlab_refresh_token", tokens.refresh_token); + if (tokens && userProfile) { + setToken(tokens.access_token); + setRefreshToken(tokens.refresh_token); + setUser(userProfile); + if (typeof window !== "undefined") { + localStorage.setItem("ozymorlab_token", tokens.access_token); + localStorage.setItem("ozymorlab_refresh_token", tokens.refresh_token); + } + return { success: true }; } - return { success: true }; + + if (json.data?.user) { + setUser(json.data.user); + } + return { success: true, confirmationRequired: true }; } catch (err: any) { return { success: false, error: err.message || "Failed to register local account" }; } @@ -367,8 +374,10 @@ export function AuthProvider({ children }: { children: ReactNode }) { is_active: true, }); } + return { success: true }; } - return { success: true }; + + return { success: true, confirmationRequired: true }; } catch { return { success: false, error: "Network error. Is Supabase configured correctly?" }; } diff --git a/src/app/login/page.tsx b/src/app/login/page.tsx index 4d95972..cf31131 100644 --- a/src/app/login/page.tsx +++ b/src/app/login/page.tsx @@ -1,19 +1,16 @@ "use client"; -import { useState, useEffect, Suspense } from "react"; -import { useRouter, useSearchParams } from "next/navigation"; +import { useState, useEffect } from "react"; +import { useRouter } from "next/navigation"; import Link from "next/link"; import { Eye, EyeOff, ArrowRight, AlertCircle, CheckCircle2, Sparkles } from "lucide-react"; import { AuthProvider, useAuth } from "../context/AuthContext"; function LoginPageContent() { const router = useRouter(); - const searchParams = useSearchParams(); - const { login, signup, loginWithGoogle, user, isLoading } = useAuth(); + const { login, signup, loginWithGoogle, logout, user, isLoading } = useAuth(); - const [activeTab, setActiveTab] = useState<"login" | "signup">( - searchParams.get("tab") === "signup" ? "signup" : "login" - ); + const [activeTab, setActiveTab] = useState<"login" | "signup">("login"); const [email, setEmail] = useState(""); const [password, setPassword] = useState(""); const [fullName, setFullName] = useState(""); @@ -23,12 +20,42 @@ function LoginPageContent() { const [loading, setLoading] = useState(false); const [success, setSuccess] = useState(""); - // Redirect if already logged in useEffect(() => { - if (!isLoading && user) { - router.push("/dashboard"); + if (typeof window !== "undefined") { + const params = new URLSearchParams(window.location.search); + if (params.get("tab") === "signup") { + setActiveTab("signup"); + } } - }, [user, isLoading, router]); + }, []); + + // If already logged in, show a small informative UI instead of auto-redirecting. + // This prevents the back button from immediately bouncing the user back to dashboard. + if (!isLoading && user) { + return ( +
+
+
+
+
Oz
+ OzymorLab AIOS +
+

Welcome back

+

You are already signed in as {user.email}. Use the buttons to continue.

+
+ + +
+
+
+
+
+

If you want to use a different account, sign out first.

+
+
+
+ ); + } const handleLogin = async (e: React.FormEvent) => { e.preventDefault(); @@ -56,8 +83,12 @@ function LoginPageContent() { const result = await signup(email, password, fullName, role); if (result.success) { - setSuccess("Account created! Redirecting..."); - setTimeout(() => router.push("/dashboard"), 800); + if (result.confirmationRequired) { + setSuccess("Registration successful. Check your email and confirm your address before signing in."); + } else { + setSuccess("Account created! Redirecting..."); + setTimeout(() => router.push("/dashboard"), 800); + } } else { setError(result.error || "Signup failed"); } @@ -363,9 +394,7 @@ function LoginPageContent() { export default function LoginPage() { return ( -
}> - -
+
); } diff --git a/test-results/.last-run.json b/test-results/.last-run.json index 957284b..5fca3f8 100644 --- a/test-results/.last-run.json +++ b/test-results/.last-run.json @@ -1,6 +1,4 @@ { "status": "failed", - "failedTests": [ - "70b872a5e72a7b2c7282-d4de8269ab84cb7dd8a5" - ] + "failedTests": [] } \ No newline at end of file