> Note for AI agents: For other pages, see [llms.txt](https://supabase.com/docs/llms.txt).

# Convert an anonymous user

Convert an anonymous user to a permanent user by linking an email, phone, or OAuth identity, and refresh the session so row-level security sees the change.

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](https://supabase.com/docs/guides/auth/auth-anonymous).
- [Manual linking enabled](https://supabase.com/dashboard/project/_/auth/providers) in your Supabase project. Converting a user requires [linking an identity](https://supabase.com/docs/guides/auth/auth-identity-linking#manual-linking-beta).

## Link an email / phone identity

**JavaScript**

Use [`updateUser()`](https://supabase.com/docs/reference/javascript/auth-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.

```js
import { createClient } from '@supabase/supabase-js'

const supabase = createClient('https://your-project-id.supabase.co', 'sb_publishable_...')

// ---cut---
const { data: updateEmailData, error: updateEmailError } = await supabase.auth.updateUser({
  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
const { data: updatePasswordData, error: updatePasswordError } = await supabase.auth.updateUser({
  password: 'password',
})
```

**Flutter**

Use [`updateUser()`](https://supabase.com/docs/reference/dart/auth-updateuser) to link an email or phone identity to the anonymous user.

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

**Swift**

Use [`update(user:)`](https://supabase.com/docs/reference/swift/auth-updateuser) to link an email or phone identity to the anonymous user.

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

**Kotlin**

Use [`updateUser()`](https://supabase.com/docs/reference/kotlin/auth-updateuser) to link an email or phone identity to the anonymous user.

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

**Python**

Use [`update_user()`](https://supabase.com/docs/reference/python/auth-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.

```python
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',
})
```

**C#**

Use [`Update()`](https://supabase.com/docs/reference/csharp/auth-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.

```c#
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" });
```

## Link an OAuth identity

**JavaScript**

Use [`linkIdentity()`](https://supabase.com/docs/reference/javascript/auth-linkidentity) to link an OAuth identity to the anonymous user.

```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.linkIdentity({ provider: 'google' })
```

**Flutter**

Use [`linkIdentity()`](https://supabase.com/docs/reference/dart/auth-linkidentity) to link an OAuth identity to the anonymous user.

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

**Swift**

Use [`linkIdentity()`](https://supabase.com/docs/reference/swift/auth-linkidentity) to link an OAuth identity to the anonymous user.

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

**Kotlin**

Use [`linkIdentity()`](https://supabase.com/docs/reference/kotlin/auth-linkidentity) to link an OAuth identity to the anonymous user.

```kotlin
supabase.auth.linkIdentity(Google)
```

**Python**

Use [`link_identity()`](https://supabase.com/docs/reference/python/auth-linkidentity) to link an OAuth identity to the anonymous user.

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

**C#**

Use [`LinkIdentity()`](https://supabase.com/docs/reference/csharp/auth-linkidentity) to link an OAuth identity to the anonymous user.

```c#
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:

```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.refreshSession()
```

To write policies that don't depend on the token, see [Restrict anonymous user access](https://supabase.com/docs/guides/auth/auth-anonymous/access-control).

## 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

### 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:

```js
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:

- [Restrict anonymous user access](https://supabase.com/docs/guides/auth/auth-anonymous/access-control): write policies that treat a linked user as permanent.
- [Identity linking](https://supabase.com/docs/guides/auth/auth-identity-linking): manage the identities on a permanent user.
