Skip to main content

Auth

Sign in to an auth collection and manage sessions. To sign up, create a record in the auth collection.

Sign in with a password​

Checks the password and the collection's authRule, then starts a session. Every failure returns the same error, so responses never reveal which emails exist. Ten failures for one account within 15 minutes block it for the rest of that window.

// Bearer token, kept by the SDK's auth store:
await nb.collection("users").signInWithPassword("ann@example.com", "a long password");

// Or an HttpOnly session cookie:
await nb.collection("users").signInWithPassword("ann@example.com", "a long password", {
cookie: true,
});
POST /collections/{collection}/auth-with-password

Body

ParamTypeDescription
identitystringRequired. The email (matched regardless of case), or another identity field.
passwordstringRequired.
cookiebooleanOptional. true sets the session cookie instead of returning the token. The request must then send an Origin header naming this site.

Query parameters: expand and fields, applied to record.

Responses

{
"token": "yK2v0Xo8...43 characters",
"record": {
"collectionId": "c9d8e7f6a5b4c3d",
"collectionName": "users",
"id": "u7v8w9x0y1z2a3b",
"email": "ann@example.com",
"emailVisibility": false,
"verified": false,
"name": "Ann"
}
}

With cookie: true, token is left out and the response sets:

Set-Cookie: nextbase_session=<token>; Path=/; Max-Age=604800; HttpOnly; SameSite=Lax; Secure

Refresh​

Extends the current session to its full lifetime and returns the record, re-checking the authRule. A cookie session gets a renewed cookie and never the token.

await nb.collection("users").refreshSession();
POST /collections/{collection}/auth-refresh

Responses

{ "token": "...", "record": { ... } }, without token for cookie sessions.

Get the current session​

Returns the caller's session and record, or null when signed out.

const session = await nb.getSession();
GET /session

Responses

{
"id": "s1t2u3v4w5x6y7z",
"created": "2026-10-19T09:30:00.000Z",
"expires": "2026-10-26T09:30:00.000Z",
"userAgent": "Mozilla/5.0 ...",
"current": true,
"record": { "collectionName": "users", "id": "u7v8w9x0y1z2a3b", "email": "ann@example.com" }
}

null when signed out.

Sign out​

Ends the current session, so its token stops working, and removes the session cookie.

await nb.signOut();
DELETE /session

Responses: 204 with no body, also when there was no session.

List sessions​

Lists the signed-in record's live sessions, newest first: one per device or client.

const sessions = await nb.listSessions();
GET /sessions

Responses

{
"items": [
{
"id": "s1t2u3v4w5x6y7z",
"created": "2026-10-19T09:30:00.000Z",
"expires": "2026-10-26T09:30:00.000Z",
"userAgent": "Mozilla/5.0 ...",
"current": true
}
]
}

Revoke a session​

Ends one of the signed-in record's sessions, such as a lost device. Revoking the current cookie session also removes the cookie.

await nb.revokeSession("s1t2u3v4w5x6y7z");
DELETE /sessions/{id}

Responses

No body.