forked from admin/services_core
118 lines
3.6 KiB
PHP
118 lines
3.6 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Auth;
|
|
|
|
use App\Models\User;
|
|
use Illuminate\Support\Facades\Hash;
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
class AuthService
|
|
{
|
|
public function register(array $data): array
|
|
{
|
|
$code = $this->generateCode();
|
|
|
|
$user = User::create([
|
|
'name' => $data['name'],
|
|
'username' => $data['username'] ?? null,
|
|
'email' => $data['email'],
|
|
'whatsapp_number' => $data['whatsapp_number'],
|
|
'password' => Hash::make($data['password']),
|
|
'is_master' => false,
|
|
'status' => 'inactive',
|
|
'verification_channel' => $data['verification_channel'],
|
|
'verification_target' => $data['verification_target'],
|
|
'verification_code' => $code,
|
|
'verification_code_expires_at' => now()->addMinutes(10),
|
|
'verified_at' => null,
|
|
]);
|
|
|
|
return [
|
|
'user' => $user,
|
|
'verification' => [
|
|
'channel' => $user->verification_channel,
|
|
'target' => $user->verification_target,
|
|
'code' => $code,
|
|
'expires_at' => $user->verification_code_expires_at,
|
|
],
|
|
];
|
|
}
|
|
|
|
public function verifyRegistration(array $data): User
|
|
{
|
|
$user = User::query()
|
|
->where('verification_channel', $data['verification_channel'])
|
|
->where('verification_target', $data['verification_target'])
|
|
->first();
|
|
|
|
if (!$user) {
|
|
throw ValidationException::withMessages([
|
|
'verification_target' => 'Target verifikasi tidak ditemukan.',
|
|
]);
|
|
}
|
|
|
|
if (empty($user->verification_code) || $user->verification_code !== $data['verification_code']) {
|
|
throw ValidationException::withMessages([
|
|
'verification_code' => 'Kode verifikasi tidak valid.',
|
|
]);
|
|
}
|
|
|
|
if (
|
|
empty($user->verification_code_expires_at) ||
|
|
now()->greaterThan($user->verification_code_expires_at)
|
|
) {
|
|
throw ValidationException::withMessages([
|
|
'verification_code' => 'Kode verifikasi sudah kadaluarsa.',
|
|
]);
|
|
}
|
|
|
|
$user->update([
|
|
'status' => 'active',
|
|
'verified_at' => now(),
|
|
'verification_code' => null,
|
|
'verification_code_expires_at' => null,
|
|
]);
|
|
|
|
return $user->fresh();
|
|
}
|
|
|
|
public function resendVerificationCode(array $data): array
|
|
{
|
|
$user = User::query()
|
|
->where('verification_channel', $data['verification_channel'])
|
|
->where('verification_target', $data['verification_target'])
|
|
->first();
|
|
|
|
if (!$user) {
|
|
throw ValidationException::withMessages([
|
|
'verification_target' => 'Target verifikasi tidak ditemukan.',
|
|
]);
|
|
}
|
|
|
|
if ($user->status === 'active') {
|
|
throw ValidationException::withMessages([
|
|
'user' => 'User sudah aktif dan tidak memerlukan verifikasi ulang.',
|
|
]);
|
|
}
|
|
|
|
$code = $this->generateCode();
|
|
|
|
$user->update([
|
|
'verification_code' => $code,
|
|
'verification_code_expires_at' => now()->addMinutes(10),
|
|
]);
|
|
|
|
return [
|
|
'channel' => $user->verification_channel,
|
|
'target' => $user->verification_target,
|
|
'code' => $code,
|
|
'expires_at' => $user->verification_code_expires_at,
|
|
];
|
|
}
|
|
|
|
protected function generateCode(): string
|
|
{
|
|
return str_pad((string) random_int(0, 999999), 6, '0', STR_PAD_LEFT);
|
|
}
|
|
}
|