Laravel 11 API 后端集成 Sanctum 令牌认证,提供注册、登录、登出和当前用户接口
laravel-react-saas-starter-kit — Laravel 后台模板
作者 kamran1272
基于 Laravel 11 API 与 React 18 的 SaaS 后台启动器,内置 Sanctum 令牌认证、角色权限与用户管理后台。
看看你的网站会是什么样
浏览真实页面截图,比较内容排版与手机效果。点击下方缩略图切换,放大查看整页细节。
目前有 1 张原始预览,可打开完整图片查看设计细节。
截图用于了解视觉与布局,交互效果请以实际项目为准。
值得关注的模板亮点
从已有项目资料中,快速了解它的特点。
通过可复用的 role 中间件实现 admin 与 user 的角色访问控制
管理员专属的用户增删改查接口,带分页、验证和禁止删除自身的保护
React 18 + Vite + Tailwind 前端包含登录注册、受保护路由和仪表盘布局
Axios 客户端自动注入 Bearer 令牌并在 401 时自动登出
附带预置 SQLite 演示数据库和覆盖认证流程的功能测试
模板详细介绍
该项目是一个前后端分离的 SaaS 后台启动器。后端为 Laravel 11 API,使用 Laravel Sanctum 提供令牌认证,包含注册、登录、登出和获取当前用户的接口;通过可复用的 role 中间件实现 admin 与 user 的角色访问控制;提供仅管理员可用的用户增删改查接口,带分页、验证和禁止删除自身的保护;另有仪表盘统计接口,返回总用户数、本周新增用户、管理员数量和活跃令牌数等由数据库计算的数据。API 统一返回 JSON 格式的 401/403/404/422 响应,并附带数据库填充器和覆盖认证流程的功能测试。
前端为 React 18 + Vite + Tailwind CSS,包含登录与注册页面(带逐字段验证错误)、带令牌持久化和会话自动恢复的认证上下文、受保护路由和管理员专属路由、深色侧边栏与顶栏的仪表盘布局(含统计卡片和最近用户表格),以及带模态框、角色管理和分页的管理员用户管理页面。Axios 客户端会自动注入 Bearer 令牌,并在收到 401 时自动登出。
仓库附带预置的 SQLite 演示数据库,后端默认零配置即可运行;也支持 MySQL 或 PostgreSQL。前端可通过设置 VITE_DEMO_MODE 构建为使用内存模拟 API 的演示模式,无需后端即可点击操作。项目采用 MIT 许可,允许免费使用、修改、商用和分发该版本代码,但不得将启动器本身作为独立产品转售;演示图片、字体、图标等第三方素材需单独核对许可,域名、托管、数据库、支付或 AI 等外部服务可能另行收费。
适合你的项目吗?
适用场景
- 快速搭建需要用户认证与角色权限的 SaaS 产品后台
- 为现有 Laravel API 项目补充 React 管理界面
- 作为学习 Laravel Sanctum 令牌认证与 React 路由保护的参考项目
- 在演示模式下无后端预览仪表盘和用户管理交互
适合人群
- 需要 Laravel + React 全栈起步模板的开发者
- 希望复用认证、角色权限和用户管理模块的 SaaS 项目
- 想参考前后端分离后台实现方式的学习者
上手与定制指南
结合仓库当前版本的说明,按下面的资料检查内容、配置与运行要求。
- 1
后端需 PHP 8.2+、Composer 2.x,进入 backend 目录执行 composer install,复制 .env.example 为 .env 并生成应用密钥后启动服务
- 2
前端需 Node.js 18+、npm 9+,进入 frontend 目录执行 npm install,复制 .env.example 为 .env 后运行开发服务器
- 3
仓库自带预置 SQLite 数据库,默认无需运行迁移和填充;仅在需要完全重置数据库时执行 migrate:fresh --seed
- 4
生产环境需移除登录页中的演示凭据提示块,并将 VITE_API_URL 指向实际 API 域名后重新构建
- 5
后端不包含 vendor 目录、前端不包含 node_modules 目录,解压后需分别安装依赖
授权与来源资料
使用条件与限制
免费使用 · 保留许可声明本次收录的是公开仓库中采用 MIT 许可的免费版本;免费范围为该版本代码,不代表外部服务或第三方素材免费。
使用时需要遵守
- 允许免费使用、修改、商用和分发该版本代码。
- 分发代码副本或实质性部分时,保留原作者版权声明和完整 MIT 许可声明。
- 代码按现状提供;担保与责任限制以原许可证及适用法律为准。
额外费用与待核对事项
- 演示图片、字体、图标及其他第三方素材需按各自许可核对,不能仅凭代码的 MIT 许可判断。
- 域名、托管、数据库、支付或 AI 等外部服务可能另外收费。
- MIT 许可允许个人和商业项目使用,但明确禁止将本启动器本身作为独立产品转售。
查看原文依据
Permission is hereby granted, free of charge
许可证原文
https://github.com/kamran1272/laravel-react-saas-starter-kit/blob/main/LICENSEhttps://github.com/kamran1272/laravel-react-saas-starter-kit/blob/main/README.mdMIT — use it in personal and commercial projects. The only thing you may not do is resell this starter kit itself as a standalone product.
README 原文
DeepSeek 根据仓库许可证和 README 整理 ·2026-10-08。结果仅覆盖已读取资料,依赖、图片、字体及后续条款变更以各自原文为准。
项目核对记录与介绍依据
模板资料
- 来源平台
- GitHub
- 资料与维护参考分
- 资料一般 · 73 / 100
- 技术栈
- Laravel · React
- 网站类型
- 后台 · SaaS · 启动器
这个分数如何计算
本站按许可资料(最高30分)、演示链接(12分)、预览图(10分)、Star数(15分)、记录的维护时间(15分)、描述完整度(8分)、功能记录(5分)和分类资料(5分)计算。它不是代码、安全或性能测试,也不是 Google 评分;有链接不代表已实测可用。
这个项目需要核对什么
- 记录的仓库更新时间
- 2026-10-08
- 截图检查日期
- 2026-10-08
目前没有记录演示链接。请依据仓库说明在本地评估;仅凭截图无法确认交互行为。
目录记录的主要技术栈是 Laravel。请通过依赖清单和锁文件核对实际版本与托管要求。这些说明基于收录资料,不代表代码审计或部署实测。
查看具体选型方法 →介绍的制作与核对状态
介绍由 AI 辅助整理目录与仓库资料。仓库作者不等于这篇介绍的审核者。
审核声明: [email protected] · 2026-10-09
查看当时使用的来源资料
SHA-256: 9c747ddff5774e7386f6e3057cdca6b93fbde0a4cb617bb6b094a007aec2fa46
2026-10-08T19:59:43.388Z
来源文件 / 仓库# Laravel + React SaaS Starter Kit A production-grade starter kit for building SaaS products: a **Laravel 11 API backend** (Sanctum token auth, role-based access, users CRUD, dashboard stats) paired with a **React 18 + Vite + Tailwind CSS frontend** (login/register, protected routes, dashboard layout, admin user management). Stop rebuilding auth for every project. Clone, install, and start building your product on day one. **[Live demo](https://kamran1272.github.io/laravel-react-saas-starter-kit/)** — sign in with `[email protected]` / `password`. The demo runs against a real Laravel API (deployed on Railway), not a mock.  --- ## Features **Backend (Laravel 11 API)** - Token authentication with Laravel Sanctum — register, login, logout, "me" endpoints - Role-based access control (`admin` / `user`) via a reusable `role` middleware - Full Users CRUD API (admin only) with pagination, validation, and self-delete protection - Dashboard stats endpoint with real database-computed metrics - JSON-first API: clean 401/403/404/422 JSON responses everywhere (no HTML error pages) - Database seeder with a demo admin + sample users - Feature tests covering the auth flow (10 passing tests included) **Frontend (React 18 + Vite + Tailwind)** - Login / Register pages with per-field validation errors - Auth context with token persistence and automatic session restore - Protected routes + admin-only routes - Dashboard layout: dark sidebar, topbar, 4 stat cards, recent-users table - Admin Users page: full CRUD with modals, role management, pagination - Axios client with Bearer token injection and 401 auto-logout --- ## Requirements | | Version | |---|---| | PHP | 8.2+ | | Composer | 2.x | | Node.js | 18+ | | npm | 9+ | | Database | SQLite (default, zero-config) or MySQL/PostgreSQL | --- ## Quick start ### 1. Backend ```bash cd backend composer install cp .env.example .env php artisan key:generate php artisan serve --port=8000 ``` The API is now live at `http://localhost:8000/api`. > **Do NOT run `php artisan migrate --seed`.** The kit ships with a pre-seeded `database/database.sqlite`, so the demo works out of the box — re-running the seeder is harmless (it only fills in missing demo data), but it won't reset anything. The only time to run `php artisan migrate:fresh --seed` is when you want a **clean reset** of the database. ### 2. Frontend ```bash cd frontend npm install cp .env.example .env # VITE_API_URL defaults to http://localhost:8000 npm run dev ``` Open `http://localhost:5173` and log in with: - **Admin:** `[email protected]` / `password` - Or register a brand-new user account (gets the `user` role automatically) > **Going to production?** Remove the "Demo credentials" hint box from the login page by deleting the `<div className="mt-6 rounded-lg bg-slate-50 px-4 py-3 ...">` block (the one rendering "Demo credentials" / `[email protected]`) in `frontend/src/pages/Login.jsx`. --- ## API reference Base URL: `http://localhost:8000/api` | Method | Endpoint | Auth | Description | |---|---|---|---| | POST | `/auth/register` | — | Register. Body: `name, email, password, password_confirmation` → 201 `{user, token}` | | POST | `/auth/login` | — | Login. Body: `email, password` → 200 `{user, token}` | | POST | `/auth/logout` | Bearer | Revokes the current token → 200 | | GET | `/auth/me` | Bearer | Current user → 200 `{user}` | | GET | `/dashboard/stats` | Bearer | `{total_users, new_users_this_week, admins_count, active_tokens}` | | GET | `/users` | Bearer + admin | Paginated users `{data, meta}` | | POST | `/users` | Bearer + admin | Create user `{name, email, password, role}` → 201 | | GET | `/users/{id}` | Bearer + admin | Single user | | PUT | `/users/{id}` | Bearer + admin | Update user → 200 | | DELETE | `/users/{id}` | Bearer + admin | Delete user → 204 (cannot delete yourself) | User object: `{id, name, email, role, email_verified_at, created_at, updated_at}` --- ## Folder map ``` saas-starter-kit/ ├── README.md ← you are here ├── LICENSE ← MIT license ├── PRODUCT-BRIEF.md ← sales-page copy + pricing suggestions ├── backend/ ← Laravel 11 API │ ├── app/ │ │ ├── Http/ │ │ │ ├── Controllers/Api/ ← AuthController, UserController, DashboardController │ │ │ ├── Middleware/ ← EnsureUserHasRole │ │ │ ├── Requests/ ← Register/Login/StoreUser/UpdateUser validation │ │ │ └── Resources/ ← UserResource (API JSON shape) │ │ └── Models/User.php │ ├── bootstrap/app.php ← middleware alias + JSON exception handling │ ├── config/cors.php ← frontend origin allowed │ ├── database/ │ │ ├── migrations/ ← users table, role column, personal_access_tokens │ │ ├── factories/ ← UserFactory │ │ ├── seeders/ ← demo admin + 8 users │ │ └── database.sqlite ← pre-seeded demo DB (reset with migrate:fresh --seed) │ ├── routes/api.php ← all 10 API routes │ ├── tests/Feature/AuthTest.php ← 10 passing tests │ ├── composer.json / composer.lock │ └── .env.example └── frontend/ ← React 18 + Vite + Tailwind ├── src/ │ ├── api/client.js ← axios instance, token + 401 handling │ ├── context/AuthContext.jsx← login/register/logout/session restore │ ├── components/ ← ProtectedRoute, Layout, StatCard, UsersTable │ └── pages/ ← Login, Register, Dashboard, Users, NotFound ├── index.html ├── tailwind.config.js / postcss.config.js / vite.config.js ├── package.json └── .env.example ← VITE_API_URL ``` --- ## Customizing - **Add a role:** add the role name to the `role` validation rule in `StoreUserRequest`/`UpdateUserRequest` and use `->middleware('role:admin,manager')` on routes. - **Add a resource:** create `Model + migration + Resource + FormRequests + Controller`, then register routes in `routes/api.php` following the `UserController` pattern. - **Change branding:** edit the sidebar logo text in `frontend/src/components/Layout.jsx` and the `<title>` in `frontend/index.html`. - **Point at production:** set `VITE_API_URL` to your API domain and rebuild (`npm run build`); set `APP_URL`/`FRONTEND_URL` in backend `.env`. ## Running tests ```bash cd backend php artisan test ``` ## Environment notes - Never commit `.env` (both apps gitignore it). Copy from `.env.example`. - The backend ships without `vendor/` — run `composer install` after extracting. - The frontend ships without `node_modules/` — run `npm install` after extracting. - CORS is pre-configured for `http://localhost:5173` via `FRONTEND_URL`. ## Demo mode (interactive preview) Build the frontend with `VITE_DEMO_MODE=true` to run it against an in-memory mock API (`frontend/src/api/demoMock.js`) — no backend needed. The preview is fully clickable: sign in with `[email protected]` / `password`, browse the dashboard, and manage the sample users. Used for the public GitHub Pages demo: ```bash cd frontend VITE_DEMO_MODE=true npm run build -- --base=/your-sub-path/ ``` The shipped product defaults to the real Laravel API (`VITE_DEMO_MODE` unset). --- ## License MIT — use it in personal and commercial projects. The only thing you may not do is resell this starter kit itself as a standalone product.
看看同类模板
对比同技术栈与用途的模板,找到更适合的视觉与内容结构。

sage
WordPress starter theme with Laravel Blade components and templates, Tailwind CSS, and block editor support
Laravel · 启动器

wave
Wave - The Software as a Service Starter Kit, designed to help you build the SAAS of your dreams 🚀 💰
Laravel · SaaS

coreui-free-laravel-admin-template
CoreUI Free Laravel Bootstrap Admin Template
Laravel · 后台

sneat-bootstrap-html-laravel-admin-template-free
Most Powerful & Comprehensive Free Bootstrap 5 HTML Laravel Admin Dashboard Template built for developers!
Laravel · 后台

filament-starter
A beautiful starting point for your next TALL stack project using Filament. ⚡
Laravel · 启动器

electrik
Laravel SaaS starter kit: auth, teams, Stripe billing, and Slate 3 — Composer package for Laravel 12
Laravel · SaaS
