From 9c8ebdfabef61f0938c8fa894e45cd953c5870ac Mon Sep 17 00:00:00 2001 From: Bayu Lukman Yusuf Date: Tue, 13 Jan 2026 13:45:34 +0700 Subject: [PATCH] otp wa --- .../Controllers/Auth/LoginEmailController.php | 4 +- .../Controllers/Auth/LoginWaController.php | 107 +++++++++++++++- lang/en/otp.php | 35 ++++++ .../{signin-email.blade.php => otp.blade.php} | 117 ++++++++++++++---- resources/views/account/signin.blade.php | 73 ++++++++++- resources/views/account/signup.blade.php | 10 +- routes/web.php | 8 +- 7 files changed, 319 insertions(+), 35 deletions(-) create mode 100644 lang/en/otp.php rename resources/views/account/{signin-email.blade.php => otp.blade.php} (56%) diff --git a/app/Http/Controllers/Auth/LoginEmailController.php b/app/Http/Controllers/Auth/LoginEmailController.php index 9536edb..2970618 100644 --- a/app/Http/Controllers/Auth/LoginEmailController.php +++ b/app/Http/Controllers/Auth/LoginEmailController.php @@ -9,6 +9,8 @@ class LoginEmailController extends Controller { public function index() { - return view('account.signin-email'); + return view('account.signin',[ + 'type' => 'email', + ]); } } diff --git a/app/Http/Controllers/Auth/LoginWaController.php b/app/Http/Controllers/Auth/LoginWaController.php index c7e5a58..ab3464f 100644 --- a/app/Http/Controllers/Auth/LoginWaController.php +++ b/app/Http/Controllers/Auth/LoginWaController.php @@ -3,12 +3,117 @@ namespace App\Http\Controllers\Auth; use App\Http\Controllers\Controller; +use App\Repositories\Member\Auth\MemberAuthRepository; use Illuminate\Http\Request; +use Illuminate\Support\Facades\Session; +use Illuminate\Support\Facades\Validator; +use Illuminate\Support\Facades\Auth; +use Illuminate\Support\Facades\Log; class LoginWaController extends Controller { + protected $memberAuthRepository; + + public function __construct(MemberAuthRepository $memberAuthRepository) + { + $this->memberAuthRepository = $memberAuthRepository; + } + public function index() { - return view('account.signin'); + return view('account.signin', [ + 'type' => 'phone', + ]); + } + + public function otp(Request $request) + { + $validator = Validator::make($request->all(), [ + 'identity' => 'required|string|min:10|max:15', + ]); + + if ($validator->fails()) { + return response()->json([ + 'success' => false, + 'message' => __('otp.invalid_phone'), + 'errors' => $validator->errors() + ], 422); + } + + $identity = $request->identity; + + try { + // Use MemberAuthRepository to generate OTP + $otp = $this->memberAuthRepository->waOtp(['phone' => $identity]); + + // TODO: Integrate with WhatsApp API to send OTP + // For now, we'll just log it (remove in production) + Log::info("OTP for {$identity}: {$otp->otp}"); + + return response()->json([ + 'success' => true, + 'message' => __('otp.sent'), + 'redirect' => route('login-phone.otp.view', ['identity' => $identity]) + ]); + } catch (\Exception $e) { + Log::error("OTP generation failed: " . $e->getMessage()); + + return response()->json([ + 'success' => false, + 'message' => __('otp.generate_failed') + ], 500); + } + } + + public function otpView($identity) + { + return view('account.otp', [ + 'identity' => $identity + ]); + } + + public function verify(Request $request) + { + $validator = Validator::make($request->all(), [ + 'identity' => 'required|string|min:10|max:15', + 'otp' => 'required|string|size:6', + ]); + + if ($validator->fails()) { + return back() + ->withErrors($validator) + ->withInput(); + } + + $identity = $request->identity; + $otp = $request->otp; + + try { + // Use MemberAuthRepository to verify OTP + $result = $this->memberAuthRepository->waOtpConfirm([ + 'phone' => $identity, + 'otp' => $otp + ]); + + // TODO: Authenticate user or create new user + // For now, we'll just redirect to dashboard + // In production, you would: + // 1. Find or create user by phone number + // 2. Log them in + // 3. Redirect to intended page + + return redirect()->route('home')->with('success', __('otp.login_success')); + + } catch (\Illuminate\Validation\ValidationException $e) { + return back() + ->withErrors(['otp' => $e->getMessage()]) + ->withInput(); + } catch (\Exception $e) { + Log::error("OTP verification failed: " . $e->getMessage()); + + return back() + ->withErrors(['otp' => __('otp.verification_failed')]) + ->withInput(); + } } } diff --git a/lang/en/otp.php b/lang/en/otp.php new file mode 100644 index 0000000..eaca3f1 --- /dev/null +++ b/lang/en/otp.php @@ -0,0 +1,35 @@ + 'Verify Your Phone', + 'description' => 'We sent a 6-digit code to :phone', + 'enter_code' => 'Enter verification code', + 'invalid_code' => 'Please enter a valid 6-digit code', + 'verify' => 'Verify', + 'back_to_login' => 'Back to login', + 'resend_code' => 'Resend code', + 'resend_in' => 'Resend in', + 'need_help' => 'Need help?', + 'invalid_phone' => 'Please enter a valid phone number', + 'sent' => 'OTP sent successfully', + 'expired' => 'OTP has expired', + 'max_attempts' => 'Maximum attempts reached. Please request a new OTP', + 'invalid' => 'Invalid OTP code', + 'login_success' => 'Login successful', + 'resend_failed' => 'Failed to resend OTP. Please try again', + 'generate_failed' => 'Failed to generate OTP. Please try again', + 'verification_failed' => 'OTP verification failed. Please try again', + +]; diff --git a/resources/views/account/signin-email.blade.php b/resources/views/account/otp.blade.php similarity index 56% rename from resources/views/account/signin-email.blade.php rename to resources/views/account/otp.blade.php index a5b6baa..504e291 100644 --- a/resources/views/account/signin-email.blade.php +++ b/resources/views/account/otp.blade.php @@ -1,9 +1,9 @@ -@extends('layouts.landing', ['title' => 'Account - Sign In']) +@extends('layouts.landing', ['title' => 'Account - OTP Verification']) @section('content')
- +
-

{{ __('signin.title') }}

- +

{{ __('otp.title') }}

+

{{ __('otp.description', ['phone' => $identity]) }}

+ + + {{-- show message if error --}} + @if ($errors->has('otp')) +
+ {{ $errors->first('otp') }} +
+ @endif + -
+ + @csrf + +
- -
{{ __('signin.email_invalid') }}
+ + +
{{ __('otp.invalid_code') }}
-
- {{--
- - -
--}} + - + - +
-
+
- -
{{ __('signin.phone_invalid') }}
+ +
{{ $type == 'email' ? __('signin.email_invalid') : __('signin.phone_invalid') }}
@@ -50,8 +50,8 @@
--}}
@@ -88,4 +88,67 @@ @endsection @section('scripts') + @endsection diff --git a/resources/views/account/signup.blade.php b/resources/views/account/signup.blade.php index e72fa82..f8ab4c8 100644 --- a/resources/views/account/signup.blade.php +++ b/resources/views/account/signup.blade.php @@ -8,7 +8,7 @@

{{ __('signup.title') }}

@@ -17,11 +17,11 @@ {{ __('signup.sign_in') }}
- --}} @csrf @@ -71,9 +71,9 @@ placeholder="{{ __('signup.referral_placeholder') }}" type="text" value="{{ old('referral') }}" />
-
+
- diff --git a/routes/web.php b/routes/web.php index 0952d59..fb1636f 100644 --- a/routes/web.php +++ b/routes/web.php @@ -47,4 +47,10 @@ Route::get('/register', [RegisterController::class, 'index'])->name('register'); Route::post('/register', [RegisterController::class, 'register'])->name('register'); Route::get('/login', [LoginWaController::class, 'index'])->name('login'); -Route::get('/login/email', [LoginEmailController::class, 'index'])->name('login-email'); \ No newline at end of file +Route::get('/login/phone', [LoginWaController::class, 'index'])->name('login-phone'); +Route::post('/login/phone/otp', [LoginWaController::class,'otp'])->name('login-phone.otp'); +Route::get('/login/phone/otp/{identity}', [LoginWaController::class, 'otpView'])->name('login-phone.otp.view'); +Route::post('/login/phone/verify', [LoginWaController::class, 'verify'])->name('login-phone.verify'); + +Route::get('/login/email', [LoginEmailController::class, 'index'])->name('login-email'); +