From 9dd79f5fa9e7d8a00b73b81d6e492c68a4280f5b Mon Sep 17 00:00:00 2001 From: DariusIII Date: Mon, 6 Oct 2025 15:46:15 +0200 Subject: [PATCH] Update 2fa --- .../PasswordSecurityController.php | 46 ++++++- resources/views/auth/login.blade.php | 22 +++- .../views/auth/passwords/email.blade.php | 12 +- resources/views/profile/edit.blade.php | 124 ++++++++++++++++-- routes/web.php | 1 + 5 files changed, 172 insertions(+), 33 deletions(-) diff --git a/app/Http/Controllers/PasswordSecurityController.php b/app/Http/Controllers/PasswordSecurityController.php index 2eaa2e0fd..ffc5e5d1d 100644 --- a/app/Http/Controllers/PasswordSecurityController.php +++ b/app/Http/Controllers/PasswordSecurityController.php @@ -52,6 +52,11 @@ class PasswordSecurityController extends Controller ] ); + // Check if request is from profile page + if ($request->has('from_profile') || $request->headers->get('referer') && str_contains($request->headers->get('referer'), 'profileedit')) { + return redirect()->to('profileedit#security')->with('success_2fa', 'Secret Key is generated, Please scan the QR code and verify to Enable 2FA'); + } + return redirect()->to('2fa')->with('success', 'Secret Key is generated, Please verify Code to Enable 2FA'); } @@ -85,6 +90,20 @@ class PasswordSecurityController extends Controller return redirect()->to('2fa')->with('error', 'Invalid Verification Code, Please try again.'); } + public function cancelSetup(Request $request): RedirectResponse + { + $user = $request->user(); + + // Only allow canceling if 2FA is not yet enabled + if ($user->passwordSecurity()->exists() && ! $user->passwordSecurity->google2fa_enable) { + $user->passwordSecurity()->delete(); + + return redirect()->to('profileedit#security')->with('success_2fa', '2FA setup has been cancelled.'); + } + + return redirect()->to('profileedit#security')->with('error_2fa', 'Unable to cancel 2FA setup.'); + } + public function disable2fa(Disable2faPasswordSecurityRequest $request): \Illuminate\Routing\Redirector|RedirectResponse|\Illuminate\Contracts\Foundation\Application { if (! (Hash::check($request->get('current-password'), $request->user()->password))) { @@ -279,8 +298,6 @@ class PasswordSecurityController extends Controller public function showEnable2faForm(Request $request): Application|View|Factory|\Illuminate\Contracts\Foundation\Application { $user = $request->user(); - $success = $request->session()->get('success'); - $error = $request->session()->get('error'); $google2fa_url = ''; if ($user->passwordSecurity()->exists()) { @@ -291,7 +308,12 @@ class PasswordSecurityController extends Controller ); } - return view('themes.Gentele.2fa_enable', compact('user', 'google2fa_url', 'success', 'error')); + $data = [ + 'user' => $user, + 'google2fa_url' => $google2fa_url, + ]; + + return view('auth.2fa')->with('data', $data); } /** @@ -300,9 +322,21 @@ class PasswordSecurityController extends Controller public function showDisable2faForm(Request $request): Application|View|Factory|\Illuminate\Contracts\Foundation\Application { $user = $request->user(); - $success = $request->session()->get('success'); - $error = $request->session()->get('error'); - return view('themes.Gentele.2fa_disable', compact('user', 'success', 'error')); + $google2fa_url = ''; + if ($user->passwordSecurity()->exists()) { + $google2fa_url = \Google2FA::getQRCodeInline( + config('app.name'), + $user->email, + $user->passwordSecurity->google2fa_secret + ); + } + + $data = [ + 'user' => $user, + 'google2fa_url' => $google2fa_url, + ]; + + return view('auth.2fa')->with('data', $data); } } diff --git a/resources/views/auth/login.blade.php b/resources/views/auth/login.blade.php index 5bfe0ab8d..382503589 100644 --- a/resources/views/auth/login.blade.php +++ b/resources/views/auth/login.blade.php @@ -116,8 +116,8 @@ - @if(Route::has('password.request')) - + @if(Route::has('forgottenpassword')) + Forgot password? @endif @@ -147,11 +147,19 @@
- - @if($google2fa_url) -
-

Two-Factor Authentication

-
-

Scan this QR code with your authenticator app to enable 2FA:

-
- {!! $google2fa_url !!} -
-

After scanning, visit the 2FA settings page to complete setup

-
-
- @endif -
@@ -176,6 +162,116 @@
+ + +
+

+ Two-Factor Authentication (2FA) +

+ + @if($user->passwordSecurity()->exists() && $user->passwordSecurity->google2fa_enable) + +
+
+ +
+

Two-Factor Authentication is Active

+

Your account is protected with an additional layer of security. You'll need your authenticator app to log in.

+ +
+ @csrf + +
+
+
+
+ @elseif($user->passwordSecurity()->exists() && !$user->passwordSecurity->google2fa_enable) + +
+
+ +
+

Complete Your 2FA Setup

+

Follow these steps to enable two-factor authentication:

+ +
    +
  1. Install an authenticator app (Google Authenticator, Authy, or similar)
  2. +
  3. Scan the QR code below with your authenticator app
  4. +
  5. Enter the 6-digit code from your app to verify
  6. +
+ +
+ 2FA QR Code +
+ +

+ Secret Key (manual entry): + {{ $user->passwordSecurity->google2fa_secret }} +

+ + +
+ @csrf +
+ + +
+
+ +
+
+ +
+ @csrf + +
+
+
+
+ @else + +
+
+ +
+

Two-Factor Authentication is Disabled

+

Add an extra layer of security to your account by enabling two-factor authentication.

+ +
+ @csrf + + +
+
+
+
+ @endif + +
+

+ + What is 2FA? Two-Factor Authentication adds an extra layer of security by requiring both your password and a code from your phone to log in. +

+
+
@endsection diff --git a/routes/web.php b/routes/web.php index 98d958d5e..2523ea224 100644 --- a/routes/web.php +++ b/routes/web.php @@ -161,6 +161,7 @@ Route::middleware('isVerified')->group(function () { // Custom 2FA routes that redirect to profile page Route::post('profileedit/enable2fa', [PasswordSecurityController::class, 'enable2fa'])->name('profileedit.enable2fa'); Route::post('profileedit/disable2fa', [PasswordSecurityController::class, 'disable2fa'])->name('profileedit.disable2fa'); + Route::post('profileedit/cancel2fa', [PasswordSecurityController::class, 'cancelSetup'])->name('profileedit.cancel2fa'); Route::post('profile-security/disable-2fa', [ProfileSecurityController::class, 'disable2fa'])->name('profile.security.disable2fa'); });