mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 15:39:13 +00:00
## Problem
The passwordless email guide says to add `{{ .Token }}` to the **Magic
link or OTP** template to send a code. When email confirmations are on,
a first-time sign-in sends the **Confirm sign up** email instead, so new
users still get a link. This is the failure the passwordless email guide
eval catches.
Closes DOCS-1814
## Solution
This PR solves the Eval checks.
**Enable email OTP** now covers both templates:
- It says which template each user gets.
- It has you add `{{ .Token }}` to both **Magic link or OTP** and
**Confirm sign up**.
- It notes that a link left in either template still works.
The rest of the PR is style, structure, and template changes. Existing
anchors still resolve.
## Manual testing
1. Open the [preview
page](https://docs-git-docs-email-otp-confirm-signup-supabase.vercel.app/docs/guides/auth/auth-email-passwordless#enabling-email-otp)
and check that it lands on **Enable email OTP**.
2. Click the **Magic link or OTP** and **Confirm sign up** template
links and check that each opens its template in the dashboard.
423 lines
13 KiB
Plaintext
423 lines
13 KiB
Plaintext
---
|
|
title: 'Passwordless email sign-in'
|
|
description: 'Let users sign in to your app with a magic link or a one-time code sent to their email address, and set up the Auth email templates that deliver each one.'
|
|
---
|
|
|
|
Let users sign in with only their email address, by clicking a magic link or entering a one-time password (OTP).
|
|
|
|
## Overview
|
|
|
|
Supabase Auth offers two passwordless sign-in methods that use the user's email address:
|
|
|
|
- [Magic link](#with-magic-link) uses the default email templates.
|
|
- [OTP](#with-otp) has you edit the email templates to send a code instead.
|
|
|
|
Both methods use the client library's "sign in with OTP" call, which sends a magic link by default. The email template decides whether the user gets a link or a code.
|
|
|
|
## With magic link
|
|
|
|
Magic links are a form of passwordless sign-in where users click a link sent to their email address. Magic links work only with email addresses, and each link works only once.
|
|
|
|
### Enable magic links [#enabling-magic-link]
|
|
|
|
Email authentication methods, including magic links, are enabled by default.
|
|
|
|
Configure the Site URL and any additional [redirect URLs](/docs/guides/auth/redirect-urls). Only these URLs are allowed as redirect destinations after the user clicks a magic link. You can change the URLs on the [URL Configuration page](/dashboard/project/_/auth/url-configuration) for hosted projects, in your [config file](/docs/guides/local-development/cli/config#auth.additional_redirect_urls) for local development, or in the `.env` configuration file for [self-hosted Supabase](/docs/guides/self-hosting/docker).
|
|
|
|
By default, a user can request a magic link only once every <SharedData data="config">auth.rate_limits.magic_link.period</SharedData>, and each link expires after <SharedData data="config">auth.rate_limits.magic_link.validity</SharedData>.
|
|
|
|
To test magic links without hitting email limits, see [Test auth flows](/docs/guides/auth/testing-auth-flows).
|
|
|
|
### Sign in with a magic link [#signing-in-with-magic-link]
|
|
|
|
Call the "sign in with OTP" method from the client library.
|
|
|
|
If the user hasn't signed up, the call signs them up by default. To prevent this, set the `shouldCreateUser` option to `false`.
|
|
|
|
<Tabs
|
|
scrollable
|
|
size="small"
|
|
type="underlined"
|
|
defaultActiveId="js"
|
|
queryGroup="language"
|
|
>
|
|
<TabPanel id="js" label="JavaScript">
|
|
|
|
```js
|
|
import { createClient } from '@supabase/supabase-js'
|
|
|
|
const supabase = createClient('https://your-project-id.supabase.co', 'sb_publishable_...')
|
|
|
|
// ---cut---
|
|
async function signInWithEmail() {
|
|
const { data, error } = await supabase.auth.signInWithOtp({
|
|
email: 'valid.email@supabase.io',
|
|
options: {
|
|
// set this to false if you do not want the user to be automatically signed up
|
|
shouldCreateUser: false,
|
|
emailRedirectTo: 'https://example.com/welcome',
|
|
},
|
|
})
|
|
}
|
|
```
|
|
|
|
</TabPanel>
|
|
<TabPanel id="react-native" label="Expo React Native">
|
|
|
|
```ts
|
|
import { makeRedirectUri } from 'expo-auth-session'
|
|
|
|
const redirectTo = makeRedirectUri()
|
|
|
|
const { error } = await supabase.auth.signInWithOtp({
|
|
email: 'valid.email@supabase.io',
|
|
options: {
|
|
emailRedirectTo: redirectTo,
|
|
},
|
|
})
|
|
```
|
|
|
|
To handle the redirect in your app, see [Native mobile deep linking](/docs/guides/auth/native-mobile-deep-linking).
|
|
|
|
</TabPanel>
|
|
<$Show if="sdk:dart">
|
|
<TabPanel id="dart" label="Dart">
|
|
|
|
```dart
|
|
Future<void> signInWithEmail() async {
|
|
await supabase.auth.signInWithOtp(email: 'valid.email@supabase.io');
|
|
}
|
|
```
|
|
|
|
</TabPanel>
|
|
</$Show>
|
|
<$Show if="sdk:swift">
|
|
<TabPanel id="swift" label="Swift">
|
|
|
|
```swift
|
|
try await supabase.auth.signInWithOTP(
|
|
email: "valid.email@supabase.io",
|
|
redirectTo: URL(string: "https://example.com/welcome"),
|
|
// set this to false if you do not want the user to be automatically signed up
|
|
shouldCreateUser: false
|
|
)
|
|
```
|
|
|
|
</TabPanel>
|
|
</$Show>
|
|
<$Show if="sdk:kotlin">
|
|
<TabPanel id="kotlin" label="Kotlin">
|
|
|
|
```kotlin
|
|
suspend fun signInWithEmail() {
|
|
supabase.auth.signInWith(OTP) {
|
|
email = "valid.email@supabase.io"
|
|
}
|
|
}
|
|
```
|
|
|
|
</TabPanel>
|
|
</$Show>
|
|
<$Show if="sdk:python">
|
|
<TabPanel id="python" label="Python">
|
|
|
|
```python
|
|
response = supabase.auth.sign_in_with_otp({
|
|
'email': 'valid.email@supabase.io',
|
|
'options': {
|
|
# set this to false if you do not want the user to be automatically signed up
|
|
'should_create_user': False,
|
|
'email_redirect_to': 'https://example.com/welcome',
|
|
},
|
|
})
|
|
```
|
|
|
|
</TabPanel>
|
|
</$Show>
|
|
<$Show if="sdk:csharp">
|
|
<TabPanel id="csharp" label="C#">
|
|
|
|
```c#
|
|
var options = new SignInOptions { RedirectTo = "https://example.com/welcome" };
|
|
var didSendMagicLink = await supabase.Auth.SendMagicLink("valid.email@supabase.io", options);
|
|
```
|
|
|
|
</TabPanel>
|
|
</$Show>
|
|
</Tabs>
|
|
|
|
The implicit flow needs no further setup.
|
|
|
|
#### Use the PKCE flow
|
|
|
|
If you use the PKCE flow, edit the **Magic link or OTP** [email template](/docs/guides/auth/auth-email-templates) to send a token hash:
|
|
|
|
```html
|
|
<h2>Sign in to your account</h2>
|
|
|
|
<p>Use this link to sign in to your account:</p>
|
|
<p><a href="{{ .SiteURL }}/auth/confirm?token_hash={{ .TokenHash }}&type=email">Sign in</a></p>
|
|
```
|
|
|
|
At the `/auth/confirm` endpoint, exchange the hash for the session:
|
|
|
|
```js
|
|
import { createClient } from '@supabase/supabase-js'
|
|
|
|
const supabase = createClient('https://your-project-id.supabase.co', 'sb_publishable_...')
|
|
|
|
// ---cut---
|
|
const { error } = await supabase.auth.verifyOtp({
|
|
token_hash: 'hash',
|
|
type: 'email',
|
|
})
|
|
```
|
|
|
|
## With OTP
|
|
|
|
Email one-time passwords (OTP) are a form of passwordless sign-in where users enter a six-digit code sent to their email address.
|
|
|
|
### Enable email OTP [#enabling-email-otp]
|
|
|
|
Email authentication methods, including email OTPs, are enabled by default.
|
|
|
|
The email template decides whether a user gets a link or a code. Which template Auth sends depends on the user:
|
|
|
|
- A user who has already confirmed their email address gets the **Magic link or OTP** template.
|
|
- When email confirmations are on, a user signing in with a new or unconfirmed email address gets the **Confirm sign up** template instead, because the sign-in call also creates their account.
|
|
|
|
To send every user an OTP instead of a magic link, add the `{{ .Token }}` variable to both templates:
|
|
|
|
1. Edit the [**Magic link or OTP** template](/dashboard/project/_/auth/templates/magic-link-or-otp) to include `{{ .Token }}`, for example:
|
|
|
|
```html
|
|
<h2>One time login code</h2>
|
|
|
|
<p>Please enter this code: {{ .Token }}</p>
|
|
```
|
|
|
|
2. Edit the [**Confirm sign up** template](/dashboard/project/_/auth/templates/confirm-sign-up) to include `{{ .Token }}` the same way.
|
|
|
|
If either template still contains a link, users can still sign in by clicking it instead of entering the code. For how confirmations work and how to edit templates outside the Dashboard, see the [Email templates guide](/docs/guides/auth/auth-email-templates).
|
|
|
|
By default, a user can request an OTP only once every <SharedData data="config">auth.rate_limits.otp.period</SharedData>, and each OTP expires after <SharedData data="config">auth.rate_limits.otp.validity</SharedData>. To change the expiry, go to **Authentication > Sign In / Providers > Auth Providers > Email > Email OTP expiration**.
|
|
|
|
Avoid an expiry longer than 86,400 seconds (one day). You can set one only through the [Management API](/docs/reference/api/v1-update-auth-service-config). Before going to production, read the [security recommendations](/docs/guides/security/production-checklist#auth).
|
|
|
|
<Admonition type="caution">
|
|
|
|
Changing **Email OTP expiration** also changes how long magic links and other email links stay valid, including confirmation, password recovery, email change, and [invitation](/docs/guides/auth/users#inviting-users) links.
|
|
|
|
</Admonition>
|
|
|
|
If users see an expired-token error, see [OTP verification failures](/kb/troubleshooting/otp-verification-failures-token-has-expired-or-otp_expired-errors-5ee4d0).
|
|
|
|
### Sign in with email OTP [#signing-in-with-email-otp]
|
|
|
|
Signing in with an OTP takes two calls: one sends a code to the user's email address, and one verifies the code they enter.
|
|
|
|
#### Step 1: Send the user an OTP code
|
|
|
|
Get the user's email and call the "sign in with OTP" method from your client library.
|
|
|
|
If the user hasn't signed up, the call signs them up by default. To prevent this, set the `shouldCreateUser` option to `false`.
|
|
|
|
<Tabs
|
|
scrollable
|
|
size="small"
|
|
type="underlined"
|
|
defaultActiveId="js"
|
|
queryGroup="language"
|
|
>
|
|
<TabPanel id="js" label="JavaScript">
|
|
|
|
```js
|
|
import { createClient } from '@supabase/supabase-js'
|
|
|
|
const supabase = createClient('https://your-project-id.supabase.co', 'sb_publishable_...')
|
|
|
|
// ---cut---
|
|
const { data, error } = await supabase.auth.signInWithOtp({
|
|
email: 'valid.email@supabase.io',
|
|
options: {
|
|
// set this to false if you do not want the user to be automatically signed up
|
|
shouldCreateUser: false,
|
|
},
|
|
})
|
|
```
|
|
|
|
</TabPanel>
|
|
<$Show if="sdk:dart">
|
|
<TabPanel id="dart" label="Dart">
|
|
|
|
```dart
|
|
Future<void> signInWithEmailOtp() async {
|
|
await supabase.auth.signInWithOtp(email: 'valid.email@supabase.io');
|
|
}
|
|
```
|
|
|
|
</TabPanel>
|
|
</$Show>
|
|
<$Show if="sdk:swift">
|
|
<TabPanel id="swift" label="Swift">
|
|
|
|
```swift
|
|
try await supabase.auth.signInWithOTP(
|
|
email: "valid.email@supabase.io",
|
|
// set this to false if you do not want the user to be automatically signed up
|
|
shouldCreateUser: false
|
|
)
|
|
```
|
|
|
|
</TabPanel>
|
|
</$Show>
|
|
<$Show if="sdk:kotlin">
|
|
<TabPanel id="kotlin" label="Kotlin">
|
|
|
|
```kotlin
|
|
suspend fun signInWithEmailOtp() {
|
|
supabase.auth.signInWith(OTP) {
|
|
email = "valid.email@supabase.io"
|
|
}
|
|
}
|
|
```
|
|
|
|
</TabPanel>
|
|
</$Show>
|
|
<$Show if="sdk:python">
|
|
<TabPanel id="python" label="Python">
|
|
|
|
```python
|
|
response = supabase.auth.sign_in_with_otp({
|
|
'email': 'valid.email@supabase.io',
|
|
'options': {
|
|
# set this to false if you do not want the user to be automatically signed up
|
|
'should_create_user': False,
|
|
},
|
|
})
|
|
```
|
|
|
|
</TabPanel>
|
|
</$Show>
|
|
<$Show if="sdk:csharp">
|
|
<TabPanel id="csharp" label="C#">
|
|
|
|
```c#
|
|
await supabase.Auth.SendMagicLink("valid.email@supabase.io");
|
|
```
|
|
|
|
</TabPanel>
|
|
</$Show>
|
|
</Tabs>
|
|
|
|
If the request is successful, you receive a response with `error: null` and a `data` object where both `user` and `session` are null. Tell the user to check their inbox.
|
|
|
|
```json
|
|
{
|
|
"data": {
|
|
"user": null,
|
|
"session": null
|
|
},
|
|
"error": null
|
|
}
|
|
```
|
|
|
|
#### Step 2: Verify the OTP to create a session
|
|
|
|
Provide an input field for the user to enter their one-time code.
|
|
|
|
Call the "verify OTP" method from your client library with the user's email address, the code, and a type of `email`:
|
|
|
|
<Tabs
|
|
scrollable
|
|
size="small"
|
|
type="underlined"
|
|
defaultActiveId="js"
|
|
queryGroup="language"
|
|
>
|
|
<TabPanel id="js" label="JavaScript">
|
|
|
|
```js
|
|
import { createClient } from '@supabase/supabase-js'
|
|
|
|
const supabase = createClient('https://your-project-id.supabase.co', 'sb_publishable_...')
|
|
|
|
// ---cut---
|
|
const {
|
|
data: { session },
|
|
error,
|
|
} = await supabase.auth.verifyOtp({
|
|
email: 'email@example.com',
|
|
token: '123456',
|
|
type: 'email',
|
|
})
|
|
```
|
|
|
|
</TabPanel>
|
|
<$Show if="sdk:swift">
|
|
<TabPanel id="swift" label="Swift">
|
|
|
|
```swift
|
|
try await supabase.auth.verifyOTP(
|
|
email: email,
|
|
token: "123456",
|
|
type: .email
|
|
)
|
|
```
|
|
|
|
</TabPanel>
|
|
</$Show>
|
|
<$Show if="sdk:kotlin">
|
|
<TabPanel id="kotlin" label="Kotlin">
|
|
|
|
```kotlin
|
|
supabase.auth.verifyEmailOtp(type = OtpType.Email.EMAIL, email = "email", token = "151345")
|
|
```
|
|
|
|
</TabPanel>
|
|
</$Show>
|
|
<$Show if="sdk:python">
|
|
<TabPanel id="python" label="Python">
|
|
|
|
```python
|
|
response = supabase.auth.verify_otp({
|
|
'email': email,
|
|
'token': '123456',
|
|
'type': 'email',
|
|
})
|
|
```
|
|
|
|
</TabPanel>
|
|
</$Show>
|
|
<$Show if="sdk:csharp">
|
|
<TabPanel id="csharp" label="C#">
|
|
|
|
```c#
|
|
var session = await supabase.Auth.VerifyOTP("email@example.com", "123456", EmailOtpType.Email);
|
|
```
|
|
|
|
</TabPanel>
|
|
</$Show>
|
|
</Tabs>
|
|
|
|
If successful, the user is now signed in, and you receive a valid session that looks like:
|
|
|
|
```json
|
|
{
|
|
"access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJhdXRoZW50aWNhdGVkIiwiZXhwIjoxNjI3MjkxNTc3LCJzdWIiOiJmYTA2NTQ1Zi1kYmI1LTQxY2EtYjk1NC1kOGUyOTg4YzcxOTEiLCJlbWFpbCI6IiIsInBob25lIjoiNjU4NzUyMjAyOSIsImFwcF9tZXRhZGF0YSI6eyJwcm92aWRlciI6InBob25lIn0sInVzZXJfbWV0YWRhdGEiOnt9LCJyb2xlIjoiYXV0aGVudGljYXRlZCJ9.1BqRi0NbS_yr1f6hnr4q3s1ylMR3c1vkiJ4e_N55dhM",
|
|
"token_type": "bearer",
|
|
"expires_in": 3600,
|
|
"refresh_token": "LSp8LglPPvf0DxGMSj-vaQ",
|
|
"user": {...}
|
|
}
|
|
```
|
|
|
|
## Next steps
|
|
|
|
To keep going, use these pages:
|
|
|
|
- [Send emails with custom SMTP](/docs/guides/auth/auth-smtp): send sign-in emails from your own provider before going to production.
|
|
- [Email templates](/docs/guides/auth/auth-email-templates): customize the content of every email Auth sends.
|
|
- [Test auth flows](/docs/guides/auth/testing-auth-flows): test sign-in without hitting email rate limits.
|
|
- [Phone sign-in](/docs/guides/auth/phone-login): send one-time passwords by SMS instead of email.
|