Auth::logout() not working as expected

Posted 1 month ago by DavidPetrov

Again, I may be misconcepting something, but anyway. Point is I'm trying to develop a /dev route which shall make me from admin user (where I'm logged in) to a guest (where I'm incoming with a company's hash as a completely unknown new guest user). So basically what I need from the /dev route is to log me out and redirect me to a link with the company's hash. That's how it looks:

Route::get('/dev', function(){
    return redirect()->to(route('guest', ['companyHash' => \App\Company::first()->hash]));
});

    Route::get('/guest/company-{companyHash}', '[email protected]')->name('guest');

And that's the [email protected] method:

public function incomingGuest(Request $request, $companyHash)
    {
        Auth::logout(); //seems to not work

        try
        {
            $id = decrypt($companyHash);
        }
        catch(\Illuminate\Contracts\Encryption\DecryptException $ex)
        {
            //logger('No company with hash '.$companyHash);
            return redirect()->action('[email protected]');
        }
        
        $company = \App\Company::find($id);
        session()->put('company', $company);
        
        Cookie::queue(cookie()->forever('company_id', $id));
        
        return redirect()->action('[email protected]');
    }

[email protected] handles the request based on whether the user has been recognized as a guest or not. Problem is that Auth::logout() changes nothing and when I've been logged in, I'm not getting logged out and redirected but rather the controller sends me to my logged in home page. I also tried resending the request after logout, because I thought it may need a refresh of the request, but with no success.

Could that be a middleware issue? Something looping somewhere. Or is that just how Auth::logout() works and I'm misusing it. Ideas? Thanks!

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.