Skip to content
Projekte

Über uns

AuthStack Starter: NestJS, Next.js and React Native authentication with OAuth/OIDC, passkeys, MFA, RBAC, and MongoDB or PostgreSQL.

Sicherheit und Datenschutz

So werden diese Zahlen gemessen

What you can do

  • Email and password registration with a 6-digit verification code sent by email
  • Magic link sign-in sent by email
  • Sign-in codes sent by email to native apps
  • Two-factor authentication (TOTP) with single-use recovery codes

Gemessener Sternzuwachs

in 24h
0
in 7 days
0
in 15 days
0
in 1 month
Verlauf wird gesammelt

Nettoänderungen in überlappenden Zeiträumen, keine Tagessummen. Fehlende Messwerte bleiben leer. Sterne zeigen Interesse, nicht Qualität.

README.md

Original

AuthStack Starter

AuthStack Starter provides a TypeScript NestJS 11 API and Next.js 16 web app for authentication, account settings, sessions, roles and permissions.

Choose MongoDB or PostgreSQL before configuring the backend.

The web app uses cookie sessions. React Native clients use rotating access and refresh tokens, with optional device binding. The generator offers an Expo client. The bare React Native shell requires manual setup from the source repository and remains a planned generator target.

Features

  • Email and password registration with a 6-digit verification code sent by email
  • Magic link sign-in sent by email
  • Sign-in codes sent by email to native apps
  • Two-factor authentication (TOTP) with single-use recovery codes
  • Passkey (WebAuthn) registration and authentication
  • OAuth and OIDC providers discovered from backend configuration
  • Dynamic role-based access control with role hierarchy, custom permissions, and direct user overrides
  • Multi-session management with device, browser, OS, and IP tracking
  • Account linking across configured OAuth providers
  • Dynamic runtime auth method discovery via GET /api/auth/methods

Tech stack

Layer Technology
Backend NestJS 11, TypeScript, MongoDB, Mongoose 8
Backend NestJS 11, TypeScript, PostgreSQL 18, Kysely
Frontend Next.js 16 (App Router), React 19, Tailwind CSS 4
State Redux Toolkit (RTK Query)
UI shadcn/ui, Radix UI, Lucide Icons
i18n next-intl
Tests Jest, Vitest

Scaffold a new project

Use create-authstack-starter to select clients, a database, sign-in methods, languages and deployment files. The renamed npm package has not been published yet. Follow CLI setup and options to build and run it from this source repository. Generated projects omit the CLI workspace.

Choose your auth methods

At runtime, the frontend calls GET /api/auth/methods to learn which authentication flows the backend enables. The backend checks its configuration and returns the active providers, credential toggles, magic link availability, passkey support, and two-factor requirements. The frontend renders only the matching forms and buttons.

Method Type Setup guide
Email and password Credential SMTP setup
Magic link Passwordless Magic link setup
Email code Passwordless, native Email code setup
Two-factor (TOTP) Second factor Two-factor setup
Passkeys (WebAuthn) Credential / 2FA Passkey setup
Google OAuth 2.0 Google setup
GitHub OAuth 2.0 GitHub setup
Facebook OAuth 2.0 Facebook setup
Apple OAuth 2.0 / Form post Apple setup
Discord OAuth 2.0 Discord setup
GitLab OIDC GitLab setup
LinkedIn OIDC LinkedIn setup
Microsoft OAuth 2.0 Microsoft setup
Generic OIDC OIDC OIDC setup
Slack OIDC Slack setup
Twitch OIDC Twitch setup
X (Twitter) OAuth 2.0 with PKCE X setup

Quick start

Use Node 22.12 or newer on the 22 line, or Node 24.22 or newer on the 24 line, and pnpm 12.6.0. Node 24.21 and earlier can crash under load in V8's garbage collector (nodejs/node#62393), which 24.22.0 fixes. Node 22 and Node 24 include Corepack. Enable the pinned manager before cloning or scaffolding:

nvm use 22
corepack enable
corepack prepare [email protected] --activate
pnpm --version

Clone the repository:

git clone https://github.com/Mohammed-Abdelhady/authstack-starter.git
cd authstack-starter

Install dependencies from the project folder:

pnpm install

Start with Docker

cp .env.docker.example .env.docker
docker compose --env-file .env.docker up --build

Copy the example configuration files:

cp backend/.env.example backend/.env
cp frontend/.env.example frontend/.env.local

Prepare PostgreSQL

