first init
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Auth\Services;
|
||||
|
||||
use App\Services\ActiveRoleService;
|
||||
use App\Support\AuthCookie;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Modules\Auth\Entities\User;
|
||||
use Modules\Auth\Transformers\UserResource;
|
||||
|
||||
class AuthSessionService
|
||||
{
|
||||
public function createAuthResponse(User $user, string $message, int $status = 200): JsonResponse
|
||||
{
|
||||
if (config('app.env') !== 'local') {
|
||||
$user->tokens()->delete();
|
||||
}
|
||||
|
||||
$token = $user->createToken(
|
||||
name: 'authToken',
|
||||
abilities: ['*'],
|
||||
expiresAt: now()->addMinutes((int) config('auth_cookie.lifetime_minutes', 720))
|
||||
);
|
||||
|
||||
$user->load(['roles.permissions']);
|
||||
|
||||
ActiveRoleService::assignDefaultToToken($user, $token->accessToken);
|
||||
|
||||
$payload = [
|
||||
'success' => true,
|
||||
'message' => $message,
|
||||
'data' => [
|
||||
'user' => new UserResource($user),
|
||||
'token' => $token->plainTextToken,
|
||||
'token_type' => 'Bearer',
|
||||
'expires_at' => $token->accessToken->expires_at,
|
||||
],
|
||||
...ActiveRoleService::sessionMeta($user),
|
||||
];
|
||||
|
||||
if (AuthCookie::shouldExposeTokenInResponse()) {
|
||||
$payload['data']['token'] = $token->plainTextToken;
|
||||
}
|
||||
|
||||
$response = response()->json($payload, $status);
|
||||
|
||||
return AuthCookie::attachAuthToken($response, $token->plainTextToken);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Auth\Services;
|
||||
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use Modules\Auth\Entities\EmailVerificationOtp;
|
||||
use Modules\Auth\Entities\User;
|
||||
use Modules\Auth\Emails\EmailVerificationOtpEmail;
|
||||
|
||||
class EmailVerificationOtpService
|
||||
{
|
||||
public function send(User $user): void
|
||||
{
|
||||
if ($user->hasVerifiedEmail()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$otp = $this->generateOtp();
|
||||
|
||||
EmailVerificationOtp::query()
|
||||
->where('user_id', $user->id)
|
||||
->delete();
|
||||
|
||||
EmailVerificationOtp::create([
|
||||
'user_id' => $user->id,
|
||||
'code' => Hash::make($otp),
|
||||
'expires_at' => now()->addMinutes($this->expiryMinutes()),
|
||||
'attempts' => 0,
|
||||
]);
|
||||
|
||||
$user->notify(new EmailVerificationOtpEmail($otp));
|
||||
}
|
||||
|
||||
public function verify(User $user, string $otp): void
|
||||
{
|
||||
if ($user->hasVerifiedEmail()) {
|
||||
throw ValidationException::withMessages([
|
||||
'email' => ['E-mel anda telah disahkan.'],
|
||||
]);
|
||||
}
|
||||
|
||||
$record = EmailVerificationOtp::query()
|
||||
->where('user_id', $user->id)
|
||||
->latest()
|
||||
->first();
|
||||
|
||||
if (! $record) {
|
||||
throw ValidationException::withMessages([
|
||||
'otp' => ['Kod OTP tidak dijumpai. Sila minta kod baharu.'],
|
||||
]);
|
||||
}
|
||||
|
||||
if ($record->isExpired()) {
|
||||
$record->delete();
|
||||
|
||||
throw ValidationException::withMessages([
|
||||
'otp' => ['Kod OTP telah tamat tempoh. Sila minta kod baharu.'],
|
||||
]);
|
||||
}
|
||||
|
||||
if ($record->hasExceededMaxAttempts()) {
|
||||
$record->delete();
|
||||
|
||||
throw ValidationException::withMessages([
|
||||
'otp' => ['Terlalu banyak percubaan. Sila minta kod baharu.'],
|
||||
]);
|
||||
}
|
||||
|
||||
if (! Hash::check($otp, $record->code)) {
|
||||
$record->increment('attempts');
|
||||
|
||||
if ($record->fresh()->hasExceededMaxAttempts()) {
|
||||
$record->delete();
|
||||
}
|
||||
|
||||
throw ValidationException::withMessages([
|
||||
'otp' => ['Kod OTP tidak sah.'],
|
||||
]);
|
||||
}
|
||||
|
||||
$user->forceFill(['email_verified_at' => now()])->save();
|
||||
|
||||
EmailVerificationOtp::query()
|
||||
->where('user_id', $user->id)
|
||||
->delete();
|
||||
}
|
||||
|
||||
protected function generateOtp(): string
|
||||
{
|
||||
return str_pad((string) random_int(0, 999999), 6, '0', STR_PAD_LEFT);
|
||||
}
|
||||
|
||||
protected function expiryMinutes(): int
|
||||
{
|
||||
return (int) config('auth.email_verification.expiry_minutes', 10);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user