@@ -5,37 +5,36 @@ import { headers } from "next/headers";
55
66export const resetUserPassword = async ( email : string ) => {
77 try {
8-
8+ const normalizedEmail = email . toLowerCase ( ) . trim ( ) ;
99 const supabase = await createClientServerComponent ( ) ;
1010 const headersList = await headers ( ) ;
11- const origin = headersList . get ( "origin" ) || process . env . NEXT_PUBLIC_APP_BASE_URL ;
11+ const origin = headersList . get ( "origin" ) || process . env . NEXT_PUBLIC_APP_BASE_URL || "http://localhost:3000" ;
1212
13- // check the user of the email
13+ // Check user record by email
1414 const { data, error : userError } = await supabase
1515 . from ( "codev" )
16- . select ( )
17- . eq ( "email_address" , email )
18- . single ( ) ;
16+ . select ( "id, application_status" )
17+ . eq ( "email_address" , normalizedEmail )
18+ . maybeSingle ( ) ;
1919
2020 if ( userError ) {
2121 console . error ( "Database query error:" , userError ) ;
2222 throw new Error ( `Database error: ${ userError . message } ` ) ;
2323 }
2424
2525 if ( ! data ) {
26- console . error ( "User not found for email:" , email ) ;
27- throw new Error ( "User not found" ) ;
26+ console . error ( "User not found for email:" , normalizedEmail ) ;
27+ throw new Error ( "No account found with this email address " ) ;
2828 }
2929
30- const url = data . availability_status === "passed"
30+ // Determine redirect destination after magic link callback
31+ const isPassed = data . application_status === "passed" ;
32+ const url = isPassed
3133 ? `${ origin } /auth/callback?redirect_to=/home/account-settings`
32- : `${ origin } /auth/callback?redirect_to=/applicant/account-settings ` ;
34+ : `${ origin } /auth/callback?redirect_to=/applicant/waiting ` ;
3335
34- /*
35- * Send password reset email using Supabase
36- * The redirectTo URL is where the user will be redirected after clicking the link in the email
37- */
38- const { error } = await supabase . auth . resetPasswordForEmail ( email , {
36+ // Send password reset email using Supabase Auth
37+ const { error } = await supabase . auth . resetPasswordForEmail ( normalizedEmail , {
3938 redirectTo : url ,
4039 } ) ;
4140
@@ -44,8 +43,9 @@ export const resetUserPassword = async (email: string) => {
4443 throw new Error ( error . message || "Failed to send reset email" ) ;
4544 }
4645
47- } catch ( error ) {
46+ return { success : true } ;
47+ } catch ( error : any ) {
4848 console . error ( "Error resetting password:" , error ) ;
49- throw new Error ( ` Failed to reset password: ${ error instanceof Error ? error . message : 'Unknown error' } ` ) ;
49+ throw new Error ( error . message || " Failed to reset password" ) ;
5050 }
5151} ;
0 commit comments