The backend needs PostgreSQL 18 or newer, because new ids come from the uuidv7() function of the server. Create a database, put its address in POSTGRES_URL in backend/.env, and apply the migrations once. The server checks them when it starts and never applies one itself.

Set DATABASE_TYPE=postgres in backend/.env as well. Without it the server runs on MongoDB.

pnpm --filter backend run migration:postgres:up

Start manually

The backend needs MongoDB running as a replica set, because sign-in uses transactions. MONGO_URI in backend/.env expects one named rs0 on port 27017:

mkdir -p ./mongodb-data
mongod --replSet rs0 --dbpath ./mongodb-data

# Leave mongod running. In another terminal, once:
mongosh --eval "rs.initiate()"
# In one terminal, start the backend:
pnpm --filter backend run start:dev

# In another terminal, start the frontend:
pnpm --filter frontend run dev

Endpoints:

Environment configuration

Key backend variables (backend/.env)

Variable Description Default
PORT API server port. Example file sets 5001. 3000 when unset
NODE_ENV Environment name. production turns on the start checks and secure cookies development
MONGO_URI MongoDB address, starting with mongodb:// or mongodb+srv:// None, required
CLIENT_URL Frontend origin for CORS and mailed links. Must be https in production http://localhost:3000
OAUTH_STATE_SECRET Secret used to sign OAuth state cookies, at least 32 characters None, required
AUTH_NATIVE_DPOP_NONCE_SECRET HMAC secret for DPoP nonces; required when AUTH_NATIVE_ENABLED=true At least 32 characters when native sign-in is enabled
API_URL Public API origin used for OAuth callbacks and native sign-in http://localhost:<PORT>. Use https in production
AUTH_NATIVE_DPOP_REQUIRED Reject native exchanges without DPoP and refreshes of unbound families false
TOTP_ENCRYPTION_KEY 32 bytes in base64 that encrypt TOTP secrets. openssl rand -base64 32 makes one None. Two-step verification needs it

| TOTP_ENCRYPTION_KEY_PREVIOUS | Previous 32-byte base64 TOTP key, used during rotation | Unset | | | PASSKEY_CHALLENGE_SECRET | Independent passkey challenge signing key, at least 32 characters | Falls back to the OAuth state secret when unset | | | PASSKEY_CHALLENGE_SECRET_PREVIOUS | Previous passkey challenge key, used during rotation | Unset | | | PASSKEY_CHALLENGE_LEGACY_UNTIL | ISO timestamp ending legacy challenge acceptance, at most five minutes ahead | Unset, legacy challenges refused | |

PostgreSQL variables (backend/.env)

Variable Description Default
DATABASE_TYPE The database the server runs on: mongodb or postgres mongodb
POSTGRES_URL PostgreSQL address, version 18 or newer None, required
POSTGRES_POOL_MAX Connections the server may hold open, from 1 to 200 10

Optional authentication settings

