socialite with laravel. [Login with => Facebook, Google, Twitter] e.t.c social site.
First We should install Laravel Socialite:
composer require laravel/socialite
Configuration:
After installing the Socialite library, register the
Laravel\Socialite\SocialiteServiceProvider
in your config/app.php
configuration file:'providers' => [
// Other service providers...
Laravel\Socialite\SocialiteServiceProvider::class,
],
Also, add the
Socialite
facade to the aliases
array in your app
configuration file:'Socialite' => Laravel\Socialite\Facades\Socialite::class,
Before using Socialite, you will also need to add credentials for the OAuth services your application utilizes. These credentials should be placed in your
config/services.php
configuration file, and should use the key facebook
, twitter
, linkedin
, google
, github
or bitbucket
, depending on the providers your application requires. For example:'github' => [
'client_id' => env('GITHUB_CLIENT_ID'), // Your GitHub Client ID
'client_secret' => env('GITHUB_CLIENT_SECRET'), // Your GitHub Client Secret
'redirect' => 'http://your-callback-url',
],
Now Follow the Main Documents:
OR, Follow Me : 👦
Make 2 routes:
- for => redirect To Provider :
Route::get('login/{service}', 'Auth\LoginController@redirectToProvider_all');
- for => handle Provider Callback:
Route::get('login/{service}/callback', 'Auth\LoginController@handleProviderCallback_all');
Copy 3 methods from below and paste in LoginController.php:
/**it works for all socials: facebook, twitter,google **/ /** * Redirect the user to the Google/e.t.c authentication page. * * @return \Illuminate\Http\Response */ public function redirectToProvider_all($service){ //$this->check = $loginid; return Socialite::driver($service)->redirect(); } /** * Obtain the user information from Google/e.t.c. * * @return \Illuminate\Http\Response */ public function handleProviderCallback_all($service){ $user = Socialite::driver($service)->stateless()->user(); $access_token = $user->token; $name = $user->name; $email = $user->email; $pass = $user->id; if($this->isRegistered($email)){ $user_array = array(); $user_array['email'] = $email; $user_array['password'] = $pass; /*For Login */ if(Auth::attempt($user_array)){ return Redirect::to('/home'); }else{ echo "pass incorrect"; } }else{ return User::create([ 'name' => $name, 'email' => $email, 'password' => Hash::make($pass), ]); } } public function isRegistered($mail){ $user = User::where('email', $mail)->first(); if ($user) { return true; } else { return false; } }
Now the Last work is:
Do What ever You want.
মন্তব্যসমূহ
একটি মন্তব্য পোস্ট করুন