Sending additional parameters to callback uri in socialite package for laravel

后端 未结 4 735
甜味超标
甜味超标 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 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');
            }
        }
    }
    

提交回复
热议问题