Skip to content

Concepts

Authentication

SurrealDB is designed to be used in a distributed environment, so the database and the data stored in it need to be secured. SurrealDB provides a number of methods for authenticating users and securing the database.

In your SurrealDB database, you can create authentication login using the DEFINE ACCESS statement which supports JWT and Record Access methods.

The access method used will inform the input for Access in the .SignUp() and .SignIn() methods. This page follows a record user from sign-up to sign-out and shows every credential type. Every method takes an optional cancellationToken, which cancels the operation.

Important

If you are not on Version v4.0.1 of SurrealDB, you will use the Scope property instead of Access.

MethodDescription
db.SignUp()Signs up a new record user through an access method
db.SignIn()Signs in to a root, namespace, database or record user
db.Authenticate()Authenticates the current connection with a token
db.Invalidate()Invalidates the authentication for the current connection
db.Info<T>()Returns the record of the signed-in record user

The .NET SDK has a .Query() method which allows you to write secure SurrealQL statements from within your application. Using this method, you can define access for your users and securely manage authentication. See the code example below:

await db.Query(
    $"""
    DEFINE ACCESS account 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;
    """
);
Note

Depending on the connection protocol you choose, authentication tokens and sessions lifetime work differently. Refer to the connection options documentation for more information.

A record user signs up with credentials that name the namespace, the database and the access method, together with every value the access method's SIGNUP clause reads. Define a class that extends ScopeAuth with those values as properties, and pass an instance to .SignUp(). The SDK sends each property under its C# name unless a [CborProperty] attribute (from Dahomey.Cbor.Attributes) names it, so give each property the name of the parameter the clause reads. For the account access method above, the clause reads $email and $pass:

var authParams = new AuthParams
{
    Namespace = "main",
    Database = "main",
    Access = "account",
    Email = "info@surrealdb.com",
    Pass = "123456"
};

Jwt jwt = await db.SignUp(authParams);

public class AuthParams : ScopeAuth
{
	[CborProperty("email")]
	public string? Email { get; set; }

	[CborProperty("pass")]
	public string? Pass { get; set; }
}

.SignUp() creates the user record, signs the connection in as that user and returns the token as a Jwt.

Method Syntax
await db.SignUp(credentials)

A user who already exists signs in with .SignIn(). The credentials say what kind of user it is: root, namespace, database or record. A record user passes the same credentials as at sign-up, and the access method runs its SIGNIN clause instead.

Method Syntax
await db.SignIn(credentials)
// Sign in as root user
await db.SignIn(new RootAuth { Username = "root",
    Password = "secret" });

A token returned by .SignUp() or .SignIn() can authenticate another connection, or the same connection later, without the user's credentials. Pass it to .Authenticate():

Method Syntax
await db.Authenticate(jwt)

The token is a Jwt, which can also be created from a token string that your application stored:

var jwt = new Jwt("<token>");
await db.Authenticate(jwt);

.Invalidate() removes the authentication from the current connection, so later requests run without a signed-in user:

await db.Invalidate();

.Info<T>() returns the record of the record user that the connection is signed in as.

Method Syntax
await db.Info<T>()
var currentUser = await db.Info<User>();

Was this page helpful?

Last updated