SurrealDB supports several levels of authentication, from system users to fine-grained record access. Version 2 of the PHP SDK represents each level as a typed credential class, so the required fields are explicit.
API references
| Method | Description |
|---|---|
$db->signin($auth) | Signs in as a root, namespace, database, or record user |
$db->signup($auth) | Signs up a new record user through an access method |
$db->authenticate($token) | Authenticates the session with an existing token |
$db->invalidate() | Invalidates the current session, signing the user out |
Credential types
Each authentication level has a credential class in the SurrealDB\SDK\Auth namespace.
| Class | Level | Constructor |
|---|---|---|
RootAuth | Root (system) user | new RootAuth($username, $password) |
NamespaceAuth | Namespace user | new NamespaceAuth($namespace, $username, $password) |
DatabaseAuth | Database user | new DatabaseAuth($namespace, $database, $username, $password) |
RecordAccessAuth | Record access | new RecordAccessAuth($namespace, $database, $access, $variables) |
BearerAuth | Bearer access key | new BearerAuth($namespace, $database, $access, $key) |
Signing in users
The signin() method authenticates an existing user. Pass the credential class for the level you need. It returns a Tokens object holding the access token and an optional refresh token.
The session is authenticated after a successful sign in.
Note
Signing up users
The signup() method creates a new record user through a defined record access method. Use RecordAccessAuth with the variables your access definition expects.
Authenticating with a token
If you already have an access token, authenticate with it directly instead of signing in again. This restores a session without re-entering credentials.
Bearer access
A bearer access key authenticates against a defined bearer access method. Use BearerAuth with the access name and the key.
Refreshing and revoking tokens
When a record access method issues refresh tokens, signin() and signup() return a Tokens object with both an access and a refresh token. Use the refresh token to obtain a new pair without re-entering credentials. Both operations are on the ConnectionController.
Important
By default the SDK renews an expiring token in the background. To sign the session out instead of renewing it, set invalidateOnExpiry to true on connect().
Providing credentials on connect
Rather than calling signin() separately, pass credentials to connect() through the authentication option. This is preferred for system users because it lets the SDK re-authenticate automatically after a reconnect.
The authentication option also accepts a closure, which is useful when credentials are fetched at runtime.
Listening to authentication changes
The SDK emits an auth event whenever the authentication state changes, including on sign in, sign up, token renewal, and invalidation. The payload is the current Tokens object, or null when signed out.
Selecting the current user
The auth() builder compiles to SELECT * FROM ONLY $auth, which returns the record of the authenticated record user.
Signing out
The invalidate() method clears the session's authentication. Queries after this run unauthenticated.
Learn more
Surreal API reference for the authentication method signatures
Authentication in SurrealDB for how authentication works at the database level
DEFINE ACCESS for defining access methods