Skip to content

Updated

Convert an anonymous user

Link an identity to an anonymous user to make them a permanent user and keep their data.

Prerequisites

To convert an anonymous user, you need:

Use updateUser() to link an email or phone identity to the anonymous user. The user must verify their email address or phone number before you can add a password.

const { : , : } = await ..({
: 'valid.email@supabase.io',
})
// verify the user's email by clicking on the email change link
// or entering the 6-digit OTP sent to the email address
// once the user has been verified, update the password
const { : , : } = await ..({
: 'password',
})

Use updateUser() to link an email or phone identity to the anonymous user.

await supabase.auth.updateUser(UserAttributes(email: 'valid.email@supabase.io'));

Use update(user:) to link an email or phone identity to the anonymous user.

try await supabase.auth.update(
user: UserAttributes(email: "valid.email@supabase.io")
)

Use updateUser() to link an email or phone identity to the anonymous user.

supabase.auth.updateUser {
email = "valid.email@supabase.io"
}

Use update_user() to link an email or phone identity to the anonymous user. The user must verify their email address or phone number before you can add a password.

response = supabase.auth.update_user({
'email': 'valid.email@supabase.io',
})
# verify the user's email by clicking on the email change link
# or entering the 6-digit OTP sent to the email address
# once the user has been verified, update the password
response = supabase.auth.update_user({
'password': 'password',
})

Use Update() to link an email or phone identity to the anonymous user. The user must verify their email address or phone number before you can add a password.

var updateEmail = await supabase.Auth.Update(new UserAttributes { Email = "valid.email@supabase.io" });
// verify the user's email by clicking on the email change link
// or entering the 6-digit OTP sent to the email address
// once the user has been verified, update the password
var updatePassword = await supabase.Auth.Update(new UserAttributes { Password = "password" });

Use linkIdentity() to link an OAuth identity to the anonymous user.

const { , } = await ..({ : 'google' })

Use linkIdentity() to link an OAuth identity to the anonymous user.

await supabase.auth.linkIdentity(OAuthProvider.google);

Use linkIdentity() to link an OAuth identity to the anonymous user.

try await supabase.auth.linkIdentity(provider: .google)

Use linkIdentity() to link an OAuth identity to the anonymous user.

supabase.auth.linkIdentity(Google)

Use link_identity() to link an OAuth identity to the anonymous user.

response = supabase.auth.link_identity({'provider': 'google'})

Use LinkIdentity() to link an OAuth identity to the anonymous user.

var state = await supabase.Auth.LinkIdentity(Provider.Google, new SignInOptions { FlowType = OAuthFlowType.PKCE });
var authorizeUrl = state.Uri;

Refresh the session after linking

Linking an identity makes the user permanent at once, but the access token the client holds still has is_anonymous: true until the session refreshes. RLS policies that check the claim keep treating the user as anonymous until then. Refresh the session after linking so the next request carries the updated claim:

const { , } = await ..()

To write policies that don't depend on the token, see Restrict anonymous user access.

Resolve identity conflicts

Data conflicts can arise when an anonymous user becomes a permanent user. For example, an e-commerce app might let an anonymous user add items to a cart without signing up or signing in. When that user signs in to an existing account, decide how to resolve conflicts in the cart:

  1. Overwrite the items in the cart with those in the existing account
  2. Overwrite the items in the cart with those from the anonymous user
  3. Merge the items in the cart together

To link an anonymous user to an existing account instead of creating a new permanent account, handle the conflicts yourself. A general approach:

import { createClient } from '@supabase/supabase-js'
const supabase = createClient('https://your-project-id.supabase.co', 'sb_publishable_...')
// ---cut---
// 1. Get the current session and verify the user is anonymous
const { data: anonData, error: anonError } = await supabase.auth.getSession()
if (!anonData.session?.user?.is_anonymous) {
throw new Error('User is not anonymous. This flow only applies to anonymous users.')
}
// 2. Attempt to update the user with the existing email
const { data: updateData, error: updateError } = await supabase.auth.updateUser({
email: 'valid.email@supabase.io',
})
// 3. Handle the error (since the email belongs to an existing user)
if (updateError) {
console.log('This email belongs to an existing user. Please sign in to that account.')
// 4. Sign in to the existing account
const {
data: { user: existingUser },
error: signInError,
} = await supabase.auth.signInWithPassword({
email: 'valid.email@supabase.io',
password: 'user_password',
})
if (existingUser) {
// 5. Reassign entities tied to the anonymous user
// This step will vary based on your specific use case and data model
const { data: reassignData, error: reassignError } = await supabase
.from('your_table')
.update({ user_id: existingUser.id })
.eq('user_id', anonData.session.user.id)
// 6. Implement your chosen conflict resolution strategy
// This could involve merging data, overwriting, or other custom logic
await resolveDataConflicts(anonData.session.user.id, existingUser.id)
}
}
// Helper function to resolve data conflicts (implement based on your strategy)
async function resolveDataConflicts(anonymousUserId, existingUserId) {
// Implement your conflict resolution logic here
// This could involve ignoring the anonymous user's metadata, overwriting the existing user's metadata, or merging the data of both the anonymous and existing user.
}

Next steps

To keep going, use these pages: