A production-ready, scalable Next.js starter template with TypeScript, authentication, role-based access control, and modern UI components.
- Next.js 15+ with App Router
- TypeScript for type safety
- Tailwind CSS for styling
- Dark/Light Mode with next-themes
- Responsive Design mobile-first approach
- JWT-based authentication with access & refresh tokens
- Auto-refresh token mechanism
- Secure token storage
- Dynamic Role-Based Access Control (RBAC)
- Permission checking utilities
- Protected routes and components
- Conditional rendering based on roles/permissions
- Zustand for lightweight, performant state management
- Persistent auth state
- Notification state management
- Axios instance with interceptors
- Automatic token refresh
- Global error handling
- Request/response transformation
- Reusable components (Button, Input, Card, etc.)
- Toast notifications with react-hot-toast
- Theme switcher
- Dashboard layout with sidebar and header
- ESLint configuration
- TypeScript strict mode
- Organized folder structure
- Clean architecture principles
enterprise-nextjs-starter/
βββ app/ # Next.js app directory
β βββ dashboard/ # Dashboard page
β βββ login/ # Login page
β βββ settings/ # Settings page
β βββ unauthorized/ # 403 page
β βββ layout.tsx # Root layout
β βββ page.tsx # Home page
βββ components/ # React components
β βββ auth/ # Auth-related components
β β βββ Can.tsx # Permission-based rendering
β β βββ ProtectedRoute.tsx
β βββ layout/ # Layout components
β β βββ DashboardLayout.tsx
β β βββ Header.tsx
β β βββ Sidebar.tsx
β βββ providers/ # Context providers
β β βββ ThemeProvider.tsx
β β βββ ToastProvider.tsx
β βββ ui/ # Reusable UI components
β βββ Button.tsx
β βββ Card.tsx
β βββ Input.tsx
β βββ ThemeSwitcher.tsx
βββ hooks/ # Custom React hooks
β βββ useAuth.ts # Authentication hook
β βββ usePermission.ts # Permission checking hook
βββ lib/ # Library code
β βββ api/ # API clients
β β βββ auth.ts
β β βββ client.ts # Axios instance with interceptors
β β βββ dashboard.ts
β βββ utils/ # Utility functions
β βββ helpers.ts
β βββ permissions.ts
β βββ token.ts
βββ store/ # Zustand stores
β βββ authStore.ts
β βββ notificationStore.ts
βββ types/ # TypeScript type definitions
β βββ index.ts
βββ config/ # Configuration files
β βββ constants.ts
βββ middleware/ # Next.js middleware (future use)
- Node.js 18+ and npm/yarn
- Backend API running (or configure mock API)
-
Clone and navigate to the project:
cd enterprise-nextjs-starter -
Install dependencies:
npm install
-
Configure environment variables:
cp .env.example .env.local
Update
.env.localwith your backend API URL:NEXT_PUBLIC_API_BASE_URL=http://localhost:8000/api
-
Run the development server:
npm run dev
-
Open your browser: Navigate to http://localhost:3000
npm run build
npm start- User submits credentials on
/login - API returns user data with access and refresh tokens
- Tokens are stored in localStorage
- User is redirected to
/dashboard - Zustand store manages authentication state
- Access Token: Short-lived, sent with every API request
- Refresh Token: Long-lived, used to obtain new access tokens
- Auto-refresh: Tokens are automatically refreshed before expiration
- Interceptors: Axios interceptors handle token injection and refresh
<ProtectedRoute
requiredPermissions={['user:read']}
requiredRoles={['admin']}
>
<YourComponent />
</ProtectedRoute><Can permission="user:create">
<CreateUserButton />
</Can>
<Can permissions={["user:update", "user:delete"]} requireAll={false}>
<UserActions />
</Can>const { hasPermission, hasRole, canPerformAction } = usePermission();
if (hasPermission('user:delete')) {
// Show delete button
}
if (canPerformAction('posts', 'create')) {
// Allow post creation
}Permissions follow the resource:action pattern:
user:read- View usersuser:create- Create usersuser:update- Edit usersuser:delete- Delete usersuser:manage- Full user management
The app supports dark/light mode with automatic system preference detection.
import { ThemeSwitcher } from '@/components/ui/ThemeSwitcher';
<ThemeSwitcher /><div className="bg-white dark:bg-gray-800">
<p className="text-gray-900 dark:text-white">Content</p>
</div>import { getDashboardStats } from '@/lib/api/dashboard';
const stats = await getDashboardStats();import apiClient from '@/lib/api/client';
const response = await apiClient.get('/your-endpoint');
const data = await apiClient.post('/your-endpoint', { data });import toast from 'react-hot-toast';
toast.success('Operation successful!');
toast.error('Something went wrong!');
toast.loading('Processing...');- Create a new folder in
app/directory - Add
page.tsxfile - Wrap with
ProtectedRouteif authentication is required - Use
DashboardLayoutfor consistent layout
- Create component file in appropriate
components/subdirectory - Export from the file
- Use TypeScript for props interface
- Follow existing component patterns
- Define endpoint in
config/constants.ts - Create service function in
lib/api/ - Add TypeScript types in
types/index.ts
- Use Zustand for global state
- Keep component state local when possible
- Persist critical state (auth) to localStorage
- Never store sensitive data in localStorage (only tokens)
- Always validate permissions on both client and server
- Use environment variables for API URLs and secrets
- Use React.memo for expensive components
- Implement lazy loading for large pages
- Optimize images with Next.js Image component
- Group related files together
- Use index files for clean exports
- Follow consistent naming conventions
- Add JSDoc comments for complex functions
npm install -g vercel
vercel# Dockerfile example
FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build
CMD ["npm", "start"]Ensure all NEXT_PUBLIC_* variables are set in your deployment platform.
- Framework: Next.js 15+
- Language: TypeScript
- Styling: Tailwind CSS
- State Management: Zustand
- HTTP Client: Axios
- Notifications: react-hot-toast
- Theme: next-themes
- Fonts: Geist Sans & Mono
- Fork the repository
- Create a feature branch
- Make your changes
- Write tests if applicable
- Submit a pull request
This project is licensed under the MIT License.
For issues and questions:
- Check existing documentation
- Review example implementations
- Consult the backend API documentation
- Add unit tests with Jest
- Implement E2E tests with Playwright
- Add internationalization (i18n)
- Create admin panel
- Add data tables with filtering/sorting
- Implement real-time features with WebSockets
- Add analytics integration
- Create CI/CD pipeline
Built with β€οΈ for scalable enterprise applications