SurrealDB supports a number of methods for authenticating users and securing the database. The Swift SDK exposes these through the methods below.
Method | Description |
|---|---|
client.signup(credentials) | Signs this connection up to a specific authentication access |
client.signin(credentials) | Signs this connection in to a specific authentication level |
client.authenticate(token) | Authenticates the current connection with a JWT token |
client.invalidate() | Invalidates the authentication for the current connection |
.signin()
Signs this connection in to a specific authentication level and returns the issued tokens.
let tokens = try await client.signin(credentials)Arguments
| Argument | Description |
|---|---|
credentials (required) | The credentials to authenticate with: one of .root, .namespace, .database, .accessVariables or .accessBearer. |
Example usage
The credentials enum describes the level you wish to authenticate at.
// Root user
let tokens = try await client.signin(.root(username: "root", password: "secret"))
// Namespace user
let tokens = try await client.signin(.namespace(
namespace: "myapp",
username: "ns_user",
password: "secret"
))
// Database user
let tokens = try await client.signin(.database(
namespace: "myapp",
database: "mydb",
username: "db_user",
password: "secret"
))
// Record access with variables
let tokens = try await client.signin(.accessVariables(
namespace: "myapp",
database: "mydb",
access: "account",
variables: ["email": .string("user@example.com"), "pass": .string("secret")]
))
// Bearer token access
let tokens = try await client.signin(.accessBearer(
namespace: "myapp",
database: "mydb",
access: "account",
key: "bearer-token-value"
)) .signup()
Signs a new record-access user up and returns the issued tokens.
let tokens = try await client.signup(credentials)Arguments
| Argument | Description |
|---|---|
credentials (required) | The record-access credentials to sign up with, created via .accessRecord. |
Example usage
let tokens = try await client.signup(.accessRecord(
namespace: "myapp",
database: "mydb",
access: "account",
variables: ["email": .string("new@example.com"), "pass": .string("secret")]
)) .authenticate()
Authenticates the current connection with a previously issued JWT access token, which resumes a session.
try await client.authenticate(token)Arguments
| Argument | Description |
|---|---|
token (required) | The JWT access token to authenticate the connection with. |
Example usage
try await client.authenticate(tokens.access)Alternatively, provide a token when constructing the client through a SessionContext:
let client = try SurrealHTTPClient(
endpoint: "http://localhost:8000",
session: SessionContext(
namespace: "myapp",
database: "mydb",
accessToken: "existing-jwt"
)
) .invalidate()
Invalidates the authentication for the current connection, which clears the current authentication state.
try await client.invalidate()Example usage
try await client.invalidate()