Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

vandan's avatar
Level 13

In laravel 2fa Google Authenticator how to setup ?

how to setup or packages use for simple login/registration form using 2fa google authenticator

please suggest me thanks

0 likes
8 replies
CookieMonster's avatar

@slev1n I know this is an old post but I am having issue verifying the digit. Was wondering if i may ask some help?

vandan's avatar
Level 13

yes i try this but when i register data then error like

Config (google2fa.php) not found. Have you published it?

vandan's avatar
Level 13

yes its all working but when i try to enter otp then redirect back otp page

otp blade file {{ csrf_field() }}

controller file

protected function create(array $data)
    {
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
            'google2fa_secret' => $data['google2fa_secret'],
        ]);
    }

public function register(Request $request)
    {
        //Validate the incoming request using the already included validator method
        $this->validator($request->all())->validate();

        // Initialise the 2FA class
        $google2fa = app('pragmarx.google2fa');

        // Save the registration data in an array
        $registration_data = $request->all();

        // Add the secret key to the registrationdata
        $registration_data["google2fa_secret"] = $google2fa->generateSecretKey();

        // Save the registration data to the user session for just the next request
        $request->session()->flash('registration_data', $registration_data);

        // Generate the QR image. This is the image the user will scan with their app
        // to set up two factor authentication
        $QR_Image = $google2fa->getQRCodeInline(
            config('app.name'),
            $registration_data['email'],
            $registration_data['google2fa_secret']
        );

        // Pass the QR barcode image to our view
        return view('google2fa.register', ['QR_Image' => $QR_Image, 'secret' =>         $registration_data['google2fa_secret']]);
    }

public function completeRegistration(Request $request)
    {        
        // add the session data back to the request input
        $request->merge(session('registration_data'));

        // Call the default laravel authentication
        return $this->registration($request);
    }

route file

Route::get('/home', 'HomeController@index')->name('home');
Route::get('/complete-registration', 'Auth\RegisterController@completeRegistration')->name('complete-   registration');

Route::post('/2fa', function () {
        return redirect(URL()->previous());
})->name('2fa')->middleware('2fa');
slev1n's avatar
slev1n
Best Answer
Level 4

Little how-to:

You need something like Google2FA Activation page, that showing QR to user (https://github.com/antonioribeiro/google2fa#generating-qrcodes)

Page controller algorithm:

  • generate secret key, save into session, make QR and show it to user;
  • user scan qr, put digits and send it to the server;
  • get secret from session, verify it with requested digits Google2FA::verifyGoogle2FA($secret, digits), if success - save secret to user model.

If user's google2fa_secret not empty - 2fa activated.

2fa middleware handles authorization and other session stuff.

2 likes
vandan's avatar
Level 13

that's fine its working i follow this link step by step i am done thanks you so much @slev1n sir

slev1n's avatar

@van1310

You're welcome! Check if you can re-login with phone-generated OTP.

Please or to participate in this conversation.