Skip to content

v1 (stable)

/

Concepts

Authentication

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.

MethodDescription
$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

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
;
');

Signs up to a specific authentication access method.

Method Syntax
$db->signup([
    "namespace" => "main",
    "database" => "db",
    "access" => "account",
    // ... other variables
]);

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.

$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"
]);

Signs in to a root, namespace, database or record user.

Method Syntax
$db->signin([
    "namespace" => "main",
    "database" => "db",
    "access" => "account",
    // ... other variables
]);

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

access

before SurrealDB 2.0.

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"
]);

Authenticates the current connection with a JWT token. Use it when you already have an auth token stored somewhere.

Method Syntax
$db->authenticate($token)

Arguments

Type

Description

tokenstring

The JWT authentication token.

$db->authenticate('your-jwt-token');

This method returns the authenticated record user as an associative array.

Method Syntax
$db->info()
$user = $db->info();

Invalidates the authentication for the current connection. The user session is invalidated and the user is signed out.

Method Syntax
$db->invalidate()
$db->invalidate();

Was this page helpful?

Last updated