Skip to content

Concepts

Authentication

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

Signs this connection in to a specific authentication level and returns the issued tokens.

Method Syntax
let tokens = try await client.signin(credentials)
ArgumentDescription
credentials (required)The credentials to authenticate with: one of .root, .namespace, .database, .accessVariables or .accessBearer.

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

Signs a new record-access user up and returns the issued tokens.

Method Syntax
let tokens = try await client.signup(credentials)
ArgumentDescription
credentials (required)The record-access credentials to sign up with, created via .accessRecord.
let tokens = try await client.signup(.accessRecord(
    namespace: "myapp",
    database: "mydb",
    access: "account",
    variables: ["email": .string("new@example.com"), "pass": .string("secret")]
))

Authenticates the current connection with a previously issued JWT access token, which resumes a session.

Method Syntax
try await client.authenticate(token)
ArgumentDescription
token (required)The JWT access token to authenticate the connection with.
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"
    )
)

Invalidates the authentication for the current connection, which clears the current authentication state.

Method Syntax
try await client.invalidate()
try await client.invalidate()

Was this page helpful?

Last updated