Welcome Guest, Not a member yet? Register   Sign In
Shield - 2FA for only some users
#7

So I added my file inside \App\Libraries and called it CustomEmail2FA.php.

Inside it is my code:
Code:
<?php

namespace App\Libraries;
use CodeIgniter\Shield\Authentication\Actions\Email2FA;

class CustomEmail2FA extends Email2FA{
    /**
    * Creates an identity for the action of the user.
    *
    * @return string secret
    */
    public function createIdentity(User $user): string
    {
        if($user->email2fa != '1'){
            return '';
        }
        /** @var UserIdentityModel $identityModel */
        $identityModel = model(UserIdentityModel::class);

        // Delete any previous identities for action
        $identityModel->deleteIdentitiesByType($user, $this->type);

        $generator = static fn (): string => random_string('nozero', 6);

        return $identityModel->createCodeIdentity(
            $user,
            [
                'type'  => $this->type,
                'name'  => 'login',
                'extra' => lang('Auth.need2FA'),
            ],
            $generator
        );
    }
}

Then I declared it in Auth.php:
Code:
public array $actions = [
        'register' => \CodeIgniter\Shield\Authentication\Actions\EmailActivator::class,
        'login'    => \App\Libraries\CustomEmail2FA::class,
    ];

Now, when I log in, I always get this error:
Quote:Call to a member function getType() on null

From this file:
Quote:VENDORPATH/codeigniter4/shield/src/Authentication/Authenticators/Session.php at line 486

Line 486:
Code:
$identity = $this->userIdentityModel->getIdentityByType($this->user, $action->getType());

What am I doing wrong?
Reply


Messages In This Thread
Shield - 2FA for only some users - by TexasTF - 10-03-2023, 08:48 AM
RE: Shield - 2FA for only some users - by kenjis - 10-03-2023, 02:04 PM
RE: Shield - 2FA for only some users - by TexasTF - 10-04-2023, 07:38 AM
RE: Shield - 2FA for only some users - by TexasTF - 10-04-2023, 10:03 AM
RE: Shield - 2FA for only some users - by kenjis - 03-07-2024, 04:51 PM
RE: Shield - 2FA for only some users - by kenjis - 03-10-2024, 05:54 PM
RE: Shield - 2FA for only some users - by kenjis - 03-10-2024, 06:06 PM
RE: Shield - 2FA for only some users - by scottb - 04-29-2024, 08:33 AM



Theme © iAndrew 2016 - Forum software by © MyBB