Skip to content

The Example Code Written In The Docs & The Code In The Plugin Gives Out A Solution That Runs You In A Loop #583

@Elvis254

Description

@Elvis254

PHP Version

8.1.6.0

CodeIgniter4 Version

4.2.11

Shield Version

dev-develop 44bbf2c

Which operating systems have you tested for this bug?

Windows

Which server did you use?

apache

Database

MySQL 5.6

Did you customize Shield?

I customized the Login Controller where I need to create an admin panel and it needs no registration process maybe on another Controller so as to have the super admin create, read, update & delete users as per required. I also copied the code in the Register Controller into another file that runs during software installation. In that when the software is installing a super admin is added to the system without having them fill out forms about their details so their information is hard coded in the software.

What happened?

When I try to login into the system it does nothing but returns me to the login form.

Steps to Reproduce

I have included the codes I used to customize the software below. It doesn't even show the Log Message in the login action method.

namespace App\Controllers;

use CodeIgniter\HTTP\RedirectResponse;

class AuthController extends BaseController
{
    public function loginPage()
    {
        log_message('info', 'AuthController: Login Page Method Executed.');

        if (auth()->loggedIn()) {
            return redirect()->to(config('Auth')->loginRedirect());
        }

        $authenticator = auth('session')->getAuthenticator();

        if ($authenticator->hasAction()) {
            return redirect()->route('auth-action-show');
        }

        return $this->renderView(
            'auth/login', [
                'pageTitle' => esc('Login', 'raw'),
            ]
        );
    }

    public function loginAction(): RedirectResponse
    {
        log_message('info', 'AuthController: Login Action Method Executed.');

        $rules = $this->getValidationRules();

        if ( ! $this->validate($rules)) {
            return redirect()->back()->withInput()->with('errors', $this->validator->getErrors());
        }

        $credentials             = $this->request->getPost(setting('Auth.validFields'));
        $credentials             = array_filter($credentials);
        $credentials['password'] = $this->request->getPost('password');
        $remember                = (bool) $this->request->getPost('remember');

        $authenticator = auth('session')->getAuthenticator();

        $result = $authenticator->remember($remember)->attempt($credentials);
        if ( ! $result->isOK()) {
            return redirect()->route('login')->withInput()->with('error', $result->reason());
        }

        if ($authenticator->hasAction()) {
            return redirect()->route('auth-action-show')->withCookies();
        }

        return redirect()->to(config('Auth')->loginRedirect())->withCookies();
    }

    /**
     * Returns the rules that should be used for validation.
     *
     * @return array<string, array<string, array<string>|string>>
     * @phpstan-return array<string, array<string, string|list<string>>>
     */
    protected function getValidationRules(): array
    {
        return setting('Validation.login') ?? [
            'username' => [
                'label' => 'Username',
                'rules' => config('AuthSession')->usernameValidationRules,
            ],
            // 'email' => [
            //     'label' => 'Auth.email',
            //     'rules' => config('AuthSession')->emailValidationRules,
            // ],
            'password' => [
                'label' => 'Auth.password',
                'rules' => 'required',
            ],
        ];
    }
}

The code I used to add a super admin to the system is shown below.

public function superAdminAction()
    {
        log_message('info', 'InstallController: Super Admin Action Method Executed.');

        $userModel = model(setting('Auth.userProvider'));
        $userInfo = new User();

        $userInfo->username = 'Elvis254';
        $userInfo->email    = '[email protected]';
        $userInfo->password = '[email protected]';

        try
        {
            $userModel->save($userInfo);
        }
        catch(ValidationException $e)
        {
            throw new \RuntimeException($e->getMessage(), $e->getCode(), $e);
        }
        
        $userInfo = $userModel->findById($userModel->getInsertID());
        $userModel->addToDefaultGroup($userInfo);

        $authenticator = auth('session')->getAuthenticator();
        $authenticator->startLogin($userInfo);
        $authenticator->activateUser($userInfo);
        $authenticator->completeLogin($userInfo);

        return redirect()->to('/');
    }

Expected Output

I expect it to get me logged in and redirect me to the dashboard page of the system.

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions