Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

feat: add unenroll documentation to mfa docs (#10435)

Joel Lee3126a5d21fee
diff --git a/apps/docs/pages/guides/auth/auth-mfa.mdx b/apps/docs/pages/guides/auth/auth-mfa.mdx
index 286916d29de..0591e3af52b 100644
--- a/apps/docs/pages/guides/auth/auth-mfa.mdx
+++ b/apps/docs/pages/guides/auth/auth-mfa.mdx
@@ -7,13 +7,6 @@ export const meta = {
'Add an additional layer of security to your apps with Supabase Auth multi-factor authentication.',
}
-<Admonition type="note">
-
-Multi-Factor Authentication is in early access preview only. Although we
-believe it is production ready, APIs and some behavior may change based on
-feedback we receive in the preview period.
-
-</Admonition>
Multi-factor authentication (MFA), sometimes called two-factor
authentication (2FA), adds an additional layer of security to your
@@ -94,17 +87,20 @@ for your application. JWTs without an `aal` claim are at the `aal1` level.
## Adding to your app
-Adding MFA to your app involves these three steps:
+Adding MFA to your app involves these four steps:
1. **Add enrollment flow.**
You need to provide a UI within your app that your users will be able to set-up
MFA in. You can add this right after sign-up, or as part of a separate flow in
the settings portion of your app.
-2. **Add challenge step to login.**
+2. **Add unenrollment flow.**
+ You need to support a UI through which users can see existing devices and unenroll
+ devices which are no longer relevant.
+3. **Add challenge step to login.**
If a user has set-up MFA, your app's login flow needs to present a challenge
screen to the user asking them to prove they have access to the additional
factor.
-3. **Enforce rules for MFA logins.**
+4. **Enforce rules for MFA logins.**
Once your users have a way to enroll and log in with MFA, you need to enforce
authorization rules across your app: on the frontend, backend, API servers or
Row-Level Security policies.
@@ -141,6 +137,7 @@ Enrolling a factor for use with MFA takes three steps:
immediately becomes active for the user account. If not, you should repeat
steps 2 and 3.
+
#### Example: React
Below is an example that creates a new `EnrollMFA` component that illustrates
@@ -241,6 +238,83 @@ export function EnrollMFA({
}
```
+### Add unenrollment flow
+
+An unenrollment flow provides a UI for users to manage and unenroll factors linked to their accounts.
+Most applications do so via a factor management page where users can view and unlink selected factors.
+
+When a user unenrolls a factor, call `supabase.auth.mfa.unenroll()` with the ID of the factor.
+For example, call `supabase.auth.mfa.unenroll({factorId: "d30fd651-184e-4748-a928-0a4b9be1d429"})` to unenroll a factor with ID `d30fd651-184e-4748-a928-0a4b9be1d429`.
+
+
+#### Example: React
+
+Below is an example that creates a new `UnenrollMFA` component that illustrates
+the important pieces of the MFA enrollment flow. Note that users can only unenroll a factor after completing the enrollment flow and obtaining
+an `aal2` JWT claim.Here are some points of note:
+
+- When the component appears on screen, the `supabase.auth.mfa.listFactors()` endpoint
+ fetches all existing factors together with their details.
+- The existing factors for a user are displayed in a table.
+- Once the user has selected a factor to unenroll, they can type in the factorId and click **Unenroll**
+ which creates a confirmation modal.
+
+<Admonition type="note">
+Unenrolling a factor will downgrade the assurance level from `aal2` to `aal1` only after the refresh interval has lapsed.
+For an immediate downgrade from `aal2` to `aal1` after enrolling one will need to manually call `refreshSession()`
+</Admonition>
+
+```tsx
+/**
+ * UnenrollMFA shows a simple table with the list of factors together with a button to unenroll.
+ * When a user types in the factorId of the factor that they wish to unenroll and clicks unenroll
+ * the corresponding factor will be unenrolled.
+ */
+export function UnenrollMFA() {
+ const [factorId, setFactorId] = useState('')
+ const [factors, setFactors] = useState([])
+ const [error, setError] = useState('') // holds an error message
+
+ useEffect(() => {
+ ;(async () => {
+ const { data, error } = await supabase.auth.mfa.listFactors()
+ if (error) {
+ throw error
+ }
+
+ setFactors(data.totp)
+ })()
+ }, [])
+
+ return (
+ <>
+ {error && <div className="error">{error}</div>}
+ <tbody>
+ <tr>
+ <td>Factor ID</td>
+ <td>Friendly Name</td>
+ <td>Factor Status</td>
+ </tr>
+ {factors.map(factor => (
+ <tr>
+ <td>{factor.id}</td>
+ <td>{factor.friendly_name}</td>
+ <td>{factor.factor_type}</td>
+ <td>{factor.status}</td>
+ </tr>
+ ))}
+ </tbody>
+ <input
+ type="text"
+ value={verifyCode}
+ onChange={(e) => setFactorId(e.target.value.trim())}
+ />
+ <button onClick={()=> supabase.auth.mfa.unenroll({factorId})}>Unenroll</button>
+ </>
+ )
+}
+```
+
### Add challenge step to login
Once a user has logged in via their first factor (email+password, magic link,
@@ -460,7 +534,7 @@ create policy "Policy name."
select
case
when created_at >= '2022-12-12T00:00:00Z' then array['aal2']
- else array['aal1', 'aal2', NULL]
+ else array['aal1', 'aal2']
end as aal
from auth.users
where auth.uid() = id));
@@ -471,8 +545,6 @@ create policy "Policy name."
`aal2` for all other timestamps.
- The `<@` operator is PostgreSQL's ["contained in"
operator.](https://www.postgresql.org/docs/current/functions-array.html)
-- `NULL` appears because some JWTs originating from prior to the introduction
- of MFA in Supabase Auth will not contain an `aal` claim.
- **Using `as restrictive` ensures this policy will restrict all commands on the
table regardless of other policies!**
@@ -491,7 +563,7 @@ create policy "Policy name."
select
case
when count(id) > 0 then array['aal2']
- else array['aal1', 'aal2', NULL]
+ else array['aal1', 'aal2']
end as aal
from auth.mfa_factors
where auth.uid() = user_id and status = 'verified'
@@ -503,8 +575,6 @@ create policy "Policy name."
- Otherwise, it will accept both `aal1` and `aal2`.
- The `<@` operator is PostgreSQL's ["contained in"
operator.](https://www.postgresql.org/docs/current/functions-array.html)
-- `NULL` appears because some JWTs originating from prior to the introduction
- of MFA in Supabase Auth will not contain an `aal` claim.
- **Using `as restrictive` ensures this policy will restrict all commands on the
table regardless of other policies!**
@@ -589,7 +659,7 @@ seconds later. The entries are ordered most recent method first!
{
"amr": [
{
- "method": "mfa/totp",
+ "method": "totp",
"timestamp": 1666086056
},
{
@@ -625,7 +695,7 @@ Currently recognized methods are:
- `otp` - any one-time password based sign in (email code, SMS code, magic
link).
- `oauth` - any OAuth based sign in (social login).
-- `mfa/totp` - a TOTP additional factor.
+- `totp` - a TOTP additional factor.
This list will expand in the future.