I'm currently utilizing NextJs and Supabase to handle the process of resetting a user's password.
Below is the server action in which I invoke the resetPasswordForEmail function to send an email to the client's mailbox:
export const resetPasswordWithEmail = async (email: string) => {
'use server';
const foundUser = await getUserByEmail(email);
if (!foundUser) {
return {
error: [
{
id: '1',
message: 'This email does not exist in our database',
},
],
};
}
const { error} = await supabaseClient.auth.resetPasswordForEmail(email);
if (error) {
return {
error: [
{
id: '1',
message: error.message,
},
],
};
}
return {
success: 'An email has been successfully sent to your mailbox'
};
}
This is the email template provided by Supabase:
<a href="{{.ConfirmationURL}}/api/auth/callback?next=/auth/reset-password" style="display: inline-block; padding: 10px 20px; background-color: #FDAC04; color: #000000; text-decoration: none; border-radius: 5px;">Click here to reset your password</a>
The resent link contains the API from my Next.js and Supabase, automatically adding two parameters, 'code' & 'hash_token'. I pass the 'code' parameter to the supabase function supabase.auth.exchangeCodeForSession for validation, redirecting from the API to the reset-password page.
Here is the code snippet:
export async function GET(request: Request) {
// The `/auth/callback` route is required for the server-side auth flow implemented
// by the Auth Helpers package. It exchanges an auth code for the user's session.
// https://supabase.com/docs/guides/auth/auth-helpers/nextjs#managing-sign-in-with-code-exchange
const requestUrl = new URL(request.url);
const code = requestUrl.searchParams.get('code');
const next = requestUrl.searchParams.get('next');
if (code) {
const cookieStore = cookies();
const supabase = createClientServer(cookieStore);
await supabase.auth.exchangeCodeForSession(code);
}
// URL to redirect to after sign in process completes
if (next) return NextResponse.redirect(next);
else return NextResponse.redirect(requestUrl.origin);
}
This exchangeCodeForSession function is throwing an error:
Error: 'AuthApiError: invalid request: both auth code and code verifier should be non-empty'
Any insights on this issue?