Login with Google in Laravel 5.7

Generic placeholder image
Brijpal Sharma

Date:-December 25th, 2018 , Posted On:- Web Development

Today topic is how to login with Google in Laravel 5.7.

In this blog, we create an application Login with Google in Laravel 5.7. to complete this application we need Google API key, Client ID and Client secret key we can get these credentials from google if you don't have now. first, register with Google.

Step:1 Install a new Laravel 5.7 project.

To create or install a new Laravel follow this command and run on the command prompt.

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


laravel new blog

After that successfully install new Laravel project new need user table. Laravel give us Auth functionality for user login or register to generate this feather we just need to run following command.

php artisan make:auth

After that, we can see some new file and folder created in Laravel application in the database table and view auth table.

After the migrate all tables using this flowing command.

php artisan migrate

The above command will generate a user table in the database.

Step:2 Add the following route in your routes table.

Route::get('google-login',array('as'=>'glogin','uses'=>'UserController@googleLogin')) ;
Route::get('google-user',array('as'=>'user.glist','uses'=>'UserController@listGoogleUser')) ;

Step:3 Create a new controller  a UserController

Run this following command

php artisan make:controller UserController 

Add some code in UserController

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\User;
class UserController extends Controller
    public function googleLogin(Request $request)  {
        $google_redirect_url = route('glogin');
        $gClient = new \Google_Client();
        $google_oauthV2 = new \Google_Service_Oauth2($gClient);
        if ($request->get('code')){
            $request->session()->put('token', $gClient->getAccessToken());
        if ($request->session()->get('token'))
        if ($gClient->getAccessToken())
            //For logged in user, get details from google using access token
            $guser = $google_oauthV2->userinfo->get();  
                $request->session()->put('name', $guser['name']);
                if ($user =User::where('email',$guser['email'])->first())
                    //logged your user via auth login
                    //register your user with response data
         return redirect()->route('user.glist');          
        } else
            //For Guest user, get google login url
            $authUrl = $gClient->createAuthUrl();
            return redirect()->to($authUrl);
    public function listGoogleUser(Request $request){
      $users = User::orderBy('id','DESC')->paginate(5);
     return view('users.list',compact('users'))->with('i', ($request->input('page', 1) - 1) * 5);;

Step:4 create a new view page 

And add some view  HTML code 

    <div class="row">
        <div class="col-lg-12 margin-tb">
            <div class="pull-left">
                <h2>Logged Google User List</h2>
            <div class="pull-right">
                <a class="btn btn-danger" href="{{ route('glogin') }}"> Login with Google</a>
    <table class="table table-bordered">
            <th>Login Time</th>
    @foreach ($users as $user)
        <td>{{ ++$i }}</td>
        <td>{{ $user->name }}</td>
        <td>{{ $user->updated_at->diffForHumans() }}</td>   
    {!! $users->render() !!}

Now you can test your application with google login.

So here we completed tutorials Log in with Google in Laravel 5.7 

About the author

user profile
Brijpal Sharma

Hello, My Name is Brijpal Sharma. I am a Web Developer, Professional Blogger and Digital Marketer from India. I am the founder of Codermen. I started this blog to help web developers & bloggers by providing easy and best tutorials, articles and offers for web developers and bloggers...


You must be logged in to post a comment.