Since SurrealDB is a database that is designed to be used in a distributed environment, it is important to secure the database and the data that is stored in it. SurrealDB provides a number of methods for authenticating users and securing the database.
| Method | Description |
|---|---|
$db->signup($vars) | Signs this connection up to a specific authentication access method |
$db->signin($vars) | Signs this connection in to a root, namespace, database or record user |
$db->authenticate($token) | Authenticates the current connection with a JWT token |
$db->info() | Returns the record of the authenticated user |
$db->invalidate() | Invalidates the authentication for the current connection |
Define scope
If you haven't defined a scope for your database, you can define a scope by querying the database with the query() method.
$db->query('
DEFINE SCOPE user SESSION 24h
SIGNUP ( CREATE user SET email = $email, pass = crypto::argon2::generate($pass) )
SIGNIN ( SELECT * FROM user WHERE email = $email AND crypto::argon2::compare(pass, $pass) );
');In 2.0 and above, the DEFINE SCOPE Statement has been replaced with the DEFINE ACCESS ... TYPE RECORD Statement so the above query would be:
$db->query('
DEFINE ACCESS user ON DATABASE TYPE RECORD
SIGNUP ( CREATE user SET email = $email, pass = crypto::argon2::generate($pass) )
SIGNIN ( SELECT * FROM user WHERE email = $email AND crypto::argon2::compare(pass, $pass) )
DURATION FOR TOKEN 15m, FOR SESSION 12h
;
'); ->signup()
Signs up to a specific authentication access method.
$db->signup([
"namespace" => "main",
"database" => "db",
"access" => "account",
// ... other variables
]);Arguments
Arguments | Description |
|---|---|
namespace | The namespace to sign up to |
database | The database to sign up to |
access | The access method to sign up to. Also pass any variables used in the access definition. |
Example usage
$token = $db->signup([
"namespace" => "surrealdb",
"database" => "docs",
"access" => "user",
// Also pass any properties required by the access definition
"email" => "info@surrealdb.com",
"pass" => "123456",
]);Before SurrealDB 2.0, a scope took the place of an access method, and the scope key took the place of access.
$db->signup([
"namespace" => "surrealdb",
"database" => "surrealdb",
"scope" => "user",
"email" => "user@email.com",
"pass" => "password-123"
]); ->signin()
Signs in to a root, namespace, database or record user.
$db->signin([
"namespace" => "main",
"database" => "db",
"access" => "account",
// ... other variables
]);Arguments
Properties | Description |
|---|---|
username | The username of the database user |
password | The password of the database user |
namespace | The namespace to sign in to |
database | The database to sign in to |
access | The access method to sign in to. Also pass any variables used in the access definition. |
scope | The scope to sign in to, used in place of accessbefore SurrealDB 2.0. |
Example usage
The tabs below show each kind of user.
Root authentication gives you access to all namespaces and databases within the SurrealDB instance.
$token = $db->signin([
"username" => "root",
"password" => "secret"
]); ->authenticate()
Authenticates the current connection with a JWT token. Use it when you already have an auth token stored somewhere.
$db->authenticate($token)Arguments
Arguments | Type | Description |
|---|---|---|
token | string | The JWT authentication token. |
Example usage
$db->authenticate('your-jwt-token'); ->info()
This method returns the authenticated record user as an associative array.
$db->info()Example usage
$user = $db->info(); ->invalidate()
Invalidates the authentication for the current connection. The user session is invalidated and the user is signed out.
$db->invalidate()Example usage
$db->invalidate();