Files
alliance-services/app/Services/JwtService.php

50 lines
1.2 KiB
PHP

<?php
namespace App\Services;
use App\Models\User;
use Firebase\JWT\JWT;
use Firebase\JWT\Key;
use Illuminate\Support\Carbon;
use RuntimeException;
class JwtService
{
public function make(User $user): string
{
$secret = config('jwt.secret');
if (! $secret) {
throw new RuntimeException('JWT secret is not configured.');
}
$now = Carbon::now()->timestamp;
$ttl = (int) config('jwt.ttl', 3600);
$payload = [
'iss' => config('jwt.issuer', config('app.name')),
'sub' => (string) $user->id,
'iat' => $now,
'nbf' => $now,
'exp' => $now + $ttl,
// App-specific claims
'character_id' => $user->character_id,
'character_name' => $user->character_name,
'character_owner_hash' => $user->character_owner_hash,
];
return JWT::encode($payload, $secret, 'HS256');
}
public function decode(string $token): object
{
$secret = config('jwt.secret');
if (! $secret) {
throw new RuntimeException('JWT secret is not configured.');
}
return JWT::decode($token, new Key($secret, 'HS256'));
}
}