A very good point on adding that param is preventing csrf attacks also.
The way how you add a param on google authorization request and then get back the param on the callback request from google, is by setting a base64
encoded json payload:
Before generating the Authorization url first off include the two functions to encode and decode the url params, simple:
public function base64UrlEncode($inputStr)
{
return strtr(base64_encode($inputStr), '+/=', '-_,');
}
public function base64UrlDecode($inputStr)
{
return base64_decode(strtr($inputStr, '-_,', '+/='));
}
Making the oauth request to google, make url params example:
$params = base64UrlEncode('{ "a" : "b" , "c" : 1 }');
In Google Client class there is a function setState($state)
that you need to call before creating the url and pass the $params
as parameters like:
$client = new Google_Client();
$client->setClientId('qweqweqweqwe');
$client->setClientSecret('vQWsd1Geweqweqweqwe');
$params = base64UrlEncode('{ "a" : "b" , "c" : 1 }');
$client->setState($params);
$client->setRedirectUri('http://localhost/test1');
Then the response will have state request param so in the callback route do:
Route::get('/callback', function(){
$state = request()->get('state'); // GET['state'];
$state = base64_decode(strtr($state, '-_,', '+/='));
dd($state); // will output your params
});
This answer somewhat based from:
here