- Created ExampleTest and RoutesTest for basic application response and route testing. - Implemented IsolatedDatabaseTestCase for isolated database testing. - Added unit tests for various models and services including Contract, CPE, and LeadDetail. - Developed request validation tests for CreateClientRequest, UpdateClientRequest, and ContractRequest. - Introduced Vite configuration for asset management in the Laravel application.
74 lines
2.7 KiB
PHP
74 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\User;
|
|
use Illuminate\Auth\Events\PasswordReset;
|
|
use Illuminate\Support\Facades\Hash;
|
|
use Illuminate\Support\Facades\Password;
|
|
use Illuminate\Support\Str;
|
|
use Laravel\Sanctum\PersonalAccessToken;
|
|
|
|
class AuthService
|
|
{
|
|
public function authenticate(array $credentials): User
|
|
{
|
|
$user = User::where('email', $credentials['email'])->first();
|
|
abort_unless($user && Hash::check($credentials['password'], $user->password) && $user->can_login !== false, 401, 'Credenciais invalidas.');
|
|
return $user;
|
|
}
|
|
|
|
public function issueToken(User $user): array
|
|
{
|
|
$token = $user->createToken('api-token', ['*'], now()->addMinutes(20));
|
|
return ['token' => $token->plainTextToken, 'user' => $user->only(['id', 'name', 'email'])];
|
|
}
|
|
|
|
public function sessionToken(User $user, string $plainText): ?PersonalAccessToken
|
|
{
|
|
$token = PersonalAccessToken::findToken($plainText);
|
|
if (!$token || $token->tokenable_type !== $user->getMorphClass() || (int) $token->tokenable_id !== (int) $user->id
|
|
|| ($token->expires_at && $token->expires_at->isPast()) || $user->can_login === false) {
|
|
return null;
|
|
}
|
|
return $token;
|
|
}
|
|
|
|
public function refresh(PersonalAccessToken $token): array
|
|
{
|
|
abort_if($token->expires_at && $token->expires_at->isPast(), 401, 'Token expirado.');
|
|
$token->update(['expires_at' => now()->addMinutes(20)]);
|
|
return [
|
|
'message' => 'Token renovado com sucesso',
|
|
'expires_at' => $token->expires_at->toISOString(),
|
|
'user' => $token->tokenable->only(['id', 'name', 'email']),
|
|
];
|
|
}
|
|
|
|
public function check(PersonalAccessToken $token): array
|
|
{
|
|
$valid = !$token->expires_at || $token->expires_at->isFuture();
|
|
$seconds = $valid && $token->expires_at ? max(0, now()->diffInSeconds($token->expires_at, false)) : 0;
|
|
return ['valid' => $valid, 'expires_at' => $token->expires_at?->toISOString(), 'expires_in' => $seconds, 'expires_in_minutes' => ceil($seconds / 60)];
|
|
}
|
|
|
|
public function revoke(?PersonalAccessToken $token): void
|
|
{
|
|
$token?->delete();
|
|
}
|
|
|
|
public function sendResetLink(array $credentials): string
|
|
{
|
|
return Password::sendResetLink($credentials);
|
|
}
|
|
|
|
public function resetPassword(array $credentials): string
|
|
{
|
|
return Password::reset($credentials, function (User $user, string $password) {
|
|
$user->forceFill(['password' => Hash::make($password), 'remember_token' => Str::random(60)])->save();
|
|
$user->tokens()->delete();
|
|
event(new PasswordReset($user));
|
|
});
|
|
}
|
|
}
|