Variables Purpose Example files
OAUTH_GOOGLE_CLIENT_ID, OAUTH_GOOGLE_CLIENT_SECRET, OAUTH_GOOGLE_CALLBACK_URL, NEXT_PUBLIC_GOOGLE_CLIENT_ID Google configuration. See its setup guide. backend/.env.example and frontend/.env.example
OAUTH_GITHUB_CLIENT_ID, OAUTH_GITHUB_CLIENT_SECRET, OAUTH_GITHUB_CALLBACK_URL, NEXT_PUBLIC_GITHUB_CLIENT_ID GitHub configuration. See its setup guide. backend/.env.example and frontend/.env.example
OAUTH_FACEBOOK_CLIENT_ID, OAUTH_FACEBOOK_CLIENT_SECRET, OAUTH_FACEBOOK_CALLBACK_URL, NEXT_PUBLIC_FACEBOOK_APP_ID Facebook configuration. See its setup guide. backend/.env.example and frontend/.env.example
MAGIC_LINK_ENABLED, MAGIC_LINK_EXPIRES_IN, MAGIC_LINK_MAX_PER_HOUR Magic link configuration. See its setup guide. backend/.env.example and frontend/.env.example
EMAIL_CODE_ENABLED, EMAIL_CODE_EXPIRES_IN, EMAIL_CODE_REVIEW_ACCOUNT_ID, EMAIL_CODE_REVIEW_CLIENT_ID, EMAIL_CODE_REVIEW_CODE_HASH, EMAIL_CODE_REVIEW_UNTIL Email code configuration. See its setup guide. backend/.env.example
INVITATION_EXPIRES_IN Invitation lifetime in milliseconds. backend/.env.example
TWO_FACTOR_ENABLED TOTP two-factor configuration. See its setup guide. backend/.env.example and frontend/.env.example
PASSKEYS_ENABLED, WEBAUTHN_RP_ID, WEBAUTHN_RP_NAME, WEBAUTHN_ORIGIN Passkeys configuration. See its setup guide. backend/.env.example and frontend/.env.example
OAUTH_MICROSOFT_CLIENT_ID, OAUTH_MICROSOFT_CLIENT_SECRET, OAUTH_MICROSOFT_CALLBACK_URL, OAUTH_MICROSOFT_TENANT Microsoft configuration. See its setup guide. backend/.env.example and frontend/.env.example
OAUTH_APPLE_CLIENT_ID, OAUTH_APPLE_CALLBACK_URL, OAUTH_APPLE_TEAM_ID, OAUTH_APPLE_KEY_ID, OAUTH_APPLE_PRIVATE_KEY Apple configuration. See its setup guide. backend/.env.example and frontend/.env.example
OAUTH_DISCORD_CLIENT_ID, OAUTH_DISCORD_CLIENT_SECRET, OAUTH_DISCORD_CALLBACK_URL Discord configuration. See its setup guide. backend/.env.example and frontend/.env.example
OAUTH_LINKEDIN_CLIENT_ID, OAUTH_LINKEDIN_CLIENT_SECRET, OAUTH_LINKEDIN_CALLBACK_URL LinkedIn configuration. See its setup guide. backend/.env.example and frontend/.env.example
OAUTH_GITLAB_CLIENT_ID, OAUTH_GITLAB_CLIENT_SECRET, OAUTH_GITLAB_CALLBACK_URL, OAUTH_GITLAB_BASE_URL GitLab configuration. See its setup guide. backend/.env.example and frontend/.env.example
OAUTH_X_CLIENT_ID, OAUTH_X_CLIENT_SECRET, OAUTH_X_CALLBACK_URL X configuration. See its setup guide. backend/.env.example and frontend/.env.example
OAUTH_SLACK_CLIENT_ID, OAUTH_SLACK_CLIENT_SECRET, OAUTH_SLACK_CALLBACK_URL Slack configuration. See its setup guide. backend/.env.example and frontend/.env.example
OAUTH_TWITCH_CLIENT_ID, OAUTH_TWITCH_CLIENT_SECRET, OAUTH_TWITCH_CALLBACK_URL Twitch configuration. See its setup guide. backend/.env.example and frontend/.env.example
OAUTH_OIDC_CLIENT_ID, OAUTH_OIDC_CLIENT_SECRET, OAUTH_OIDC_CALLBACK_URL, OAUTH_OIDC_ISSUER, OAUTH_OIDC_PROVIDER_ID, OAUTH_OIDC_DISPLAY_NAME, OAUTH_OIDC_SCOPES, OAUTH_OIDC_AUTHORIZATION_URL, OAUTH_OIDC_TOKEN_URL, OAUTH_OIDC_USERINFO_URL, OAUTH_OIDC_JWKS_URL Generic OIDC configuration. See its setup guide. backend/.env.example and frontend/.env.example
SMTP_HOST Mail delivery shared by password and magic link sign-in. backend/.env.example
SMTP_PORT Mail delivery shared by password and magic link sign-in. backend/.env.example
SMTP_SECURE Mail delivery shared by password and magic link sign-in. backend/.env.example
SMTP_USER Mail delivery shared by password and magic link sign-in. backend/.env.example
SMTP_PASS Mail delivery shared by password and magic link sign-in. backend/.env.example
EMAIL_FROM Mail delivery shared by password and magic link sign-in. backend/.env.example

Key frontend variables (frontend/.env.local)

Variable Description Default
NEXT_PUBLIC_API_URL Backend origin without /api suffix. Set it to the address the backend runs on Port 5000 on localhost when unset

See docs/README.md for provider-specific credentials and mail settings. See production security settings for what a production start refuses.

For a host-run backend with MongoDB in Docker Compose, use MONGO_URI=mongodb://USER:PASS@localhost:27017/authboiler?authSource=admin&directConnection=true. The replica set advertises mongodb:27017, which only containers on the Compose network can resolve.

For a host-run backend with PostgreSQL in Docker Compose, use POSTGRES_URL=postgres://USER:PASS@localhost:5432/authboiler. Compose publishes the database on port 5432.

Mobile app

