Login with google using Socialite package in laravel-5.4 or upper version.
In this section I will discuss how to login with google account in your Laravel website. Laravel 5.6 provide Socialite package that’s help to login social authentication like google, Facebook, etc. So today we discussed only Google login.
Let’s follow bellow step:
Step 1: Install Socialite package
In first we will install Socialite Package that provide google api to connect with google. So, we need to open terminal and run bellow command:
composer require laravel/socialite
After Complete installation the package we need to add service provider
` and alias
in app.php
.
So go to the directory config/app.php
open the file in any editor (I am using sublime 3)
Find provider put the changes in last.
Providers
'providers' => [
....
Laravel\Socialite\SocialiteServiceProvider::class,
]
Aliases
'aliases' => [
....
'Socialite' => Laravel\Socialite\Facades\Socialite::class,
],
Step 2: Create Google App
In google login we need google App ID and secret key for that we can get other user information. So if have no google App ID We can create App ID using this link
https://console.developers.google.com
After create account you can copy App ID and App Secret Key.After that put the APP ID and Secret key in .env
` file and have changes in service.php
`.
Now we open service.php in directory `/config/service.php` add the bellow code in service.php
return [
....
'google' => [
'client_id' => env('GOOGLE_CLIENT_ID'),
'client_secret' => env('GOOGLE_CLIENT_SECRET'),
'redirect' => env('GOOGLE_CALLBACK_URL'),
],
]
And also add bellow code in .env
` file
GOOGLE_CLIENT_ID=xxxxxxxxx
GOOGLE_CLIENT_SECRET=xxxxxxx
GOOGLE_CALLBACK_URL=http://xxxxxxx/auth/google/callback
Step 3: Create Migration to Extend the user model to put new field(google_id) in user table.
<?phpuse Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;class CreateAddColumnUsersTable extends Migration{/**
* Run the migrations.
*
* @return void
*/public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->string('google_id')->nullable()->after('id');
});
}/*** Reverse the migrations.
*
* @return void
*/public function down(){
Schema::table('users', function (Blueprint $table) {
$table->string('google_id')->nullable()->after('id');
});
}
}
Step 4: In this step we need to add field in user model which field added by migration. So
We open the `User.php`
<?phpnamespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;class User extends Authenticatable{
use Notifiable;/**
* The attributes that are mass assignable.
*
* @var array
*/ protected $fillable = [
'name', 'email', 'password', 'google_id'
];/**
* The attributes that should be hidden for arrays.
*
* @var array
*/protected $hidden = ['password', 'remember_token',];public function addNew($input)
{
$check = static::where('google_id',$input['google_id'])->first();
if(is_null($check)){
return static::create($input);
}
return $check;
}}
Step 5: In this step we need to make new controller and method to handle google callback request and other. First make new controller GoogleController.php in bellow directory
app/Http/Controllers/Auth
And put the code in GoogleController.php
<?phpnamespace App\Http\Controllers\Auth;
use App\User;
use App\Http\Controllers\Controller;
use Socialite;
use Exception;
use Auth;class GoogleController extends Controller
{/**
* Create a new controller instance.
*
* @return void
*/public function redirectToGoogle(){
return Socialite::driver('google')->redirect();
}/**
* Create a new controller instance.
*
* @return void
*/public function handleGoogleCallback(){ try { $user = Socialite::driver('google')->user();
$create['name'] = $user->getName();
$create['email'] = $user->getEmail();
$create['google_id'] = $user->getId();
$userModel = new User;
$createdUser = $userModel->addNew($create);
Auth::loginUsingId($createdUser->id);
return redirect()->route('welcome');
} catch (Exception $e) { return redirect('auth/google');
} }}
Step 6: Create new route for google login.
So We need to add route bellow file route/web.php
`
Route::get(‘google’, function () {return view(‘google’);});
Route::get(‘auth/google’, ‘Auth\GoogleController@redirectToGoogle’);
Route::get(‘auth/google/callback’,‘Auth\GoogleController@handleGoogleCallback’);
Step 6:In this step we set the google login button where we need. The code are bellow
Google login button
<a href="{{ url('auth/google') }}" class="btn btn-primary btn-block"><strong>Login With Google</strong></a>
After that you can able to login using google in your web site.
May be this article help you to make google login in laravel 5 or upper version.