How do I update the recovery password on the SSR ?

I manage user sessions on the server side using Next.js, but I don't know how to handle user password recovery on the server side, and I don't want to create a Supabase instance on the client side for this. What do you recommend ?
31 Replies
Ali Lee
Ali Lee3w ago
You can do that with supabase server instance.
Asperiuns
AsperiunsOP3w ago
I don't know how to do it. Do you have any resources ?
Ali Lee
Ali Lee3w ago
you can call this for example:
supabase.auth.resetPasswordForEmail(email, {redirectTo})
supabase.auth.resetPasswordForEmail(email, {redirectTo})
here, supabase is server instance and you can send email and rediret url to the reset api endpoint also, you need to add that url to supabase project url part
Asperiuns
AsperiunsOP3w ago
Yes, I did that, but I created an instance on the CSR side using the Supabase documentation:
/** * Step 1: Send an email to the user to obtain a password reset token. * This email contains a link that redirects the user back to your application. */

const { data, error } = await supabase.auth .resetPasswordForEmail(‘user@email.com’)

useEffect(() => {
supabase.auth.onAuthStateChange(
async (event, session) => {
if (event ==PASSWORD_RECOVERY”) {
const newPassword = prompt(“What would you like your new password to be?”);
const { data, error } = await supabase.auth.updateUser({ password: newPassword })

if (data) alert(“Password successfully updated!”)
if (error) alert(“An error occurred while updating your password.”)
}
}
)
}, [])
/** * Step 1: Send an email to the user to obtain a password reset token. * This email contains a link that redirects the user back to your application. */

const { data, error } = await supabase.auth .resetPasswordForEmail(‘user@email.com’)

useEffect(() => {
supabase.auth.onAuthStateChange(
async (event, session) => {
if (event ==PASSWORD_RECOVERY”) {
const newPassword = prompt(“What would you like your new password to be?”);
const { data, error } = await supabase.auth.updateUser({ password: newPassword })

if (data) alert(“Password successfully updated!”)
if (error) alert(“An error occurred while updating your password.”)
}
}
)
}, [])
silentworks
silentworks3w ago
All of the normal auth flows can be found in this example repo that I created https://github.com/silentworks/supabase-by-example/tree/main/nextjs
GitHub
supabase-by-example/nextjs at main · silentworks/supabase-by-example
Contribute to silentworks/supabase-by-example development by creating an account on GitHub.
Ali Lee
Ali Lee3w ago
you did this in useAuth?
Ali Lee
Ali Lee3w ago
No description
Ali Lee
Ali Lee3w ago
you need to build api for reset and call it 🙂
Asperiuns
AsperiunsOP3w ago
I can do that, but I don't know how to perform the password update process on the server side after email forwarding.
Ali Lee
Ali Lee3w ago
this will send email to user when user click that link, the user will be redirected to reset-password page here, user will type password and confirm you will reset password with this new password
Asperiuns
AsperiunsOP3w ago
Okay, after that part, I don't know how to update the password. How do I do this on the server side ? I only need the update section on the SSR
Ali Lee
Ali Lee3w ago
you need new page for reset password
Ali Lee
Ali Lee3w ago
No description
Ali Lee
Ali Lee3w ago
when user click forgot password he will redirected to this page
Ali Lee
Ali Lee3w ago
No description
Ali Lee
Ali Lee3w ago
after he types email and click button he will receive email if he click the link in the email he will redirected to this one
Ali Lee
Ali Lee3w ago
No description
Ali Lee
Ali Lee3w ago
here user will type new password you need call new api or just can use supabase instance for updating password
supabase.auth.updateUser({ password });
supabase.auth.updateUser({ password });
if you want do this in server side, you need to create new api and call it or you can force reset password using admin (service role key)
Asperiuns
AsperiunsOP3w ago
How can I do this with a server action, for example ?
Ali Lee
Ali Lee3w ago
like this
Ali Lee
Ali Lee3w ago
No description
Ali Lee
Ali Lee3w ago
did you create these 2 things in your project?
Asperiuns
AsperiunsOP3w ago
This only sends an e-mail. If the user clicks on the link, it directs them to a page. How do I know that the user is in recovery mode and will only update their e-mail ? and I don't have client side supabase instance just I have server side supabase instance I want to manage users only on the server action side
Ali Lee
Ali Lee3w ago
this is for sending email with reset link this is for updating password with new one user typed build new api like this for updating password understood?
Asperiuns
AsperiunsOP3w ago
What I don't understand is how it knows who the user is who clicked on the Supabase link. This situation is confusing me. No 😄
Ali Lee
Ali Lee3w ago
supabase send token with url like this: your.domain.com/reset-password&code=~~~~~
Ali Lee
Ali Lee3w ago
No description
Ali Lee
Ali Lee3w ago
you need to handle this part
Asperiuns
AsperiunsOP3w ago
okey I understand but I wonder, Does this token only appear on the CSR side instance, or does it also appear on the SSR side Supabase instance ?
Ali Lee
Ali Lee3w ago
available for both
Asperiuns
AsperiunsOP3w ago
I think I understand now, when the user clicks on the link in the email, a temporary session is opened with the token generated for the address in the link and Supabase updates the password through this temporary session, thus understanding which email the link was clicked from.

Did you find this page helpful?