Hệ thống quản lý ký túc xá với JWT authentication cho Laravel 12.
- JWT Authentication: Đăng ký, đăng nhập, đăng xuất với JWT token
- Token Management: Refresh token, vô hiệu hóa token
- User Management: Quản lý thông tin người dùng
- API Security: Middleware bảo vệ các route cần xác thực
- Laravel 12: Sử dụng phiên bản Laravel mới nhất
- PHP >= 8.2
- Composer
- MySQL/MariaDB
- Laragon (Windows) hoặc XAMPP/WAMP
- Git
git clone <repository-url>
cd dormitory_managercomposer install# Copy file cấu hình
cp .env.example .env
# Tạo application key
php artisan key:generate
# Tạo JWT secret key
php artisan jwt:secretDB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=dormitory_manager
DB_USERNAME=root
DB_PASSWORD=php artisan migratephp artisan serveServer sẽ chạy tại: http://localhost:8000
- Mở Laragon
- Click vào MySQL → phpMyAdmin
- Tạo database mới:
dormitory_manager - Cập nhật
.envvới thông tin database
- Tạo file
dormitory_manager.conftronglaragon/etc/apache2/sites-enabled/ - Thêm cấu hình:
<VirtualHost *:80>
DocumentRoot "E:/dormitory_manager/public"
ServerName dormitory-manager.test
<Directory "E:/dormitory_manager/public">
AllowOverride All
Require all granted
</Directory>
</VirtualHost>- Thêm vào
C:\Windows\System32\drivers\etc\hosts:
127.0.0.1 dormitory-manager.test
- Restart Laragon
POST /api/auth/register
Content-Type: application/json
{
"name": "John Doe",
"email": "john@example.com",
"password": "password123",
"password_confirmation": "password123"
}POST /api/auth/login
Content-Type: application/json
{
"email": "john@example.com",
"password": "password123"
}GET /api/auth/me
Authorization: Bearer {jwt_token}POST /api/auth/logout
Authorization: Bearer {jwt_token}POST /api/auth/refresh
Authorization: Bearer {jwt_token}GET /api/user
Authorization: Bearer {jwt_token}- Import file
postman_collection.json - Chạy request Register để tạo user mới
- Copy JWT token từ response
- Sử dụng token cho các request khác
curl -X POST http://localhost:8000/api/auth/register \
-H "Content-Type: application/json" \
-d '{
"name": "Test User",
"email": "test@example.com",
"password": "password123",
"password_confirmation": "password123"
}'curl -X POST http://localhost:8000/api/auth/login \
-H "Content-Type: application/json" \
-d '{
"email": "test@example.com",
"password": "password123"
}'curl -X GET http://localhost:8000/api/auth/me \
-H "Authorization: Bearer YOUR_JWT_TOKEN"Mở file public/test-api.html trong browser để test API với giao diện web.
JWT token có 3 phần: header.payload.signature
- Header:
Authorization: Bearer {token} - Expiration: 60 phút (có thể refresh)
- Blacklist: Token bị vô hiệu hóa khi logout
- Login/Register: Tạo token mới
- API Requests: Sử dụng token trong header
- Token Expired: Refresh token hoặc login lại
- Logout: Token bị blacklist
dormitory_manager/
├── app/
│ ├── Http/
│ │ ├── Controllers/
│ │ │ └── AuthController.php # JWT Authentication
│ │ └── Middleware/
│ │ └── JWTMiddleware.php # JWT Middleware
│ └── Models/
│ └── User.php # User model với JWT
├── config/
│ ├── auth.php # Auth config với JWT guard
│ └── jwt.php # JWT configuration
├── routes/
│ └── api.php # API routes
├── bootstrap/
│ └── app.php # Laravel 12 bootstrap
└── public/
└── test-api.html # Test page
# Refresh token
curl -X POST http://localhost:8000/api/auth/refresh \
-H "Authorization: Bearer YOUR_TOKEN"- Kiểm tra format token
- Đảm bảo có
Bearerprefix - Token có thể đã bị blacklist
# Tạo lại JWT secret
php artisan jwt:secret- Kiểm tra Laragon đã start
- Kiểm tra database
dormitory_managerđã tạo - Kiểm tra thông tin trong
.env
# Kiểm tra routes
php artisan route:list -v# Kiểm tra routes
php artisan route:list -v
# Kiểm tra config
php artisan config:show database
php artisan config:show auth
# Clear cache
php artisan config:clear
php artisan route:clear
php artisan cache:clear
# Kiểm tra JWT config
php artisan config:show jwt- Laravel 12 Documentation
- JWT Auth Package
- JWT.io - JWT Token Debugger
- Postman Documentation
- Fork project
- Tạo feature branch (
git checkout -b feature/AmazingFeature) - Commit changes (
git commit -m 'Add some AmazingFeature') - Push to branch (
git push origin feature/AmazingFeature) - Tạo Pull Request
Distributed under the MIT License. See LICENSE for more information.
Nếu gặp vấn đề, hãy tạo issue hoặc liên hệ qua email.
Happy Coding! 🎉