Files
better-auth/docs/content/docs/plugins/anonymous.mdx
T
Bereket EngidaandGitHub 2953fe882d refactor: move cli to it's own pacakge (#176)
* refactor

* cleanup and more refactor

* test: cli

* chore: release v0.4.9-beta.12

* feat: kysley generate and orm migrations

* chore: release v0.4.9-beta.13

* test: generate

* test: change test snapshot file

* docs

* chore: lint

* fix: test

* chore: release v0.4.9-beta.14

* chore: release v0.4.9
2024-10-15 16:35:14 +03:00

114 lines
2.8 KiB
Plaintext

---
title: Anonymous
description: Anonymous plugin for Better Auth.
---
The Anonymous plugin allows users to have an authenticated experience without requiring them to provide an email address, password, OAuth provider, or any other Personally Identifiable Information (PII). Users can later link an authentication method to their account when ready.
## Installation
<Steps>
<Step>
### Add the plugin to your auth config
To enable anonymous authentication, add the anonymous plugin to your authentication configuration.
```ts title="auth.ts"
import { betterAuth } from "better-auth"
import { anonymous } from "better-auth/plugins" // [!code highlight]
export const auth = betterAuth({
// ... other config options
plugins: [
anonymous() // [!code highlight]
]
})
```
</Step>
<Step>
### Migrate the database
Run the migration or generate the schema to add the necessary fields and tables to the database.
<Tabs items={["migrate", "generate"]}>
<Tab value="migrate">
```bash
npx @better-auth/cli migrate
```
</Tab>
<Tab value="generate">
```bash
npx @better-auth/cli generate
```
</Tab>
</Tabs>
See the [Schema](#schema) section to add the fields manually.
</Step>
<Step>
### Add the client plugin
Next, include the anonymous client plugin in your authentication client instance.
```ts title="client.ts"
import { createAuthClient } from "better-auth/client"
import { anonymousClient } from "better-auth/client/plugins"
const client = createAuthClient({
plugins: [
anonymousClient()
]
})
```
</Step>
</Steps>
## Usage
### Sign In
To sign in a user anonymously, use the `signIn.anonymous()` method.
```ts title="example.ts"
const user = await client.signIn.anonymous()
```
### Link Account
Once the user is signed in, you can link an account to their anonymous profile. Currently, only email/password linking is supported.
```ts title="example.ts"
const user = await client.anonymous.linkAccount({
email: "user@email.com",
password: "secure-password"
})
```
## Options
- `emailDomainName`: The domain name to use when generating an email address for anonymous users. Defaults to the domain name of the current site.
```ts title="auth.ts"
import { betterAuth } from "better-auth"
export const auth = betterAuth({
plugins: [
anonymous({
emailDomainName: "example.com"
})
]
})
```
## Schema
The anonymous plugin requires an additional field in the user table:
<DatabaseTable
fields={[
{ name: "isAnonymous", type: "boolean", description: "Indicates whether the user is anonymous.", isOptional: true },
]}
/>