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:
- A user signed in with anonymous sign-ins.
- Manual linking enabled in your Supabase project. Converting a user requires linking an identity.
Link an email / phone identity
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 passwordconst { : , : } = 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 passwordresponse = 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 passwordvar updatePassword = await supabase.Auth.Update(new UserAttributes { Password = "password" });Link an OAuth identity
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:
- Overwrite the items in the cart with those in the existing account
- Overwrite the items in the cart with those from the anonymous user
- Merge the items in the cart together
Link an anonymous user to an existing account
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 anonymousconst { 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 emailconst { 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:
- Restrict anonymous user access: write policies that treat a linked user as permanent.
- Identity linking: manage the identities on a permanent user.