Handle 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.
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.
Important
| Method | Description |
|---|---|
db.SignUp() | Connects to a local or remote database endpoint |
db.SignIn() | Signs in to a root, namespace, database or scope user |
db.Invalidate() | Invalidates the current session |
db.Authenticate() | Authenticates a user with a token |
Defining access in your application
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:
Note
User authentication
After you have defined your authentication login, you can use the following methods to authenticate users:
.SignUp()
Signs up to a specific authentication scope / access method.
Arguments
| Arguments | Description |
|---|---|
credentials
| Credentials to sign up as a scoped user. |
cancellationToken
| The cancellationToken enables graceful cancellation of asynchronous operations. |
Example usage
.SignIn()
Signs in to a root, namespace, database or scope user.
Arguments
| Arguments | Description |
|---|---|
credentials
| Variables used in a signin query. |
cancellationToken
| The cancellationToken enables graceful cancellation of asynchronous operations. |
Example usage
.Authenticate()
Authenticates the current connection with a JWT token.
Arguments
| Arguments | Description |
|---|---|
jwt
| The JWT object holder of the authentication token. |
cancellationToken
| The cancellationToken enables graceful cancellation of asynchronous operations. |
Example usage
.Invalidate()
Invalidates the authentication for the current connection.
Arguments
| Properties | Description |
|---|---|
cancellationToken
| The cancellationToken enables graceful cancellation of asynchronous operations. |