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
@@ -3,60 +3,66 @@
namespace Modules\Auth\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Auth\Events\Verified;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\URL;
use Modules\Auth\Entities\User;
use Modules\Auth\Services\AuthSessionService;
use Modules\Auth\Services\EmailVerificationOtpService;
class EmailVerificationController extends Controller
{
public function __construct(
protected EmailVerificationOtpService $otpService,
protected AuthSessionService $authSession
) {}
public function verify(Request $request): JsonResponse
public function verify(Request $request, string $id, string $hash): RedirectResponse
{
$validated = $request->validate([
'email' => ['required', 'string', 'email'],
'otp' => ['required', 'string', 'digits:6'],
]);
$user = User::where('email', $validated['email'])->first();
if (! $user) {
return response()->json([
'success' => false,
'message' => 'Kod OTP tidak sah.',
], 422);
if (! URL::hasValidSignature($request)) {
return $this->redirectToFrontend('invalid');
}
$this->otpService->verify($user, $validated['otp']);
$user = User::find($id);
$user->refresh();
if (! $user || ! hash_equals($hash, sha1($user->getEmailForVerification()))) {
return $this->redirectToFrontend('invalid');
}
return $this->authSession->createAuthResponse(
$user,
'E-mel anda telah berjaya disahkan. Akaun anda sedang menunggu pengaktifan daripada pentadbir sistem.'
);
if ($user->hasVerifiedEmail()) {
return $this->redirectToFrontend('already');
}
$user->markEmailAsVerified();
event(new Verified($user));
return $this->redirectToFrontend('success', $user);
}
public function resend(Request $request): JsonResponse
public function send(Request $request): JsonResponse
{
$validated = $request->validate([
'email' => ['required', 'string', 'email'],
]);
$user = $request->user();
$user = User::where('email', $validated['email'])->first();
if ($user && ! $user->hasVerifiedEmail()) {
$this->otpService->send($user);
if ($user->hasVerifiedEmail()) {
return response()->json([
'success' => true,
'message' => 'E-mel anda telah disahkan.',
]);
}
$user->sendEmailVerificationNotification();
return response()->json([
'success' => true,
'message' => 'Jika e-mel wujud dan belum disahkan, kod OTP baharu telah dihantar.',
'message' => 'Pautan pengesahan e-mel telah dihantar.',
]);
}
protected function redirectToFrontend(string $status, ?User $user = null): RedirectResponse
{
$baseUrl = rtrim(config('user.frontend_url'), '/');
$path = config('auth.verification.frontend_redirect_path', '/profile');
if ($user?->status === 'pending') {
$path = '/register?registered=success';
}
return redirect()->away("{$baseUrl}{$path}?verified={$status}");
}
}
@@ -0,0 +1,109 @@
<?php
namespace Modules\Auth\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Modules\Auth\Services\PhoneVerificationOtpService;
use Modules\Auth\Transformers\UserResource;
class PhoneVerificationController extends Controller
{
public function __construct(
protected PhoneVerificationOtpService $phoneVerificationOtpService,
) {}
public function send(Request $request): JsonResponse
{
$validated = $request->validate([
'phone_number' => $this->phoneVerificationOtpService->phoneNumberRules(),
], [
'phone_number.required' => 'Nombor telefon diperlukan.',
'phone_number.regex' => 'Format nombor telefon tidak sah.',
]);
$this->phoneVerificationOtpService->send($validated['phone_number']);
return response()->json([
'success' => true,
'message' => 'Kod OTP telah dihantar ke nombor telefon anda.',
]);
}
public function verify(Request $request): JsonResponse
{
$validated = $request->validate([
'phone_number' => $this->phoneVerificationOtpService->phoneNumberRules(),
'otp' => ['required', 'string', 'digits:6'],
], [
'phone_number.required' => 'Nombor telefon diperlukan.',
'phone_number.regex' => 'Format nombor telefon tidak sah.',
'otp.required' => 'Kod OTP diperlukan.',
'otp.digits' => 'Kod OTP mestilah 6 digit.',
]);
$result = $this->phoneVerificationOtpService->verify(
$validated['phone_number'],
$validated['otp'],
);
return response()->json([
'success' => true,
'message' => 'Nombor telefon berjaya disahkan.',
'data' => [
'phone_number' => $result['phone_number'],
'verification_token' => $result['verification_token'],
],
]);
}
public function sendForAuthenticatedUser(Request $request): JsonResponse
{
$user = $request->user();
$validated = $request->validate([
'phone_number' => $this->phoneVerificationOtpService->phoneNumberRules(),
], [
'phone_number.required' => 'Nombor telefon diperlukan.',
'phone_number.regex' => 'Format nombor telefon tidak sah.',
]);
$this->phoneVerificationOtpService->sendForUser($user, $validated['phone_number']);
return response()->json([
'success' => true,
'message' => 'Kod OTP telah dihantar ke nombor telefon anda.',
]);
}
public function verifyForAuthenticatedUser(Request $request): JsonResponse
{
$user = $request->user();
$wasVerified = (bool) $user->phone_verified_at;
$validated = $request->validate([
'phone_number' => $this->phoneVerificationOtpService->phoneNumberRules(),
'otp' => ['required', 'string', 'digits:6'],
], [
'phone_number.required' => 'Nombor telefon diperlukan.',
'phone_number.regex' => 'Format nombor telefon tidak sah.',
'otp.required' => 'Kod OTP diperlukan.',
'otp.digits' => 'Kod OTP mestilah 6 digit.',
]);
$verifiedUser = $this->phoneVerificationOtpService->verifyForUser(
$user,
$validated['phone_number'],
$validated['otp'],
);
return response()->json([
'success' => true,
'message' => $wasVerified
? 'Nombor telefon berjaya dikemas kini.'
: 'Nombor telefon berjaya disahkan.',
'data' => new UserResource($verifiedUser),
]);
}
}