Laravel 6 Login with Google Account Example

10-Apr-2023

.

Admin

Laravel 6 Login with Google Account Example

hii guys,

In this example,I will show you how to login with google account in laravel 6 application.we will use laravel socialite package for login with google account in laravel 6 application.

Nowadays user mostly sign up or login with social network, because its easy and time consuming.

so use social login in your application its make your application user friendly.

Follow below step to use social authentication in your application :

Install Laravel 6


you will create laravel 6 fresh application.

composer create-project --prefer-dist laravel/laravel loginWithGoogle

Install Socialite

In this step we will install Socialite Package that provide fb api to connect with google account. So, first open your terminal and run bellow command.

composer require laravel/socialite

config/app.php

After install above package we should add providers and aliases in config file.

'providers' => [

....

Laravel\Socialite\SocialiteServiceProvider::class,

],

'aliases' => [

....

'Socialite' => Laravel\Socialite\Facades\Socialite::class,

],

Install Auth

First you need to install laravel/ui package as like bellow:

composer require laravel/ui

You can use following commands for creating auth:

php artisan ui bootstrap --auth

Now you need to run npm command, otherwise you can not see better layout of login and register page.

Install NPM:

npm install

Run NPM:

npm run dev

Create Google App

In this step we need google client id and secret that way we can get information of other user. so if you don't have google app account then you can create from here : Google Developers Console. you can find bellow screen :

Now you have to click on Credentials and choose second option oAuth client id and click Create new Client ID button. now you can see following slide:

after create account you can copy client id and secret.

config/services.php

you will create the index.php file in the views directory.

return [

....

'google' => [

'client_id' => 'app id',

'client_secret' => 'add secret',

'redirect' => 'http://learnl52.hd/auth/google/callback',

],

]

Add Database Column

In this step first we have to create migration for add google_id in your user table. So let's run bellow command:

php artisan make:migration add_google_id_column

<php

use Illuminate\Support\Facades\Schema;

use Illuminate\Database\Schema\Blueprint;

use Illuminate\Database\Migrations\Migration;

class AddGoogleIdColumn extends Migration

{

/**

* Run the migrations.

*

* @return void

*/

public function up()

{

Schema::table('users', function ($table) {

$table->string('google_id')->nullable();

});

}

/**

* Reverse the migrations.

*

* @return void

*/

public function down()

{

//

}

}

app/User.php

Update mode like this way:

<php

namespace App;

use Illuminate\Notifications\Notifiable;

use Illuminate\Contracts\Auth\MustVerifyEmail;

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',

];

/**

* The attributes that should be cast to native types.

*

* @var array

*/

protected $casts = [

'email_verified_at' => 'datetime',

];

}

Create Routes

After adding google_id column first we have to add new route for google login. so let's add bellow route in routes.php file.

app/Http/routes.php

Route::get('google', function () {

return view('googleAuth');

});

Route::get('auth/google', 'Auth\LoginController@redirectToGoogle');

Route::get('auth/google/callback', 'Auth\LoginController@handleGoogleCallback');

Update Controller File

After add route, we need to add method of google auth that method will handle google callback url and etc, first put bellow code on your LoginController.php file.

app/Http/Controllers/Auth/LoginController.php

<php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;

use Illuminate\Foundation\Auth\AuthenticatesUsers;

use Socialite;

use Auth;

use Exception;

use App\User;

class LoginController extends Controller

{

/*

|--------------------------------------------------------------------------

| Login Controller

|--------------------------------------------------------------------------

|

| This controller handles authenticating users for the application and

| redirecting them to your home screen. The controller uses a trait

| to conveniently provide its functionality to your applications.

|

*/

use AuthenticatesUsers;

/**

* Where to redirect users after login.

*

* @var string

*/

protected $redirectTo = '/home';

/**

* Create a new controller instance.

*

* @return void

*/

public function __construct()

{

$this->middleware('guest')->except('logout');

}

public function redirectToGoogle()

{

return Socialite::driver('google')->redirect();

}

public function handleGoogleCallback()

{

try {

$user = Socialite::driver('google')->user();

$finduser = User::where('google_id', $user->id)->first();

if($finduser){

Auth::login($finduser);

return return redirect('/home');

}else{

$newUser = User::create([

'name' => $user->name,

'email' => $user->email,

'google_id'=> $user->id

]);

Auth::login($newUser);

return redirect()->back();

}

} catch (Exception $e) {

return redirect('auth/google');

}

}

}

Create Blade File

Ok, now at last we need to add blade view so first create new file googleAuth.blade.php file and put bellow code:

resources/views/googleAuth.blade.php

<!DOCTYPE html>

<html>

<head>

<title>Laravel 6 Login with Google Account Example</title>

</head>

<body>

<div class="container">

<div class="row">

<div class="col-md-12 row-block">

<a href="{{ url('auth/google') }}" class="btn btn-lg btn-primary btn-block">

<strong>Login With Google</strong>

</a>

</div>

</div>

</div>

</body>

</html>

It will help you....

#Laravel 6