Im trying to use Socialite package for laravel and I would like to know how to pass additional parameters to callback url. It seems that OAuth allows additional params, but
You can update on the fly callback URL as like:
public function redirectToProvider($providerName)
{
config([
"services.$providerName.redirect" => config("services.$providerName.redirect").'?queryString=test'
]);
try {
return Socialite::driver($providerName)->redirect();
} catch (\Exception $e) {
return redirect('login');
}
}
It return queryString (request->all()) in callback URL function.
Changing redirect_uri in config on fly will redirect back to the intended domain but it does not return the user when tried to get user with following
Socialite::driver($social)->user()
You need to use state param if you want to pass some data.
Example for your provider
$provider = 'google';
return Socialite::with(['state' => $provider])->redirect();
Your callback function:
$provider = request()->input('state');
$driver = Socialite::driver($provider)->stateless();
gist example
For some reason, Optional Parameters didn't work for me, so i ended up by using session to pass variables from redirect method to the callback method. it's not the best way to do it, but it does the trick.
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\User;
use Socialite;
class FacebookController extends Controller {
/**
* Create a new controller instance.
*
* @return void
*/
public function redirect($my_variable)
{
session(['my_variable' => $my_variable]);
return Socialite::driver('facebook')->redirect();
}
/**
* Create a new controller instance.
*
* @return void
*/
public function callback(Request $request)
{
try {
$facebookAccount = Socialite::driver('facebook')->stateless()->user();
$my_variable = session('my_variable');
// your logic...
return redirect()->route('route.name');
} catch (Exception $e) {
return redirect()->route('auth.facebook');
}
}
}