Sending additional parameters to callback uri in socialite package for laravel

后端 未结 4 729
甜味超标
甜味超标 2020-12-21 15:15

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

相关标签:
4条回答
  • 2020-12-21 15:51

    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.

    0 讨论(0)
  • 2020-12-21 15:57

    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()
    
    0 讨论(0)
  • 2020-12-21 16:00

    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

    0 讨论(0)
  • 2020-12-21 16:03

    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.

    Simplified example

    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');
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题