Skip to main content

Function: emailAuth()

emailAuth(options): Router

Defined in: http-server-auth/src/emailAuth.ts:74

Mounts the email and password credential flows as a Koa Router, adapting the runner-agnostic engine from @ttoss/auth-core. Which routes appear is decided by modes, so an app that only signs users in with a mailed code never exposes a password endpoint.

Every route is a POST, including the link redemptions: the token arrives in the body from the page the user landed on, which keeps it out of server logs and out of the Referer header. The engine also reads a token from the query string, so a GET redemption can be added by an application that wants one.

Mount it before authMiddleware, or exempt its paths — the sign-in routes are what a client calls precisely because it has no token yet.

Parameters

ParameterType
optionsEmailAuthOptions & object

Returns

Router

Example

import { App, bodyParser } from '@ttoss/http-server';
import { emailAuth } from '@ttoss/http-server-auth';

const app = new App();
app.use(bodyParser());
app.use(
emailAuth({
modes: ['emailCode'],
userStore,
oneTimeTokenStore,
issueSession: (user) => issueSession(user),
sendEmail: async ({ to, token }) => {
await ses.send(buildCodeEmail({ to, code: token }));
},
}).routes()
);