A minimal Nuxt 3 application demonstrating user registration, login, protected routes, and data persistence using RushDB.
Passwords are salted with HMAC-SHA256 (using an environment secret) and stored in RushDB. JWT tokens are set as HTTP-only cookies.
- User Registration: Check username uniqueness, hash passwords, store in RushDB
- User Login: Verify credentials via RushDB, issue JWT token via HTTP-only cookie
- Protected Route:
/userspage showing all registered users stored in RushDB, highlighting the current user - Logout: Clear authentication cookie
- Client + Server: All in one Nuxt 3 project (API routes under
server/api/) - TypeScript + Tailwind CSS
- Node.js ≥ 18
- RushDB API token (from https://app.rushdb.com/)
npmoryarn
git clone https://github.com/rush-db/examples.git
cd examples/nuxt-auth
npm installCopy .env.example to .env and fill in your values:
# .env
PORT=3000
RUSHDB_API_TOKEN=your_rushdb_token
RUSHDB_BASE_URL=https://api.rushdb.com/api/v1
AUTH_SECRET=your_hmac_secretnpm run dev # start in development mode (hot reload)
npm run build # compile for production
npm run start # run production buildnuxt-auth/
├── .env.example
├── package.json
├── nuxt.config.ts
├── tailwind.config.js
├── tsconfig.json
└── src/
├── index.d.ts
├── composables/
│ └── useDb.ts # RushDB client factory
├── middleware/
│ └── auth.client.ts # Route guard for authentication
├── pages/
│ ├── login.vue # Login form
│ ├── register.vue # Registration form
│ ├── users.vue # Protected users list
│ └── [...404].vue # Handle 404 pages
├── server/
│ └── api/
│ ├── login.post.ts
│ ├── logout.post.ts
│ ├── register.post.ts
│ └── users.get.ts
└── shared/
└── models/
└── index.ts # `UserModel` definition
- Body:
{ username, password } - Responses:
200 { success: true }409 Username already exists400 Username & password required
- Body:
{ username, password } - Sets
auth_tokenHTTP-only cookie - Responses:
200 { success: true }401 Invalid credentials400 Username & password required
- Clears
auth_tokencookie - Response:
200 { success: true }
- Requires valid
auth_tokencookie - Returns
{ users: string[], current: string }
/register— registration form/login— login form/users— protected user list (middleware enforces authentication)
Nuxt 3 auto-generates these routes from the pages/ directory.
- UI styled with Tailwind CSS (see
tailwind.config.js)
- Register → check uniqueness → store username + hashed password
- Login → verify password hash → issue JWT in HTTP-only cookie
- Protected Pages → middleware reads cookie (SSR & client) → redirects if missing/invalid
- Logout → cookie max-age=0
- Change hashing algorithm or add salt rounds
- Extend
UserModelinshared/models/user.ts - Adjust JWT expiration or cookie flags in
server/api/login.post.ts
Happy building with RushDB and Nuxt!