Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: Call to a member function inGroup() on null after logging out #576

Merged
merged 2 commits into from
Jan 6, 2023
Merged

bug: Call to a member function inGroup() on null after logging out #576

merged 2 commits into from
Jan 6, 2023

Conversation

cornejobarraza
Copy link
Contributor

@cornejobarraza cornejobarraza commented Dec 30, 2022

I have declared the following under my Auth.php file:

public function logoutRedirect(): string
{
    $url = auth()->user()->inGroup('admin')
        ? 'login'
        : setting('Auth.redirects')['logout'];

    return $this->getUrl($url);
}

And get a Call to a member function inGroup() on null error when navigating to /logout.

After some debugging I realized the logout action function under LoginController.php logs the user out before redirecting, causing the redirect() helper to get an invalid value.

To overcome this, I first capture the URL in a variable and pass it to the redirect function after the auth is logged out:

public function logoutAction(): RedirectResponse
{
    $url = config('Auth')->logoutRedirect();

    auth()->logout();

    return redirect()->to($url)->with('message', lang('Auth.successLogout'));
}

This is my first time contributing to CodeIgniter, I'm not sure if I did this correctly so please let me know if I should change something, or even if this should not be considered an issue as it's not something CI Shield sets up out of the box.

Thank you and have a good one!

@kenjis kenjis added GPG-Signing needed Pull requests that need GPG-Signing tests needed Pull requests that need tests labels Dec 31, 2022
@kenjis
Copy link
Member

kenjis commented Dec 31, 2022

Thank you for the contribution!

This PR makes sense to me.

We need your GPG sign to your commits. Please sign.
See https://github.com/codeigniter4/CodeIgniter4/blob/develop/contributing/pull_request.md#signing
and https://github.com/codeigniter4/CodeIgniter4/blob/develop/contributing/workflow.md#gpg-signing-old-commits

@cornejobarraza
Copy link
Contributor Author

Thank you, I didn't know about that. Just signed the commit and pushed the update, let me know if anything else is needed

@kenjis kenjis removed the GPG-Signing needed Pull requests that need GPG-Signing label Dec 31, 2022
@datamweb
Copy link
Collaborator

datamweb commented Jan 2, 2023

@cornejobarraza Thanks, can you write the php unit test for this PR?

see :
https://github.com/codeigniter4/CodeIgniter4/tree/develop/tests#resources

@kenjis kenjis added the bug Something isn't working label Jan 4, 2023
@cornejobarraza
Copy link
Contributor Author

@cornejobarraza Thanks, can you write the php unit test for this PR?

see : https://github.com/codeigniter4/CodeIgniter4/tree/develop/tests#resources

Hi @datamweb, I think I ran the tests correctly and some files were generated, but those seem to be in the .gitignore file. I'm new to this, so please enlighten me on how to proceed, thank you!

@datamweb
Copy link
Collaborator

datamweb commented Jan 4, 2023

I think I ran the tests correctly

Running a test is different from writing a test.

but those seem to be in the .gitignore file.

We have:

shield/.gitignore

Lines 53 to 56 in ca7335c

#-------------------------
# Test Files
#-------------------------
tests/coverage*

so this issue does not create a problem for the new test. I did not understand your point in this regard.

When you execute the test command, the existing tests are executed.
Therefore, you should write a new test related to this PR.
Have you written a test for this PR? If not, see here.

I'm new to this!

Welcome. Don't worry, this PR will remain open until you write the corresponding test.

@kenjis
Copy link
Member

kenjis commented Jan 5, 2023

@datamweb The code is covered by existing test code.
Screenshot 2023-01-05 14 42 43

So I think this PR is okay with no additional test case.

If we write a test case that reproduces the bug, we need to write an anonymous class which extends Config\Auth and override the logoutRedirect(), and inject it to the Factories.
I don't think we need such a test because the change is very small.
And it is not easy for test beginners.

@kenjis kenjis removed the tests needed Pull requests that need tests label Jan 5, 2023
@datamweb
Copy link
Collaborator

datamweb commented Jan 5, 2023

So I think this PR is okay with no additional test case.

I agree., my emphasis was because of label teste needed.

Copy link
Member

@kenjis kenjis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Copy link
Collaborator

@datamweb datamweb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cornejobarraza thanks, merged.

@datamweb datamweb merged commit 44bbf2c into codeigniter4:develop Jan 6, 2023
@cornejobarraza cornejobarraza deleted the logOutError branch January 6, 2023 22:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants