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.
| Method | Description |
|---|---|
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 |
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.
Access tokens
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.
.signin()
Signs in to the database with CBOR-encoded credentials. On success, the returned token is stored on the client for subsequent requests.
client.signin(credentials_cbor, session)Arguments
| Argument | Description |
|---|---|
credentials_cbor | The credentials, CBOR-encoded as a map, as a List[UInt8]. |
session | An optional session id. |
Example usage
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") .signup()
Creates a new record-access account from CBOR-encoded credentials and signs in.
client.signup(credentials_cbor, session)Arguments
| Argument | Description |
|---|---|
credentials_cbor | The 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. |
session | An optional session id. |
Example usage
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)) .authenticate()
Authenticates the current connection with a token, and stores it on the client for subsequent requests.
client.authenticate(token, session)Arguments
| Argument | Description |
|---|---|
token | The JWT to authenticate with. |
session | An optional session id. |
Example usage
client.authenticate("eyJhbGciOi...") .invalidate()
Invalidates the current session and clears the stored token.
client.invalidate(session)Arguments
| Argument | Description |
|---|---|
session | An optional session id. |
Example usage
client.invalidate()