Laravel cookie forever expires on session end

Posted 3 days ago by DavidPetrov

So either I don't seem to correctly grasp the concept of cookies and sessions or there's something wrong. What I'm strangely experiencing is the following: I've got a very crucial token I need to save for the user when they log in from a special link containing the token, so for that matter I'm using a cookie and am setting it forever.

Here's the code:

    public function incomingGuest(Request $request, $companyHash)
    {
        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)); //that's the other crucial cookie
        
        return redirect()->action('[email protected]');
    }
    public function index(Request $request)
    {
        $company = company(); //fetches company from the cookie and several other instances if it's not present there
        
        if(!$company)
        {
            return redirect()->action('[email protected]');
        }
        
        clearConfiguratorSession();
        Auth::login($company->users->first(), true); //works, don't bother
        Cookie::queue(cookie()->forever('is_guest', true)); //that's a crucial cookie, but it's getting emptied
        
        return view('dogrami.select');
    }
    

However, when the session has been open for longer than its lifetime (which by default is 120 minutes), this cookie is getting emptied alongside the session, so the user can refresh and loses his identification functionality. Is that a desired behaviour and if not, how can I sustain eternal cookies? :D Thanks a lot in advance!

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

Reply to

Use Markdown with GitHub-flavored code blocks.