feat: add API routes for authentication and task management
- 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.
This commit is contained in:
50
app/Http/Middleware/CheckToken.php
Normal file
50
app/Http/Middleware/CheckToken.php
Normal file
@@ -0,0 +1,50 @@
|
||||
<?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);
|
||||
}
|
||||
}
|
||||
21
app/Http/Middleware/ForceJsonResponse.php
Normal file
21
app/Http/Middleware/ForceJsonResponse.php
Normal file
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use Closure;
|
||||
use Illuminate\Http\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
class ForceJsonResponse
|
||||
{
|
||||
/**
|
||||
* Handle an incoming request.
|
||||
*
|
||||
* @param Closure(Request): (Response) $next
|
||||
*/
|
||||
public function handle(Request $request, Closure $next): Response
|
||||
{
|
||||
$request->headers->set('Accept', 'application/json');
|
||||
return $next($request);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user