Skip to content

Commit 79f161b

Browse files
Baroshemlarbish
andauthored
docs(get-started): add reset password section (#499)
Co-authored-by: Baptiste Leproux <[email protected]>
1 parent b7c55ea commit 79f161b

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed

docs/content/1.getting-started/2.authentication.md

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,94 @@ watch(user, () => {
107107
::tip
108108
If you want to manually set the redirect path, you can do so by disabling [`saveRedirectToCookie`](/getting-started/introduction#redirectoptions), and then set the value using the [`useSupabaseCookieRedirect`](/composables/usesupabasecookieredirect) composable directly.
109109
::
110+
111+
## Reset Password
112+
113+
You can easily handle password reset request for the user. The flow consist of 2 steps:
114+
115+
::steps
116+
117+
#### Allow the user to login via the password reset link. When the user clicks the reset link in the email they are redirected back to your application.
118+
119+
```vue [pages/password/reset.vue]
120+
<script setup lang="ts">
121+
const supabase = useSupabaseClient()
122+
const email = ref('')
123+
124+
const requestResetPassword = async () => {
125+
const { data, error } = await supabase.auth.resetPasswordForEmail(email.value, {
126+
redirectTo: 'https://example.com/password/update',
127+
})
128+
if (error) console.log(error)
129+
}
130+
</script>
131+
132+
<template>
133+
<div>
134+
<input
135+
v-model="email"
136+
type="email"
137+
/>
138+
<button @click="requestResetPassword">
139+
Reset Password
140+
</button>
141+
</div>
142+
</template>
143+
```
144+
145+
::tip
146+
You can configure the URL that the user is redirected to with the `redirectTo` parameter.
147+
::
148+
149+
#### After the user has been redirected successfully, prompt them for a new password and call `updateUser()`
150+
151+
```vue [pages/password/update.vue]
152+
<script setup lang="ts">
153+
const supabase = useSupabaseClient()
154+
const newPassword = ref('')
155+
156+
const updateUserPassword = async () => {
157+
const { data, error } = await supabase.auth.updateUser({
158+
password: newPassword.value
159+
})
160+
if (error) console.log(error)
161+
}
162+
</script>
163+
164+
<template>
165+
<div>
166+
<input
167+
v-model="newPassword"
168+
type="password"
169+
/>
170+
<button @click="updateUserPassword">
171+
Update Password
172+
</button>
173+
</div>
174+
</template>
175+
```
176+
177+
::
178+
179+
You can also watch for the event `PASSWORD_RECOVERY` that will be emitted when the password recovery link is clicked. You can use `onAuthStateChange()` to listen and invoke a callback function on these events.
180+
181+
```vue [pages/password/update.vue]
182+
<script setup lang="ts">
183+
const supabase = useSupabaseClient()
184+
const password = generateRandomPassword()
185+
186+
watch(newPassword, () => {
187+
supabase.auth.onAuthStateChange(async (event, session) => {
188+
if (event == "PASSWORD_RECOVERY") {
189+
const { data, error } = await supabase.auth
190+
.updateUser({ password })
191+
192+
if (data) alert("Password updated successfully!")
193+
if (error) alert("There was an error updating your password.")
194+
}
195+
})
196+
</script>
197+
```
198+
199+
If you want to learn more about it, you can read this [section](https://supabase.com/docs/reference/javascript/auth-resetpasswordforemail).
200+

0 commit comments

Comments
 (0)