Authentication

Add user authentication to your apps. Supports email/password, OAuth providers (Google, GitHub, Okta, OIDC), session management, and user administration.

import { getPlatform } from '@maravilla-labs/platform';
const platform = getPlatform();

const session = await platform.auth.login({
  email: 'jane@example.com',
  password: 'securePassword123'
});
// session.access_token — short-lived JWT
// session.refresh_token — single-use refresh token

Quick Start

1. Enable Auth in project settings on Maravilla Cloud.

2. Use hosted pages — every project gets branded login pages at /_auth/login and /_auth/register. No code needed.

Or build your own UI using the platform.auth API:

// SvelteKit example: src/routes/login/+page.server.ts
import { redirect, fail } from '@sveltejs/kit';
import { getPlatform } from '@maravilla-labs/platform';

export const actions = {
  default: async ({ request, cookies }) => {
    const platform = getPlatform();
    const data = await request.formData();

    try {
      const session = await platform.auth.login({
        email: data.get('email'),
        password: data.get('password')
      });
      cookies.set('__session', session.access_token, {
        httpOnly: true, secure: true, sameSite: 'lax', path: '/', maxAge: 60 * 60 * 24 * 30
      });
      throw redirect(303, '/');
    } catch (e) {
      if (e instanceof Response) throw e;
      return fail(401, { error: 'Invalid credentials' });
    }
  }
};

3. Protect routes with withAuth or manual validation:

// Middleware helper — extracts token from Authorization header or __session cookie
export default {
  fetch: platform.auth.withAuth(async (request) => {
    // request.user is guaranteed
    return Response.json({ hello: request.user.email });
  })
};

Hosted Login Pages

Customizable through Auth Settings > Branding:

  • Layouts — Centered, split-left, split-right, fullscreen
  • Theming — Colors, logo, dark mode, Google Fonts, background image/gradient
  • Legal — Terms and privacy policy links

OAuth / OIDC

Configure providers in Auth Settings > OAuth Providers. Hosted login pages automatically show provider buttons.

ProviderSetup
GoogleClient ID + Secret from Google Cloud Console
GitHubClient ID + Secret from GitHub Developer Settings
OktaClient ID + Secret + Issuer URL
Custom OIDCClient ID + Secret + Discovery URL

Keep client_secret values out of maravilla.config.* checked into git. Reference them by name with ${env.VAR} (or { env: "VAR" }) and store the actual value as a project (or org) secret:

maravilla secret set GOOGLE_OAUTH_CLIENT_SECRET --env production

maravilla auth sync resolves ${env.…} placeholders from the project’s vault before pushing the config — see CLI reference › secret.

For custom UIs, start the OAuth flow programmatically:

const { auth_url } = await platform.auth.getOAuthUrl('google');
return Response.redirect(auth_url);
// User authenticates with provider → redirected back with session cookie

When an OAuth email matches an existing account, the user is prompted to confirm linking.

Managed users

A managed user is an account that never logs in. It’s created with no password and a managed status, so login and session minting reject it. Use it to model people your app tracks but who don’t sign in themselves — an imported contact, a service-owned record, or a minor whose guardian acts on their behalf.

// A child account managed by a guardian. No email needed — a non-routable
// synthetic address is generated to satisfy the per-tenant email uniqueness.
const child = await platform.auth.createManagedUser({
  profile: { display_name: 'Sam', is_minor: true },
});

// Or with a real (but still login-less) email.
const contact = await platform.auth.createManagedUser({
  email: 'contact@example.org',
  profile: { source: 'csv-import' },
  groups: ['imported'],          // optional group ids/slugs to add on creation
});

A managed user owns records and can be the target of relations — for example a guardian linked to the child via a STEWARDS edge can then read the child’s data through a policy. If the account later needs to sign in, activate it and set a password through the normal user-management flow.

External IDs

Both register and createManagedUser accept an optional external_id — your own stable key for the user (a CRM id, an SSO subject, an enrollment number). When set, creation is idempotent on (tenant, external_id): calling again with the same key returns the existing user instead of erroring. That makes user creation safe to retry and easy to reconcile against an upstream system.

