A Laravel 12 RESTful API for user management with JWT authentication, role-based access control, and MySQL database β deployable on Railway with a Vue.js frontend on Vercel.
| Layer | Technology |
|---|---|
| Backend | Laravel 12 (PHP) |
| Database | MySQL |
| Authentication | JWT (tymon/jwt-auth) |
| Session/Cache | Database driver |
| Frontend | Vue.js (separate repo, deployed on Vercel) |
| Deployment | Railway (backend) + Aiven (MySQL) |
- PHP >= 8.2
- Composer
- MySQL >= 8.0
- Node.js (for frontend assets / Vite)
- XAMPP or any local server (for local development)
git clone https://github.com/your-username/user-management-api.git
cd user-management-apicomposer install
npm installcp .env.example .env
php artisan key:generateUpdate your .env with local DB credentials:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=user_management_db
DB_USERNAME=root
DB_PASSWORD=php artisan jwt:secretphp artisan migratephp artisan db:seedphp artisan serveAPI will be available at: http://localhost:8000
This API uses JWT (JSON Web Tokens) for stateless authentication.
| Method | Endpoint | Description |
|---|---|---|
| POST | /api/auth/register |
Register a new user |
| POST | /api/auth/login |
Login and receive JWT token |
| POST | /api/auth/logout |
Logout (invalidate token) |
| GET | /api/auth/me |
Get authenticated user |
Include the token in the Authorization header:
Authorization: Bearer <your_token>
| Method | Endpoint | Description | Role Required |
|---|---|---|---|
| GET | /api/users |
List all users | Admin |
| GET | /api/users/{id} |
Get a single user | Admin / Self |
| PUT | /api/users/{id} |
Update user | Admin / Self |
| DELETE | /api/users/{id} |
Delete user | Admin |
| Variable | Description |
|---|---|
APP_KEY |
Laravel application key |
DB_* |
Database connection settings |
JWT_SECRET |
Secret key for signing JWT tokens |
SESSION_DRIVER |
Set to database |
CACHE_STORE |
Set to database |
QUEUE_CONNECTION |
Set to database |
β οΈ Never commit your.envfile to version control.
- Push your code to GitHub.
- Create a new Railway project and connect your repo.
- Add the following environment variables in Railway dashboard (same as your
.env). - Set the start command:
php artisan migrate --force && php artisan serve --host=0.0.0.0 --port=$PORT- Connect an Aiven MySQL instance and update
DB_*variables with SSL settings:
DB_HOST=<aiven-host>
DB_PORT=<aiven-port>
DB_DATABASE=<your-db>
DB_USERNAME=<your-user>
DB_PASSWORD=<your-password>
MYSQL_ATTR_SSL_CA=/etc/ssl/certs/ca-certificates.crtThe Vue.js frontend is hosted separately on Vercel.
- Frontend Repo: [your-frontend-repo-link]
- API Base URL must be set in the frontend
.env:
VITE_API_BASE_URL=https://your-railway-app.up.railway.app- JWT tokens expire based on
JWT_TTLconfig (default: 60 minutes). - Passwords are hashed using bcrypt with
BCRYPT_ROUNDS=12. - OWASP best practices followed for input validation and error handling.
βββ app/
β βββ Http/Controllers/ # API Controllers
β βββ Models/ # Eloquent Models
β βββ Middleware/ # Auth & Role Middleware
βββ database/
β βββ migrations/ # DB Migrations
β βββ seeders/ # DB Seeders
βββ routes/
β βββ api.php # API Routes
βββ .env.example # Environment template
βββ README.md
This project is open-source and available under the MIT License.
Pial Mahmud Full Stack Web Developer | CSE Student @ Daffodil International University GitHub Β· Portfolio