Authentication
ShipKit uses NextAuth.js v5 for secure, flexible authentication with support for multiple providers, session management, and activity logging.
Overview
The authentication system provides:
- Multiple OAuth providers (GitHub, Discord, Google)
- JWT-based session management
- Activity logging with IP tracking
- Type-safe configuration
- Secure cookie handling
- Route protection with middleware
- Enhanced auth function with redirect capabilities
Core Files
The authentication system is organized into several key files:
src/server/auth.ts
- Main authentication setup and enhanced auth function
src/server/auth.config.ts
- Core configuration and callbacks
src/server/auth.providers.ts
- Provider configuration
src/middleware.ts
- Auth middleware for route protection
src/server/services/activity-logger.ts
- Activity logging service
Configuration
Core Setup
OAuth Providers
Session Management
Extended Session Type
Session Callbacks
Route Protection
Middleware
Usage Examples
Protected API Route
Protected Server Component
Client Component with Session
Activity Logging
The authentication system integrates with the activity logger to track auth events:
Security Best Practices
-
Environment Variables
- Store all secrets securely
- Use different OAuth apps for development/production
- Validate environment variables at startup
-
Session Security
- Use secure, HTTP-only cookies
- Implement proper CSRF protection
- Set appropriate cookie expiration
-
Route Protection
- Use middleware for consistent auth checks
- Implement role-based access control
- Handle unauthorized access gracefully
-
Activity Monitoring
- Log all authentication events
- Track IP addresses and user agents
- Monitor for suspicious activity
Notes
- All routes use the new App Router format
- Session strategy is JWT-based for better performance
- Activity logging is non-blocking for better UX
- Route protection is handled by middleware
- Environment variables are strictly typed