// Safe to call on every webhook delivery — returns the same user each time.
const user = await platform.auth.createManagedUser({
  external_id: `crm:${crmContactId}`,
  profile: { name: contact.name },
});

external_id is surfaced on the AuthUser record and is null for accounts created without one.

API Reference

Registration & Login

MethodDescription
register({ email, password, profile?, external_id? })Create account. Returns AuthUser. Idempotent on external_id when supplied
login({ email, password })Authenticate. Returns AuthSession
validate(accessToken)Verify token. Returns AuthUser or throws
refresh(refreshToken)New token pair. Returns AuthSession
logout(sessionId)Revoke session
createManagedUser({ email?, profile?, external_id?, groups? })Create a login-less user. Returns AuthUser

OAuth

MethodDescription
getOAuthUrl(provider, options?)Get authorization URL for redirect
handleOAuthCallback(provider, { code, state })Exchange code for session

Email & Password

MethodDescription
sendVerification(userId)Returns { token } — you deliver it
verifyEmail(token)Mark email as verified
sendPasswordReset(email)Returns { token }
resetPassword(token, newPassword)Reset password, invalidate sessions
changePassword(userId, old, new)Change with current password

User Management

MethodDescription
getUser(userId)Get user or null
listUsers(filter?)Paginated list. Filter by status, email_contains, group_id
updateUser(userId, { email?, status?, profile? })Update user
setProfile(userId, data)Write a user’s profile from server code; policies see it on the next request
deleteUser(userId)Delete user and sessions
getFieldConfig()Get configured registration fields

withAuth(handler)

Middleware that validates the token and sets request.user. Returns 401 if unauthenticated.

Types

interface AuthUser {
  id: string;
  email: string;
  email_verified: boolean;
  status: 'active' | 'suspended' | 'deactivated';  // managed users carry "managed"
  provider: string;      // "email", "google", "github", "managed", etc.
  groups: string[];
  external_id?: string | null;   // app-supplied idempotency key, if any
  created_at: number;
  updated_at: number;
  last_login_at?: number;
}

interface AuthSession {
  access_token: string;  // short-lived JWT (default 15 min)
  refresh_token: string; // single-use (default 30 days)
  expires_in: number;
  user: AuthUser;
}

Framework Examples

React Router 7:

// app/routes/login.tsx
export async function action({ request }) {
  const formData = await request.formData();
  const session = await getPlatform().auth.login({
    email: formData.get('email'), password: formData.get('password')
  });
  return redirect('/', {
    headers: { 'Set-Cookie': `__session=${session.access_token}; HttpOnly; Secure; SameSite=Lax; Path=/; Max-Age=2592000` }
  });
}

Nuxt 3:

// server/api/login.post.ts
export default defineEventHandler(async (event) => {
  const { email, password } = await readBody(event);
  const session = await getPlatform().auth.login({ email, password });
  setCookie(event, '__session', session.access_token, {
    httpOnly: true, secure: true, sameSite: 'lax', path: '/', maxAge: 60 * 60 * 24 * 30
  });
  return { user: session.user };
});

Security

  • Passwords are hashed with industry-standard algorithms and never stored in plaintext
  • Tokens are short-lived and automatically rotated on refresh
  • OAuth flows use PKCE and verify provider ID tokens cryptographically
  • Client secrets are encrypted at rest
  • Session limits and password policies are configurable per project
  • Sign-up on the hosted pages is rate limited and screened for automated submissions. You can add a stronger challenge — a proof-of-work puzzle or a CAPTCHA — with one config line; see sign-up bot protection. A sign-up form you build yourself is not covered.

Next Steps

  • Authorization — Decide what signed-in users can do with per-resource policies
  • KV Store — Store session data and user preferences
  • Database — Query and store application data per user
  • Storage — Upload and manage user files
  • Channels — Real-time pub/sub and presence
  • Deployment — Deploy your app with auth enabled