esaeng
3 weeks ago
123
1
Laravel

Laravel\Socialite\Two\InvalidStateException

Posted 3 weeks ago by esaeng

Hi there! I am trying to login using facebook api. But getting this error.Suggestions plz..! Here is my controller code

<?php

 namespace App\Http\Controllers;
 use Illuminate\Http\Request;
 use Validator,Redirect,Response,File;
 use Socialite;
 use App\Models\User;

 class SocialiteController extends Controller
 {

 public function index()
 {
     return view('login');
 }
 
 public function redirect($provider)

 {
 	// dd($provider);
 	// dd(Socialite::driver($provider)->redirect());
     return Socialite::driver($provider)->redirect();
 }

 public function callback($provider)
 {
 	// dd('callback');

   $getInfo = Socialite::driver($provider)->user(); 
   dd($getInfo);
   $user = $this->create($getInfo,$provider); 
   auth()->login($user); 
   return redirect()->to('/home');
 }

 function create($getInfo,$provider)
 {

   $user = User::where('provider_id', $getInfo->id)->first();
   if (!$user) {
        $user = User::create([
           'name'     => $getInfo->name,
           'email'    => $getInfo->email,
           'provider' => $provider,
           'provider_id' => $getInfo->id
       ]);
     }
     return $user;
  }

}

Here is my ap\services.php code

  'facebook' => [
     'client_id' => 'client_id',
     'client_secret' => 'secret_id',
     'redirect' => 'http://localhost:8000/callback/facebook',
   ],

Please sign in or create an account to participate in this conversation.