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

isaachatilima's avatar

Laravel Fortify Unit Test

I trying to write a test case for user to confirm password, activate 2FA then confirm with code. I however am struggling generating the code. My test snippet is this

test('confirm 2FA', function () {
    $user = User::factory()->create([
        'password' => Hash::make('Password1#'),
    ]);

    // Confirm User Password with Fortify route
    $this->actingAs($user)->post('/user/confirm-password', [
        'password' => 'Password1#',
    ]);

    // Activate 2FA
    $this->actingAs($user)->post('/user/two-factor-authentication');

    // Confirm 2FA
    $response = $this->actingAs($user)->post('/user/confirmed-two-factor-authentication', [
        'code' => '',
    ]);

    $response->assertStatus(302);

    $user->refresh();
}); 

ChatGPT suggested I use PragmaRX\Google2FA\Google2FA but the methods to generate the code are undefined. How can I generate the code to confirm 2FA with?

0 likes
2 replies
isaachatilima's avatar

I figured it out, turns out I need to decrypt the two_factor_secrete then use it to generate the code.

Please or to participate in this conversation.