Login with Google in Laravel 5.7

Generic placeholder image
Brijpal Sharma
laravel  

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

or

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();
        $gClient->setApplicationName(config('services.google.app_name'));
        $gClient->setClientId(config('services.google.client_id'));
        $gClient->setClientSecret(config('services.google.client_secret'));
        $gClient->setRedirectUri($google_redirect_url);
        $gClient->setDeveloperKey(config('services.google.api_key'));
        $gClient->setScopes(array(
            'https://www.googleapis.com/auth/plus.me',
            'https://www.googleapis.com/auth/userinfo.email',
            'https://www.googleapis.com/auth/userinfo.profile',
        ));
        $google_oauthV2 = new \Google_Service_Oauth2($gClient);
        if ($request->get('code')){
            $gClient->authenticate($request->get('code'));
            $request->session()->put('token', $gClient->getAccessToken());
        }
        if ($request->session()->get('token'))
        {
            $gClient->setAccessToken($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
                }else{
                    //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 

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

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...

0 comments

You must be logged in to post a comment.