- Created api.php for handling authentication routes (register, login, logout) with token middleware. - Implemented task management routes using API resource controller. feat: add console routes for artisan commands - Added console.php to define an inspiring quote command. feat: add web routes for homepage - Created web.php to serve the welcome view at the root URL. chore: add .gitignore files for storage directories - Added .gitignore files in storage/app, storage/framework, and storage/logs to exclude unnecessary files from version control. test: add feature and unit tests - Created ExampleTest in Feature and Unit directories to verify basic application responses and assertions. build: add Vite configuration for Laravel and Tailwind CSS - Added vite.config.js to configure Vite with Laravel and Tailwind CSS for asset management.
51 lines
1.2 KiB
PHP
51 lines
1.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Middleware;
|
|
|
|
use Closure;
|
|
use Illuminate\Http\Request;
|
|
use Laravel\Sanctum\PersonalAccessToken;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class CheckToken
|
|
{
|
|
public function handle(Request $request, Closure $next)
|
|
{
|
|
$token = $request->bearerToken();
|
|
|
|
// tidak ada token, lanjut
|
|
if (!$token) {
|
|
return $next($request);
|
|
}
|
|
|
|
// sanctum menyimpan token dengan format "id|token"
|
|
$parts = explode('|', $token);
|
|
|
|
// pastikan format token benar
|
|
if (count($parts) !== 2) {
|
|
return $next($request);
|
|
}
|
|
|
|
// cari token berdasarkan id
|
|
$accessToken = PersonalAccessToken::find($parts[0]);
|
|
|
|
// token tidak ditemukan
|
|
if (!$accessToken) {
|
|
return $next($request);
|
|
}
|
|
|
|
// verifikasi hash token
|
|
$hashedToken = hash('sha256', $parts[1]);
|
|
|
|
if (!hash_equals($accessToken->token, $hashedToken)) {
|
|
return $next($request);
|
|
}
|
|
|
|
// token valid, tolak request
|
|
Log::info('CheckToken - Token valid, tolak request');
|
|
return response()->json([
|
|
'message' => 'Anda sudah login, silahkan logout terlebih dahulu!'
|
|
], 403);
|
|
}
|
|
}
|