Feature/phone register (#11)

Co-authored-by: ISMAIL MASSERAN <topaz@ISMAILs-Macbook.local>
Co-authored-by: ISMAIL MASSERAN <topaz@Mac.dlinkrouter.local>
Reviewed-on: #11
This commit was merged in pull request #11.
This commit is contained in:
2026-07-14 12:03:22 +08:00
parent 1e50e3d19f
commit b05e074456
160 changed files with 6497 additions and 759 deletions
@@ -10,23 +10,25 @@ use Illuminate\Support\Str;
use Illuminate\Validation\Rule;
use Laravel\Fortify\Contracts\CreatesNewUsers;
use Modules\Auth\Entities\User;
use Modules\Auth\Services\PhoneVerificationOtpService;
use Modules\Role\Entities\Role;
use Modules\User\Notifications\UserActivationNotification;
use Modules\Auth\Services\EmailVerificationOtpService;
use Modules\User\Policies\UserPolicy;
use Exception;
class CreateNewUser implements CreatesNewUsers
{
use PasswordValidationRules, NotifiesAdmins;
/**
* Validate and create a newly registered user.
*
* @param array<string, string> $input
*/
public function __construct(
protected PhoneVerificationOtpService $phoneVerificationOtpService,
) {}
public function create(array $input): User
{
Validator::make($input, [
$phoneNumber = $this->phoneVerificationOtpService->normalizePhoneNumber($input['phone_number'] ?? '');
Validator::make(array_merge($input, ['phone_number' => $phoneNumber]), [
'name' => ['required', 'string', 'max:255'],
'email' => [
'required',
@@ -36,15 +38,32 @@ class CreateNewUser implements CreatesNewUsers
Rule::unique(User::class),
],
'ic_number' => ['required', 'string', 'max:255'],
'phone_number' => [
...$this->phoneVerificationOtpService->phoneNumberRules(),
Rule::unique(User::class),
],
'phone_verification_token' => ['required', 'string', 'size:64'],
'password' => ['required', 'string', 'min:8'],
], [
'phone_number.required' => 'Nombor telefon diperlukan.',
'phone_number.regex' => 'Format nombor telefon tidak sah.',
'phone_number.unique' => 'Nombor telefon ini telah didaftarkan.',
'phone_verification_token.required' => 'Pengesahan nombor telefon diperlukan.',
])->validate();
$this->phoneVerificationOtpService->consumeRegistrationToken(
$phoneNumber,
$input['phone_verification_token'],
);
$user = User::create([
'name' => $input['name'],
'uuid' => Str::uuid(),
'email' => $input['email'],
'password' => Hash::make($input['password']),
'ic_number' => $input['ic_number'],
'phone_number' => $phoneNumber,
'phone_verified_at' => now(),
'status' => 'pending',
]);
@@ -54,9 +73,6 @@ class CreateNewUser implements CreatesNewUsers
$user->assignRole($role);
}
app(EmailVerificationOtpService::class)->send($user);
// Send notification to admins if user requires activation
if ($user->status === 'pending') {
$this->notifyAdminsForActivation($user);
}
@@ -65,21 +81,19 @@ class CreateNewUser implements CreatesNewUsers
}
/**
* Notify admins about new user requiring activation
* Notify users who can kemaskini pengguna about new user requiring activation.
*/
private function notifyAdminsForActivation(User $newUser): void
{
try {
$adminRoles = ['PENTADBIR', 'PS 2 KJC', 'PS 2 ALAT'];
// Get users with specific roles plus admins (PENTADBIR and DEVELOPER)
$adminUsers = $this->getUsersWithRolesAndAdmins($adminRoles);
$recipients = $this->getUsersWithPermission(UserPolicy::PERMISSION_UPDATE)
->where('id', '!=', $newUser->id);
$sender = auth()->user() ?? $newUser; // Use current user as sender, or new user if no auth
$sender = auth()->user() ?? $newUser;
foreach ($adminUsers as $admin) {
foreach ($recipients as $recipient) {
try {
$admin->notify(new UserActivationNotification($newUser, $sender));
$recipient->notify(new UserActivationNotification($newUser, $sender));
} catch (Exception $e) {
Log::error('Failed to send user activation notification: '.$e->getMessage());
}
@@ -88,4 +102,5 @@ class CreateNewUser implements CreatesNewUsers
Log::error('Failed to notify admins for user activation: '.$e->getMessage());
}
}
}
@@ -3,39 +3,19 @@
namespace Modules\Auth\Actions\Fortify;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Auth;
use Laravel\Fortify\Contracts\LoginResponse as ContractsLoginResponse;
use Modules\Auth\Services\AuthSessionService;
use Modules\Auth\Services\EmailVerificationOtpService;
class LoginResponse implements ContractsLoginResponse
{
public function __construct(
protected AuthSessionService $authSession,
protected EmailVerificationOtpService $otpService,
) {}
public function toResponse($request): JsonResponse
{
$user = $request->user();
if (! $user->hasVerifiedEmail()) {
$this->otpService->send($user);
Auth::guard(config('fortify.guard'))->logout();
return response()->json([
'success' => true,
'message' => 'Sila semak e-mel anda untuk kod pengesahan 6 digit.',
'data' => [
'email' => $user->email,
'requires_email_verification' => true,
],
]);
}
return $this->authSession->createAuthResponse(
$user,
$request->user(),
'Login successful'
);
}
@@ -3,24 +3,21 @@
namespace Modules\Auth\Actions\Fortify;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Auth;
use Laravel\Fortify\Contracts\RegisterResponse as RegisterResponseContract;
use Modules\Auth\Services\AuthSessionService;
class RegisterResponse implements RegisterResponseContract
{
public function __construct(
protected AuthSessionService $authSession,
) {}
public function toResponse($request): JsonResponse
{
$email = $request->user()?->email;
Auth::guard(config('fortify.guard'))->logout();
return response()->json([
'success' => true,
'message' => 'Pendaftaran berjaya. Sila semak e-mel anda untuk kod pengesahan 6 digit.',
'data' => [
'email' => $email,
'requires_email_verification' => true,
],
], 201);
return $this->authSession->createAuthResponse(
$request->user(),
'Pendaftaran berjaya. Akaun anda sedang menunggu pengaktifan daripada pentadbir sistem.',
201
);
}
}