Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cookie is removed after gmail login

I'm implementing an online reservation system using Laravel Framework version 5.6 and Laravel Socialite to implement gmail login.

I have a method that checks if user is logged in before reservation, or it puts reserveData and redirectUrl specified by an uniqid in redis and cookie to fetch it after logging in:

public function checkAuthentication(Request $request)
{
    $reserveData = json_decode($request->input('reserveData'), true);
    Session::put('reserveData', $reserveData);

    if (!Auth::check()) {
        $reserveID = uniqid();
        Cookie::queue(Cookie::forget('reserveID'));
        Cookie::queue(Cookie::make('reserveID', $reserveID, 1440));

        $stepData = [
            'redirectUrl' => route('reserve', ['productId' => $reserveData['productId']]),
            'reserveData' => $reserveData
        ];

        Redis::set($reserveID, serialize($stepData));

        return redirect()->route('redirectToGmail');
    }

    return redirect()->route('reserve', ['productId' => $reserveData['productId']]);
}

redirectToGmail:

public function redirectToGmail()
{
    return Socialite::driver('google')->redirect();
}

The problem is, the uniqid doesn't exist in cookie after returning back from gmail only for the first time that user tries to login:

public function login()
{
    $user = Socialite::driver('google')->stateless()->user();
    dd(Cookie::get());
}

Here it is my output of dd(Cookie::get()); after returning back from gmail:

array:4 [▼
    "XSRF-TOKEN" => "DxiHpLSqB8juOkdLSptORyXs2XGggwWuY4tKJDkz"
    "project_session" => "Gy7p3nhUNGF9D34FmWYxyvewb6juiDNSVLXWTDvS"
    "__cfduid" => null
]
like image 683
Ali Tavafi Avatar asked Oct 22 '19 14:10

Ali Tavafi


People also ask

Why are my cookies being deleted automatically?

It must be due to some extension that you have installed. Extensions can have access to clear the cookies. So, If you have not deleted the cookies manually, then the other extensions installed, are the responsible for clearing the cookies.

Why is Google deleting cookies?

When you use a browser, like Chrome, it saves some information from websites in its cache and cookies. Clearing them fixes certain problems, like loading or formatting issues on sites.

How do I unblock cookies in Gmail?

Under "Privacy and security," click Site settings. Click Cookies. From here, you can: Turn on cookies: Next to "Blocked," turn on the switch.


2 Answers

Laravel by default sets the domain parameter of Cookie::make() method to current host address, that it contains www. sub domain in it. As you can see it by calling getHost() method:

request()->getHost(); // returns e.g. www.yourdomain.com

The return url that I had already registered in gmail service was mydomain.com/return/url. I had set the cookie without passing any domain to it, so the default host address was set that it differences with the registered domain in gmail.

I removed the previous domain (mydomain.com/return/url) from gmail and registered it with www. sub domain (www.mydomain.com/return/url). Also I passed the path and domain arguments to Cookie::make() method and it works like a charm ;) :

Cookie::queue(Cookie::forget('reserveID', '/', $request->getHost()));
Cookie::queue(Cookie::make('reserveID', $reserveID, 1440, '/', $request->getHost()));
like image 62
Ali Tavafi Avatar answered Oct 25 '22 08:10

Ali Tavafi


The url of the site before and after login Gmail is the same? If the url changed, the cookie will be reset.

like image 41
MHZarei Avatar answered Oct 25 '22 09:10

MHZarei