Skip to main content

Authentication

Lark supports anonymous and token-based authentication. You choose how to authenticate when you connect, and you can change authentication state after connecting.

Anonymous authentication

The simplest option. Connect without any user identity:
Anonymous connections are not assigned a UID and auth will be null for any security rules checks. You can still choose to let anonymous users read or modify data in your app by setting your security rules appropriately.

Token-based authentication

For identified users, pass a JWT when creating the instance:
The token contains the user’s identity (UID, provider, custom claims) and is validated by the Lark server. Your security rules can then reference auth.uid, auth.provider, and any custom claims.
See the platform authentication docs for details on how to generate JWTs for your users.

Changing auth state after connecting

You don’t have to authenticate at connect time. You can sign in or out at any point after connecting.

db.signIn(token)

Authenticate with a new token. If you were previously anonymous or signed in as a different user, the auth state updates:

db.signOut()

Reverts to anonymous authentication:
After calling signOut(), the connection stays open. You’re still connected, just without user identity. Any subscriptions that depend on authenticated access (via security rules) may stop receiving updates.

The auth property

Access the current authentication state at any time:

Listening for auth changes

Use db.onAuthStateChanged(callback) to react to sign-in and sign-out events:
The callback fires immediately with the current auth state, then again whenever it changes.

Full example