Skip to content

Authentication flows (server 3.0) ​

MongoCamp server 3.0 adds opt-in auth flows: registration, invitations, e-mail verification, password reset, login by link or code, and sessions with rotating refresh tokens. Each is a server setting; the server announces only sessions (capabilities feature authSessions), so the module's pages and actions are opt-in too — put a page on its path, set a prop.

Server settingModule piecePath
AUTH_REGISTRATION=openMongocampRegisterpaths.register (/register)
AUTH_REGISTRATION=inviteInvite User of MongocampUsers (invite prop) + MongocampRegister/register?token=
(verification mails of an open registration)MongocampVerifyEmailpaths.verifyEmail (/verify-email)
AUTH_PASSWORD_RESET=trueMongocampResetPassword + forgot-password of MongocampLoginpaths.resetPassword (/reset-password)
AUTH_PASSWORDLESS=truepasswordless of MongocampLogin + MongocampLoginLinkpaths.loginLink (/login-link)
AUTH_SESSIONS=trueSessions card of MongocampAccount—

open and invite exclude each other. AUTH_REQUIRE_VERIFIED_EMAIL=true refuses the login of an unverified address (403, code 4035 — MongocampLogin says so). AUTH_PASSWORD_MIN_LENGTH (default 8) is the minPasswordLength prop of the pages.

The pages are public ​

The server mails links to AUTH_APP_URL + the path (/register?token=…, /verify-email?token=…). Create a page for each flow you switch on, outside the protected routes:

vue
<!-- pages/verify-email.vue -->
<template>
  <MongocampVerifyEmail />
</template>

If your pages live elsewhere, set both: the module option paths and a server AUTH_APP_URL whose links reach them.

Sending the mails ​

The server sends no mail. Every mail is a Nitro hook mongocamp:auth:mail with { kind: 'verify' | 'reset' | 'invite' | 'magic', to, url, token, code?, expiresAt, user? } — the app sends it (at most one mail of a kind per address and minute):

ts
// server/plugins/auth-mail.ts
import { definePlugin } from 'nitro'

export default definePlugin((nitroApp) => {
  nitroApp.hooks.hook('mongocamp:auth:mail', async (mail) => {
    await sendMail({ to: mail.to, subject: mail.kind, text: `${mail.url}${mail.code ? `\n\nCode: ${mail.code}` : ''}` })
  })
})

The hook is not awaited by the request, so a slow mail server does not slow the answer. The playground prints the mails to the console of the dev server instead (playground/server/plugins/playground-auth-mail.ts).

No account enumeration ​

Registration, a new verification link and a password reset answer 202 whether or not the address exists, and the pages say if the address …, a mail is on its way. A used, expired or unknown token is 400, code 4403.

Released under the MIT License.