JavaScript SDK (@auris/js)
@auris/js v0.1.0@auris/js is the foundation SDK for Auris. It is a zero-dependency library that works in browsers, Node.js 18+, and edge runtimes (Cloudflare Workers, Vercel Edge, Deno). Both ESM (import) and CJS (require) builds are included, along with full TypeScript declarations.
The React and Next.js SDKs are thin wrappers around @auris/js. If you are building a framework-agnostic application or need the lowest-level API, use this package directly.
Installation
npm install @auris/jspnpm add @auris/jsyarn add @auris/jsAurisClient
AurisClient is the main entry point. Create one instance per application.
Constructor
import { AurisClient } from '@auris/js'
const auris = new AurisClient({
domain: 'auth.yourdomain.com', // Your Auris tenant domain (required)
clientId: 'app_xxxxx', // Application Client ID from the Console (required)
redirectUri: 'http://localhost:3000/callback', // Must match a registered Callback URL (required for PKCE flows)
tenant: 'my-tenant', // Tenant identifier sent as x-tenant header (optional, defaults to 'default')
storage: 'localStorage', // Token storage: 'localStorage' | 'sessionStorage' | 'memory' | TokenStore instance
autoRefresh: true, // Automatically refresh access tokens before expiry (default: true)
scope: 'openid profile email', // OAuth2 scopes to request (default: 'openid profile email')
})Configuration options:
In edge runtimes and Node.js environments without localStorage, the SDK falls back to in-memory storage automatically. Pass storage: 'memory' explicitly if you want to opt into this behavior in browser environments.
Authentication Methods
loginWithRedirect(options?)
Initiates the OAuth2 Authorization Code flow with PKCE. Generates a code verifier and challenge, stores the verifier, then redirects the browser to the Auris hosted login page.
Signature:
loginWithRedirect(options?: LoginWithRedirectOptions): Promise<void>Parameters:
// Basic login redirect
await auris.loginWithRedirect()
// Open the signup screen
await auris.loginWithRedirect({ screen_hint: 'signup' })
// Pre-fill the email and force re-authentication
await auris.loginWithRedirect({ login_hint: '[email protected]', prompt: 'login' })handleRedirectCallback()
Completes the OAuth2 PKCE flow after the user returns to your application. Reads the code and state from the current URL, validates the state, exchanges the code for tokens, and stores the result.
Call this method exactly once on your callback page when it first loads.
Signature:
handleRedirectCallback(): Promise<AuthResult>Returns: AuthResult
const result = await auris.handleRedirectCallback()
if (result.user) {
console.log('Signed in as', result.user.email)
window.location.href = '/dashboard'
}login(email, password)
Direct email/password authentication without a redirect. Returns tokens immediately.
Signature:
login(email: string, password: string): Promise<AuthResult>Direct login sends credentials to your application server. It is not recommended for browser applications because it bypasses the security benefits of the hosted login page (CAPTCHA, rate limiting, suspicious login detection, MFA). Use loginWithRedirect() for end-user authentication in browser apps.
const result = await auris.login('[email protected]', 'hunter2')
console.log(result.user.id)signup(email, password, options?)
Register a new user and return tokens immediately.
Signature:
signup(email: string, password: string, options?: SignupOptions): Promise<AuthResult>Options:
const result = await auris.signup('[email protected]', 'securepassword', {
firstName: 'Bob',
lastName: 'Smith',
})logout(options?)
Revokes the refresh token, clears stored tokens, and optionally redirects the browser.
Signature:
logout(options?: LogoutOptions): Promise<void>Options:
// Clear tokens and redirect to the home page
await auris.logout({ returnTo: 'http://localhost:3000' })
// Clear tokens without redirecting (e.g., in a Node.js context)
await auris.logout()getUser()
Returns the currently authenticated user from the stored ID token. Returns null if the user is not authenticated.
Signature:
getUser(): Promise<UserInfo | null>Returns: UserInfo | null
const user = await auris.getUser()
if (user) {
console.log(user.id) // Auris user ID
console.log(user.email)
console.log(user.roles) // string[] — assigned roles
console.log(user.metadata) // Record<string, unknown>
}isAuthenticated()
Returns true if the user has a valid (non-expired) access token.
Signature:
isAuthenticated(): Promise<boolean>if (await auris.isAuthenticated()) {
// Proceed with authenticated operations
}getAccessToken()
Returns the current access token. If autoRefresh is enabled and the token is expired, it automatically exchanges the refresh token for a new access token before returning.
Signature:
getAccessToken(): Promise<string | null>const token = await auris.getAccessToken()
// Use the token in API requests
const response = await fetch('/api/data', {
headers: { Authorization: `Bearer ${token}` },
})refreshToken()
Manually exchanges the stored refresh token for a new access token and refresh token pair.
Signature:
refreshToken(): Promise<AuthResult>const result = await auris.refreshToken()
console.log(result.accessToken)loginWithMagicLink(email)
Triggers a magic link email to the specified address. The user clicks the link in the email to complete authentication via verifyMagicLink().
Signature:
loginWithMagicLink(email: string): Promise<void>await auris.loginWithMagicLink('[email protected]')
// Redirect or show a "Check your email" messageverifyMagicLink(token)
Verifies a magic link token (extracted from the URL the user clicked). Returns tokens and user info on success.
Signature:
verifyMagicLink(token: string): Promise<AuthResult>const urlParams = new URLSearchParams(window.location.search)
const token = urlParams.get('token')
if (token) {
const result = await auris.verifyMagicLink(token)
console.log('Authenticated:', result.user)
}forgotPassword(email)
Sends a password reset email to the specified address.
Signature:
forgotPassword(email: string): Promise<void>await auris.forgotPassword('[email protected]')loginWithSocial(provider)
Redirects the user to the specified social identity provider. Auris handles the provider OAuth flow and returns the user to your redirectUri with an authorization code.
Signature:
loginWithSocial(provider: SocialProvider): Promise<void>Supported providers:
type SocialProvider =
| 'google'
| 'github'
| 'microsoft'
| 'apple'
| 'facebook'
| 'discord'
| 'linkedin'
| 'twitter'
| 'slack'await auris.loginWithSocial('google')
await auris.loginWithSocial('github')handleCallback()
Handles the redirect back from a social login provider. Identical to handleRedirectCallback() — both methods are aliases.
Signature:
handleCallback(): Promise<AuthResult>getM2MToken(clientSecret, scopes?)
Obtains a machine-to-machine access token using the OAuth2 client_credentials grant. Use this for server-to-server API calls where no user is involved.
Signature:
getM2MToken(clientSecret: string, scopes?: string[]): Promise<M2MTokenResult>const result = await auris.getM2MToken('cs_live_xxxxx', ['read:users', 'manage:roles'])
console.log(result.accessToken)
console.log(result.expiresIn) // seconds until expiryNever use getM2MToken() in browser code. The clientSecret must be kept server-side only. Use this method in Node.js API routes, background workers, and CLI scripts.
onAuthStateChange(callback)
Subscribes to authentication state changes. The callback fires whenever the user logs in, logs out, or their token is refreshed.
Signature:
onAuthStateChange(callback: (user: UserInfo | null) => void): UnsubscribeReturns: Unsubscribe — call it to remove the listener.
const unsubscribe = auris.onAuthStateChange((user) => {
if (user) {
console.log('Signed in:', user.email)
} else {
console.log('Signed out')
}
})
// Later, when you no longer need the listener:
unsubscribe()Permissions Module
Access via auris.permissions. All methods call the Auris API and require the user to be authenticated.
check(permissions)
Check whether the current user has any or all of the specified permissions.
Signature:
check(permissions: string[]): Promise<PermissionCheckResult>Returns: PermissionCheckResult
interface PermissionCheckResult {
has: (permission: string) => boolean
hasAll: boolean // true if user has every permission in the array
hasAny: boolean // true if user has at least one permission in the array
results: Record<string, boolean> // per-permission result map
}const result = await auris.permissions.check(['manage:users', 'view:reports'])
if (result.has('manage:users')) {
// Show admin UI
}
if (result.hasAll) {
// User has both permissions
}checkOne(permission)
Check a single permission. Returns true if allowed, false otherwise.
Signature:
checkOne(permission: string): Promise<boolean>const canDelete = await auris.permissions.checkOne('delete:documents')
if (canDelete) {
showDeleteButton()
}listUserPermissions()
Returns the full list of permission strings assigned to the current user across all their roles.
Signature:
listUserPermissions(): Promise<string[]>const permissions = await auris.permissions.listUserPermissions()
console.log(permissions) // ['view:documents', 'create:documents', 'manage:users', ...]FGA Module
Access via auris.fga. Implements Zanzibar-style Fine-Grained Authorization. Requires the FGA feature to be enabled on your tenant.
check(input)
Check whether a subject has a relation to an object.
Signature:
check(input: FgaCheckInput): Promise<FgaCheckResult>interface FgaCheckInput {
objectType: string // e.g. 'document'
objectId: string // e.g. 'doc_abc123'
relation: string // e.g. 'viewer'
subjectType: string // e.g. 'user'
subjectId: string // e.g. the current user's ID
}
interface FgaCheckResult {
allowed: boolean
resolution?: ResolutionNode[] // explain mode trace
}const result = await auris.fga.check({
objectType: 'document',
objectId: 'doc_abc123',
relation: 'editor',
subjectType: 'user',
subjectId: user.id,
})
if (result.allowed) {
enableEditMode()
}expand(input)
Expand a relation to list all subjects (users or groups) that have it on a given object.
Signature:
expand(input: FgaExpandInput): Promise<ExpandTree>const tree = await auris.fga.expand({
objectType: 'project',
objectId: 'proj_xyz',
relation: 'member',
})
console.log(tree)listObjects(input)
List all object IDs of a given type on which the subject has the specified relation.
Signature:
listObjects(input: FgaListObjectsInput): Promise<string[]>// List all documents the current user can view
const documentIds = await auris.fga.listObjects({
objectType: 'document',
relation: 'viewer',
subjectType: 'user',
subjectId: user.id,
})writeTuples(tuples)
Write one or more relationship tuples to the FGA store.
Signature:
writeTuples(tuples: WriteTupleInput[]): Promise<void>await auris.fga.writeTuples([
{
objectType: 'document',
objectId: 'doc_abc123',
relation: 'editor',
subjectType: 'user',
subjectId: 'usr_456',
},
])deleteTuples(tuples)
Delete one or more relationship tuples from the FGA store.
Signature:
deleteTuples(tuples: WriteTupleInput[]): Promise<void>await auris.fga.deleteTuples([
{
objectType: 'document',
objectId: 'doc_abc123',
relation: 'editor',
subjectType: 'user',
subjectId: 'usr_456',
},
])Management Client
createManagementClient(config) creates a Management API client that authenticates with M2M client_credentials. Use it in backend code (Node.js scripts, API routes, CLI tools) to manage your tenant programmatically.
Signature:
import { createManagementClient } from '@auris/js'
const mgmt = await createManagementClient({
domain: 'auth.yourdomain.com',
clientId: 'app_xxxxx',
clientSecret: 'cs_live_xxxxx',
tenant: 'my-tenant',
})Users
// List users with pagination
const { users, total } = await mgmt.users.list({ page: 1, limit: 50 })
// Get a single user by ID
const user = await mgmt.users.get('usr_abc123')
// Create a user
const newUser = await mgmt.users.create({
email: '[email protected]',
password: 'temporary-password',
firstName: 'Alice',
roles: ['editor'],
})
// Update a user
await mgmt.users.update('usr_abc123', { firstName: 'Alicia' })
// Disable a user (soft-disable — they cannot log in)
await mgmt.users.update('usr_abc123', { enabled: false })
// Delete a user
await mgmt.users.delete('usr_abc123')Organizations
const orgs = await mgmt.organizations.list()
const org = await mgmt.organizations.get('org_xyz')
const newOrg = await mgmt.organizations.create({ name: 'Acme Corp', slug: 'acme' })
await mgmt.organizations.update('org_xyz', { name: 'Acme Corporation' })
await mgmt.organizations.delete('org_xyz')Roles
const roles = await mgmt.roles.list()
const role = await mgmt.roles.get('role_abc')
const newRole = await mgmt.roles.create({ name: 'Editor', description: 'Can edit content' })
await mgmt.roles.update('role_abc', { description: 'Can view and edit content' })
await mgmt.roles.delete('role_abc')Webhook Verification
verifyWebhookSignature(options) verifies that an incoming webhook was sent by Auris and has not been tampered with. Uses HMAC-SHA256.
Signature:
import { verifyWebhookSignature } from '@auris/js'
const isValid = await verifyWebhookSignature({
payload: rawBody, // string or Buffer — the raw request body
signature: headerValue, // value of the X-Webhook-Signature header
secret: 'whsec_xxxxx', // signing secret from the Console
timestamp: headerTs, // value of the X-Webhook-Timestamp header
maxAge: 300, // optional: reject webhooks older than N seconds (default: 300)
})Node.js example (Express):
import express from 'express'
import { verifyWebhookSignature } from '@auris/js'
const app = express()
app.post('/webhooks/auris', express.raw({ type: 'application/json' }), async (req, res) => {
const isValid = await verifyWebhookSignature({
payload: req.body.toString('utf-8'),
signature: req.headers['x-webhook-signature'],
secret: process.env.AURIS_WEBHOOK_SECRET,
timestamp: req.headers['x-webhook-timestamp'],
})
if (!isValid) {
return res.status(401).json({ error: 'Invalid signature' })
}
const event = JSON.parse(req.body)
console.log('Received event:', event.type)
res.json({ received: true })
})JWT Verification
verifyJwt(token, jwksUrl) verifies an Auris-issued access token using the public keys from your JWKS endpoint. Zero-dependency — uses the Web Crypto API in browsers and Node.js 18+.
Signature:
import { verifyJwt } from '@auris/js'
const payload = await verifyJwt(token, 'https://auth.yourdomain.com/.well-known/jwks.json')The function returns the decoded JWT payload if the signature is valid, or throws if the token is expired, malformed, or signed with an unknown key. Key fetching results are cached for 1 hour.
import { verifyJwt } from '@auris/js'
try {
const payload = await verifyJwt(
accessToken,
`https://${process.env.AURIS_DOMAIN}/.well-known/jwks.json`
)
console.log('User ID:', payload.sub)
console.log('Roles:', payload.roles)
} catch (err) {
console.error('Token invalid:', err.message)
}TypeScript Types
Key types exported from @auris/js:
// Authentication results
interface AuthResult {
user: UserInfo
accessToken: string
refreshToken?: string
expiresIn: number
tokenType: 'Bearer'
}
// User information
interface UserInfo {
id: string
sub: string
email: string
emailVerified: boolean
name: string
firstName: string
lastName: string
username: string
picture?: string
roles: string[]
metadata: Record<string, unknown>
tenant: string
createdAt: string
}
// M2M token result
interface M2MTokenResult {
accessToken: string
expiresIn: number
tokenType: 'Bearer'
scope: string
}
// Permission check result
interface PermissionCheckResult {
has: (permission: string) => boolean
hasAll: boolean
hasAny: boolean
results: Record<string, boolean>
}
// FGA types
interface FgaCheckInput {
objectType: string
objectId: string
relation: string
subjectType: string
subjectId: string
subjectRelation?: string
}
interface FgaCheckResult {
allowed: boolean
resolution?: ResolutionNode[]
}
// Storage adapter interface
interface TokenStore {
get(key: string): string | null | Promise<string | null>
set(key: string, value: string): void | Promise<void>
remove(key: string): void | Promise<void>
}
// Unsubscribe function from onAuthStateChange
type Unsubscribe = () => voidCustom Storage Adapter
Implement the TokenStore interface to persist tokens in any storage backend.
import { AurisClient, TokenStore } from '@auris/js'
class RedisTokenStore implements TokenStore {
constructor(private redis: RedisClient, private prefix = 'auris:') {}
async get(key: string) {
return this.redis.get(this.prefix + key)
}
async set(key: string, value: string) {
await this.redis.set(this.prefix + key, value, { EX: 60 * 60 * 24 })
}
async remove(key: string) {
await this.redis.del(this.prefix + key)
}
}
const auris = new AurisClient({
domain: 'auth.yourdomain.com',
clientId: 'app_xxxxx',
storage: new RedisTokenStore(redisClient),
})Error Handling
All async methods throw an AurisError on failure.
import { AurisClient, AurisError } from '@auris/js'
try {
await auris.login('[email protected]', 'wrongpassword')
} catch (err) {
if (err instanceof AurisError) {
console.error(err.code) // e.g. 'invalid_credentials', 'account_locked', 'mfa_required'
console.error(err.message) // Human-readable message
console.error(err.status) // HTTP status code (e.g. 401, 403, 429)
}
}Common error codes:
Related Pages
- React SDK — React context, hooks, and components built on @auris/js
- Next.js SDK — Server-side helpers and Edge Middleware for Next.js
- Hosted Login Guide — Complete PKCE flow walkthrough
- Fine-Grained Authorization — FGA model and tuple management
- Webhooks — Webhook delivery and HMAC verification