/api/auth endpoints including sign-up, sign-in, session handling, password reset, and provider management.
This module is intended for use in server-side code only. For client-side login flows, use the React SDK components or OAuth redirects.
All Auth methods depend on cookies and headers for managing CSRF tokens,
sessions, and callback behavior. For framework-specific integrations (e.g.,
Next.js), some of these flows may be automated.
signUp
Create a new user and start a session using email/password credentials.Parameters
email: required stringpassword: required stringtenantId: optional. Join an existing tenantnewTenantName: optional. If notenantIdis provided, creates a new tenant with this name
Returns
- A
Userobject (with associated tenant info) or a rawResponse
On success, a session token is automatically stored via
withContext() and
used in subsequent calls.signIn
Authenticate an existing user with email/password or via a configured provider.Parameters
provider: either'email'(for credentials) or another supported provider namepayload: either a request or credentials objectrawResponse: optional, whentruereturns aResponse
Returns
- The authenticated
Userobject or aResponse
This method handles both OAuth and email/password sign-in flows. For OAuth
providers, the client should call
/api/auth/signin/{provider}.signOut
Ends the current session.Returns
- A
Responseobject from/api/auth/signout
nile.withContext() resets the current session state.
getSession
Retrieve the current user session.Returns
Either aJWT or an ActiveSession object, depending on the session type:
undefined if no session is active.
getCsrf
Fetches the CSRF token used for protected requests.Returns
AcsrfToken string or a Response if requested
This method is called automatically by
signIn, signUp, and
resetPassword. You typically don’t need to call it manually.listProviders
Retrieve the list of authentication providers configured for the current tenant.Returns
A mapping of provider names to provider configs:forgotPassword
Initiate a password reset request.Parameters
email: required stringcallbackUrl: optional redirect after password resetredirectUrl: optional link shown in the email
Returns
AResponse with password reset instructions
resetPassword
Complete the password reset flow.Parameters
email,password: requiredcallbackUrl,redirectUrl: optional fallback URLs
Returns
AResponse. On success, session headers are automatically applied using withContext().
callback
Handle a callback from an OAuth provider.Parameters
provider: name of the auth providerbody: aRequestor serialized request body
Returns
AResponse with session cookies or error headers
This method is rarely used directly. It powers the internal OAuth flow of
signIn().Multi-factor (Server)
Server-side helpers for enrolling, challenging, and removing MFA in Nile Auth.Features
- Single
auth.mfaentry point for setup, verification, and removal against/auth/mfa - Works with either authenticator apps or email one-time codes
- Reuses the server context (
withContext) so CSRF, session, and callback cookies flow automatically - Returns parsed JSON by default while allowing raw
Responseaccess when needed - Redirect-friendly responses for server-rendered flows (e.g., Next.js middleware)
Installation
mfa
Server entry point for/api/auth/mfa. HTTP verbs map automatically based on params.
Parameters
token: string. MFA challenge token issued during setup or sign-in; required forscope: 'challenge'.scope:'setup' | 'challenge'. Indicates setup vs. completing a challenge. Defaults to'challenge'.method:'authenticator' | 'email'. MFA mechanism to operate on. Defaults to'authenticator'.code: string. Verification or recovery code when completing a challenge.remove: boolean. Whentrue, sendsDELETEto remove the current method. Defaults tofalse.rawResponse: boolean. Whentrue, returns the rawResponseinstead of parsed JSON.
Behavior
POSTto initiate setup,PUTwith atokento verify, andDELETEwhenremoveistrue.- Runs inside
withNileContext, so cookies/CSRF from prior calls are reused automatically. - Redirects surface as
{ url: string }with anerrorquery param; otherwise expect API status codes (400 invalid payload, 401 invalid code/unauthenticated, 403 token mismatch, 404 missing challenge, 410 expired challenge, 500 server error).
Response shapes
- Authenticator setup:
{ method: 'authenticator'; token: string; scope: 'setup' | 'challenge'; otpauthUrl?: string; secret?: string; recoveryKeys?: string[] } - Email setup:
{ method: 'email'; token: string; scope: 'setup' | 'challenge'; maskedEmail?: string } - Challenge/verification:
{ ok: true; scope: 'setup' | 'challenge'; recoveryCodesRemaining?: number } - Redirects/errors:
{ url: string }
Tips
- Persist and reuse the
tokenreturned from setup or sign-in for all follow-up calls. - For authenticated server-rendered pages, call
await nile.auth.getSession()afterauth.mfaso session cookies are hydrated in the current context. - Admin removal flows may return a challenge; route the user to MFA verification with the provided
tokeninstead of bypassing MFA. - Codes are typically 6 digits; recovery codes are string tokens issued during setup.
Utility Methods
These are internal helpers that power the core authentication flows:parseToken
Extracts the session token from headers.parseCallback
Extracts the callback URL cookie from headers.parseResetToken
Extracts the reset token from password reset response.These functions are mostly used internally. You typically don’t need to
call them directly unless customizing the authentication flow.
token |
string | undefined | MFA challenge token issued during setup or sign-in;
required for scope: 'challenge'. | | scope | 'setup' \| 'challenge' |
'challenge' | Indicates whether you are starting setup or completing a
challenge. | | method | 'authenticator' \| 'email' | 'authenticator' | MFA
mechanism to operate on. | | code | string | undefined | Verification or
recovery code when completing a challenge. | | remove | boolean | false |
Sends a DELETE request to remove the currently enrolled method. | |
rawResponse | boolean | false | When true, skip JSON parsing and return
the raw Response. |
Behavior:
- HTTP verb selection follows the API spec:
POSTto initiate setup,PUTwith atokento verify challenges, andDELETEwhenremoveistrue. Ensure you pass the correctscope/token/removecombination so the helper sends the intended verb; if you call the HTTP API directly, use the same verbs described above. - The helper runs inside
withNileContext, so cookies and CSRF headers from prior calls (e.g.,signIn,getCsrf) are reused automatically. - Errors are returned as
{ url: string }with anerrorquery parameter when the upstream responds with a redirect; otherwise expect HTTP status codes surfaced from the API (400 invalid payload, 401 invalid code or unauthenticated, 403 token mismatch, 404 not found, 410 expired challenge, 500 server error).
Response shapes
- Authenticator setup:
{ method: 'authenticator'; token: string; scope: 'setup' \| 'challenge'; otpauthUrl?: string; secret?: string; recoveryKeys?: string[] } - Email setup:
{ method: 'email'; token: string; scope: 'setup' \| 'challenge'; maskedEmail?: string } - Challenge/verification:
{ ok: true; scope: 'setup' \| 'challenge'; recoveryCodesRemaining?: number } - Redirects/errors:
{ url: string }(inspect theerrorsearch param for the reason)
Tips
- Always persist and reuse the
tokenreturned from setup or sign-in; it must be included on all follow-up MFA calls. - For authenticated server-rendered pages, call
await nile.auth.getSession()afterauth.mfato ensure session cookies are hydrated in the current context. - When building admin tooling to disable MFA, expect a challenge response; route the user to your verification UI with the returned
tokeninstead of bypassing MFA. - Authenticator/email codes are expected to be 6 digits; recovery codes are string tokens issued during setup. An expired or missing challenge returns 410/404, and an invalid code returns 401.