diff --git a/src/app/login/page.tsx b/src/app/login/page.tsx index 27a79e9..b0273d2 100644 --- a/src/app/login/page.tsx +++ b/src/app/login/page.tsx @@ -18,19 +18,41 @@ export default function LoginPage() { setLoading(true); setError(''); - const result = await signIn('credentials', { - email, - password, - redirect: false, - }); + try { + // First, validate credentials with backend to get proper error messages + const validateRes = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/auth/login`, { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ email, password }), + }); - setLoading(false); + const validateData = await validateRes.json(); - if (result?.error) { - setError('Invalid email or password'); - } else { - router.push('/'); - router.refresh(); + if (!validateRes.ok) { + // Show the actual error message from backend + setError(validateData.message || 'Invalid email or password'); + setLoading(false); + return; + } + + // If validation passed, use NextAuth to create session + const result = await signIn('credentials', { + email, + password, + redirect: false, + }); + + setLoading(false); + + if (result?.error) { + setError('Login failed. Please try again.'); + } else { + router.push('/'); + router.refresh(); + } + } catch (err) { + setLoading(false); + setError('An error occurred. Please try again.'); } }; diff --git a/src/app/signup/page.tsx b/src/app/signup/page.tsx index 69b2760..3974934 100644 --- a/src/app/signup/page.tsx +++ b/src/app/signup/page.tsx @@ -1,7 +1,6 @@ 'use client'; import { useState } from 'react'; -import { useRouter } from 'next/navigation'; import { signIn } from 'next-auth/react'; import Link from 'next/link'; import { authService, AuthService, RegisterRequest } from '@/services'; @@ -14,7 +13,6 @@ interface ValidationError { } export default function SignUpPage() { - const router = useRouter(); const [userType, setUserType] = useState('USER'); const [firstName, setFirstName] = useState(''); const [lastName, setLastName] = useState(''); @@ -24,6 +22,7 @@ export default function SignUpPage() { const [error, setError] = useState(''); const [fieldErrors, setFieldErrors] = useState([]); const [loading, setLoading] = useState(false); + const [registrationSuccess, setRegistrationSuccess] = useState(false); const getFieldError = (fieldName: string): string | null => { const fieldError = fieldErrors.find((e) => e.field === fieldName); @@ -47,19 +46,8 @@ export default function SignUpPage() { await authService.register(registerData); - // Auto login after successful registration - const result = await signIn('credentials', { - email, - password, - redirect: false, - }); - - if (result?.error) { - router.push('/login'); - } else { - router.push('/'); - router.refresh(); - } + // Show success message - user must verify email before logging in + setRegistrationSuccess(true); } catch (err) { // Get field errors const errors = AuthService.getFieldErrors(err); @@ -90,15 +78,41 @@ export default function SignUpPage() { /> - {/* Sign Up Heading */} -
-

Sign Up

-

- Sign in or create an account to showcase your skills & expertise to millions of people starting their real estate journey today" -

-
+ {/* Registration Success Message */} + {registrationSuccess ? ( +
+
+
+ + + +
+

Check Your Email

+

+ We've sent a verification link to {email}. Please check your inbox and click the link to verify your account before logging in. +

+ + Go to Login + +

+ Didn't receive the email? Check your spam folder or contact support. +

+
+
+ ) : ( + <> + {/* Sign Up Heading */} +
+

Sign Up

+

+ Sign in or create an account to showcase your skills & expertise to millions of people starting their real estate journey today" +

+
- {/* User Type Toggle */} + {/* User Type Toggle */}
+ + )}
); diff --git a/src/auth.ts b/src/auth.ts index 7e469dc..02f89a1 100644 --- a/src/auth.ts +++ b/src/auth.ts @@ -25,37 +25,33 @@ export const { handlers, signIn, signOut, auth } = NextAuth({ password: { label: "Password", type: "password" }, }, async authorize(credentials) { - try { - const res = await fetch( - `${process.env.NEXT_PUBLIC_API_URL}/auth/login`, - { - method: "POST", - headers: { "Content-Type": "application/json" }, - body: JSON.stringify({ - email: credentials?.email, - password: credentials?.password, - }), - } - ); - - const data = await res.json(); - - if (res.ok && data.success) { - return { - id: data.data.user.id, - email: data.data.user.email, - name: `${data.data.user.firstName || ""} ${data.data.user.lastName || ""}`.trim(), - role: data.data.user.role, - accessToken: data.data.accessToken, - refreshToken: data.data.refreshToken, - }; + const res = await fetch( + `${process.env.NEXT_PUBLIC_API_URL}/auth/login`, + { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ + email: credentials?.email, + password: credentials?.password, + }), } + ); - return null; - } catch (error) { - console.error("Auth error:", error); - return null; + const data = await res.json(); + + if (res.ok && data.success) { + return { + id: data.data.user.id, + email: data.data.user.email, + name: `${data.data.user.firstName || ""} ${data.data.user.lastName || ""}`.trim(), + role: data.data.user.role, + accessToken: data.data.accessToken, + refreshToken: data.data.refreshToken, + }; } + + // Throw the actual error message from the backend + throw new Error(data.message || "Invalid email or password"); }, }), ],