Documentation Index
Fetch the complete documentation index at: https://mintlify.com/nextjs/saas-starter/llms.txt
Use this file to discover all available pages before exploring further.
Overview
The Next.js SaaS Starter uses a JWT-based authentication system with secure session management. Authentication is handled through HTTP-only cookies with bcrypt password hashing for security.Core Components
Session Management
Sessions are managed using JWT tokens stored in HTTP-only cookies. The session implementation is located inlib/auth/session.ts.
Session Data Structure
Key Functions
All session functions are server-side only and should be called from Server Components, Server Actions, or API Routes.
Creating a Session
UsesetSession() to create a new user session after successful authentication:
lib/auth/session.ts
The user object to create a session for. Must include a valid user ID.
Retrieving the Current Session
UsegetSession() to retrieve and verify the current user’s session:
lib/auth/session.ts
SessionData if valid, or null if no session exists or the token is invalid.
Getting the Current User
ThegetUser() function from lib/db/queries.ts retrieves the full user object from the database:
lib/db/queries.ts
Password Security
Password Hashing
Passwords are hashed using bcrypt with 10 salt rounds:lib/auth/session.ts
Password Verification
UsecomparePasswords() to verify a plaintext password against a hashed password:
lib/auth/session.ts
JWT Token Operations
Signing Tokens
Tokens are signed using the HS256 algorithm and expire after 24 hours:lib/auth/session.ts
Verifying Tokens
lib/auth/session.ts
Authentication Flow
Password verification
The system retrieves the user from the database and verifies the password using
comparePasswords().Session creation
If credentials are valid,
setSession() creates a JWT token and sets it as an HTTP-only cookie.Sign In Example
Here’s how the sign-in action uses these authentication utilities:app/(login)/actions.ts
Environment Variables
Secret key used for signing JWT tokens. Must be a strong, random string.
Security Features
- HTTP-Only Cookies: Session tokens are stored in HTTP-only cookies to prevent XSS attacks
- Secure Flag: Cookies are marked as secure in production
- SameSite Protection: Cookies use
laxSameSite policy to prevent CSRF attacks - Password Hashing: Bcrypt with 10 salt rounds
- Token Expiration: Sessions expire after 24 hours
- Soft Deletion: Users are soft-deleted to maintain referential integrity
Related
- Middleware - Learn how sessions are validated on each request
- Teams - Understand team-based access control
- Activity Logs - Track authentication events