Skip to content

Concepts

Authentication

The Mojo SDK authenticates in one of two ways: with an access token supplied on the connection, or with the signin RPC over a stateful WebSocket session.

MethodDescription
client.signin()Signs in to the database with credentials
client.signup()Signs up to a record-access method
client.authenticate()Authenticates the current connection with a token
client.invalidate()Invalidates the authentication for the current connection
Note

signin, signup, authenticate, and invalidate operate on a stateful session, which is provided by the WebSocket transport. WebSocket support is rolling out. Over HTTP, supply credentials with the access_token field on ConnectOptions.

The access_token field on ConnectOptions is placed in the Authorization header. If the value already starts with Bearer , Basic , or Digest , it is passed through verbatim. Otherwise the SDK treats it as a raw JWT and prepends Bearer .

# Basic auth (root user, dev fixtures)
ConnectOptions(access_token=Optional(String("Basic cm9vdDpzZWNyZXQ=")))

# JWT from a previous signin
ConnectOptions(access_token=Optional(String("Bearer eyJhbGciOi...")))

# Raw JWT (the SDK prepends "Bearer ")
ConnectOptions(access_token=Optional(String("eyJhbGciOi...")))

This is the recommended approach for the HTTP and HTTPS transports.

Signs in to the database with CBOR-encoded credentials. On success, the returned token is stored on the client for subsequent requests.

Method Syntax
client.signin(credentials_cbor, session)
ArgumentDescription
credentials_cborThe credentials, CBOR-encoded as a map, as a List[UInt8].
sessionAn optional session id.
from surrealdb import AsyncSurrealClient, CborCodec, ConnectOptions
from std.collections import Optional, List


def main():
    var client = AsyncSurrealClient()
    _ = client.connect(
        "ws://localhost:8000/rpc",
        ConnectOptions(
            namespace=Optional(String("test")),
            database=Optional(String("test")),
        ),
    )

    # Encode the credentials map {"user": "root", "pass": "secret"}.
    var codec = CborCodec()
    var pairs = List[Tuple[String, List[UInt8]]]()
    pairs.append(Tuple(String("user"), codec.encode_text("root")))
    pairs.append(Tuple(String("pass"), codec.encode_text("secret")))
    _ = client.signin(codec.encode_map(pairs))
    client.use("test", "test")

Creates a new record-access account from CBOR-encoded credentials and signs in.

Method Syntax
client.signup(credentials_cbor, session)
ArgumentDescription
credentials_cborThe signup payload, CBOR-encoded as a map, as a List[UInt8]. It names the access method along with the namespace, database, and any variables it expects.
sessionAn optional session id.
from surrealdb import CborCodec
from std.collections import List

var codec = CborCodec()
var pairs = List[Tuple[String, List[UInt8]]]()
pairs.append(Tuple(String("namespace"), codec.encode_text("test")))
pairs.append(Tuple(String("database"), codec.encode_text("test")))
pairs.append(Tuple(String("access"), codec.encode_text("user")))
pairs.append(Tuple(String("email"), codec.encode_text("chiru@example.com")))
pairs.append(Tuple(String("pass"), codec.encode_text("changeme")))

var resp = client.signup(codec.encode_map(pairs))

Authenticates the current connection with a token, and stores it on the client for subsequent requests.

Method Syntax
client.authenticate(token, session)
ArgumentDescription
tokenThe JWT to authenticate with.
sessionAn optional session id.
client.authenticate("eyJhbGciOi...")

Invalidates the current session and clears the stored token.

Method Syntax
client.invalidate(session)
ArgumentDescription
sessionAn optional session id.
client.invalidate()

Was this page helpful?

Last updated