Skip to content

Commit 9569bf3

Browse files
authored
docs: add reset password one-time token guide (#1445)
1 parent d0652bd commit 9569bf3

2 files changed

Lines changed: 59 additions & 3 deletions

File tree

docs/end-user-flows/one-time-token.mdx

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Logto supports the following scenarios with magic links:
1818
- **Invitation-only registration**: For internal tools or AI products in testing phase, you can disable public registration and invite specific users via magic links.
1919
- **Organization member invitation**: For SaaS products, use magic links to invite new members to join an organization, streamlining the onboarding process.
2020
- **Sign-in / Sign-up**: Send a magic link for passwordless sign-in or sign-up via email.
21+
- **Password reset**: Send a reset-password magic link from your own application so the user can verify the one-time token and set a new password in Logto.
2122

2223
For example, when you've disabled public registration, you can send a magic link with a one-time token (e.g., `https://yourapp.com/landing-page?token=YHwbXSXxQfL02IoxFqr1hGvkB13uTqcd&email=user@example.com`) to the user's email to invite them to complete account creation. You can customize the email template in your own email delivery service, such as:
2324

@@ -28,7 +29,6 @@ For example, when you've disabled public registration, you can send a magic link
2829

2930
Currently not supported:
3031

31-
- Password reset with magic link.
3232
- Using phone number or username as the identifier.
3333

3434
## One-time token flow \{#one-time-token-flow}
@@ -80,10 +80,24 @@ Sample request body payload:
8080
}
8181
```
8282

83+
For a password reset magic link, restrict the token to the forgot password flow:
84+
85+
```json
86+
{
87+
"email": "user@example.com",
88+
"expiresIn": 3600,
89+
"context": {
90+
"interactionEvent": "ForgotPassword"
91+
}
92+
}
93+
```
94+
8395
### Step 2: Compose your magic link \{#step-2-compose-your-magic-link}
8496

8597
After you get the one-time token, you can compose a magic link and send it to the end user's email address.
86-
The magic link should at least contain the token and the user email as parameters, and should navigate to a landing page in your own application.
98+
For sign-in or sign-up magic links, the magic link should at least contain the token and the user email as parameters.
99+
For password reset magic links, the email parameter is optional; if you omit it, Logto will ask the user to enter their email address before verifying the one-time token.
100+
The magic link should navigate to a landing page in your own application.
87101
E.g. `https://yourapp.com/landing-page`.
88102

89103
Here's a simple example of what the magic link may look like:
@@ -102,6 +116,8 @@ as well as encoding all the URL parameters.
102116

103117
### Step 3: Trigger the authentication flow via Logto SDK \{#step-3-trigger-the-authentication-flow-via-logto-sdk}
104118

119+
#### Sign-in or sign-up \{#sign-in-or-sign-up}
120+
105121
After the end user clicks the magic link and navigated to your application, you can extract the `token` and `email` parameters from the URL, and then call the `signIn()` function from Logto SDK to trigger the auth flow.
106122

107123
```typescript title="TokenLandingPage.tsx"
@@ -138,6 +154,42 @@ const TokenLandingPage = () => {
138154
};
139155
```
140156

157+
#### Reset password \{#reset-password}
158+
159+
For password reset magic links, start an authentication request with `first_screen` set to `reset_password`.
160+
Pass the one-time token through `one_time_token`.
161+
If you already have the user's email from your landing page, pass it through `login_hint`; otherwise, omit `login_hint` and Logto will ask the user to enter their email address before token verification.
162+
163+
```typescript title="ResetPasswordTokenLandingPage.tsx"
164+
// React example
165+
import { useLogto } from '@logto/react';
166+
import { useEffect } from 'react';
167+
import { useSearchParams } from 'react-router-dom';
168+
169+
const ResetPasswordTokenLandingPage = () => {
170+
const { signIn } = useLogto();
171+
const [searchParams] = useSearchParams();
172+
173+
useEffect(() => {
174+
const oneTimeToken = searchParams.get('token');
175+
const email = searchParams.get('email');
176+
177+
if (oneTimeToken) {
178+
signIn({
179+
redirectUri: 'https://yourapp.com/callback',
180+
extraParams: {
181+
'one_time_token': oneTimeToken,
182+
'first_screen': 'reset_password',
183+
...(email && { 'login_hint': email }),
184+
},
185+
});
186+
}
187+
}, [searchParams, signIn]);
188+
189+
return <>Please wait...</>;
190+
};
191+
```
192+
141193
:::warning
142194

143195
If a user is already signed-in, calling the`signIn()` function from SDK will automatically clear all cached tokens (ID token, access token, and refresh token) from client storage,

docs/end-user-flows/sign-up-and-sign-in/reset-password.mdx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ You can implement your own password reset flow by using the Logto's **Management
9595

9696
</summary>
9797

98-
You can create a self-hosted password reset endpoint and utilize the Logto SDK to initiate a sign-in request with [`first_screen`](/end-user-flows/authentication-parameters/first-screen) set to `reset-password`. This will seamlessly redirect the user to the password reset page.
98+
You can create a self-hosted password reset endpoint and use the Logto SDK to initiate a sign-in request with [`first_screen`](/end-user-flows/authentication-parameters/first-screen) set to `reset_password`. This redirects the user to the password reset page.
99+
100+
To verify the user with a magic link, create a [one-time token](/end-user-flows/one-time-token) for the user's email and pass it through the `one_time_token` authentication parameter. If you include `login_hint`, Logto will verify the token immediately; otherwise, Logto will ask the user to enter their email address before token verification.
101+
102+
Learn more in the [one-time token password reset guide](/end-user-flows/one-time-token#reset-password).
99103

100104
</details>

0 commit comments

Comments
 (0)