The Expo app in mobile/expo shares a sign-in engine and screens with the bare React Native shell. It supports password, email links, signup, recovery, passkeys and MFA when enabled by the backend and registered application. Configured providers use native SDKs or system OAuth. The app also offers web sign-in, profile display and session controls. See mobile sign-in setup for provider registration, callbacks and app links.

The template's Expo app, under its example name and ids, has been built and run on an iOS simulator. A generated app with its own name and ids has been linted, type checked, unit tested and bundled, but not launched. Android has not been built or run as a native app; device behavior remains unverified. Provider credentials, passkeys and app links need verification on your registered devices.

What the server needs

Set these in backend/.env before you start the backend:

  • AUTH_NATIVE_ENABLED=true
  • AUTH_NATIVE_DPOP_NONCE_SECRET with a random value of at least 32 characters. openssl rand -hex 32 makes one.
  • API_URL with the address the app uses to reach the API. On a simulator that is http://localhost:5001.
  • AUTH_NATIVE_APPLICATIONS with the client ID and callback matching the scheme in mobile/expo/app.json. Add the application's signIn permissions for native methods as described in the mobile setup guide; the generated example registers its identity and callback only.

If you start the server with Docker, set the same four in .env.docker instead. Docker serves the API on port 5000, so API_URL is http://localhost:5000 there.

The backend does not start with AUTH_NATIVE_ENABLED=true until the secret and API_URL are set.

Keep the web app running for the web sign-in option. Native credential and configured SDK flows talk to the API directly. You need an existing account or an enabled registration method; seed data creates two development accounts.

Run it on an iOS simulator

You need a Mac with Xcode and CocoaPods. Expo creates the native iOS project the first time you run the app.

From a fresh folder, in this order:

pnpm install
cp backend/.env.example backend/.env
cp frontend/.env.example frontend/.env.local

# Edit backend/.env as described above. Start MongoDB, then in one terminal:
pnpm --filter backend run start:dev

# In a second terminal:
pnpm --filter frontend run dev

# In a third terminal, once, to make the two seed accounts:
pnpm --filter backend run seed

# Then build the app and open it on a simulator:
pnpm --filter @app/mobile-expo run ios

The last command builds the app, installs it and keeps running to serve the JavaScript. Leave it open while you use the app. If the simulator asks whether to open the page in your app, choose Open.

After the first build, pnpm --filter @app/mobile-expo run start serves the JavaScript without building again. Open the app from the simulator's home screen.

A development build talks to http://localhost:5001. Set EXPO_PUBLIC_API_ORIGIN to use another address. A release build does not start without it.

With Docker the API is on port 5000, so run the app like this:

EXPO_PUBLIC_API_ORIGIN=http://localhost:5000 pnpm --filter @app/mobile-expo run ios

The app name, slug, application ID and scheme come from mobile/expo/app.json. Rebuild after changing native provider settings or capabilities. Device signing and store registration require your own configuration.

Seed data

Run the database seed script from the project root:

pnpm --filter backend run seed

The script runs only when NODE_ENV is development or test. It creates four system roles (user, support, manager, admin) and two seed accounts:

Email Default role Password
[email protected] admin Generated random base64 string printed to stdout
[email protected] user Generated random base64 string printed to stdout

To set explicit passwords for seed accounts, set SEED_ADMIN_PASSWORD and SEED_USER_PASSWORD in backend/.env before running the command.

To wipe and reseed the database:

pnpm --filter backend run seed:reset

Documentation

Guide Description
Documentation index Setup guides, workspace docs and operations references
Architecture Request lifecycle, session storage, OAuth registry, and security model
Database schema MongoDB collections, indexes, and migrations
PostgreSQL Version, settings, migrations, retention, and how the tests run
RBAC system Role levels, permission inheritance, and enforcement guards
Migration guide Database migration scripts and versioning
Production setup Domain configuration, SSL termination, and Nginx reverse proxy
Code quality Linting, formatting, git hooks, and E2E tests
Deployment Docker Compose and cloud deployment guides
Backend guide Backend modules, controllers, and configuration schema
Frontend guide Frontend routing, components, and internationalization

Contributing

  1. Create a feature branch: git checkout -b feat/feature-name
  2. Follow Conventional Commits format for commit messages.
  3. Run pnpm run lint, pnpm run typecheck, pnpm run test and pnpm run build with the supported Node version.
  4. Submit a pull request.

License

MIT

Ähnliche Projekte entdecken

Wird geladen…