Laravel 5 login with google oauth apiclient example

Laravel 5 login with google oauth apiclient example

Laravel 5 login with google oauth apiclient example.

In my previous tutorial, you will learn, how to login with facebook now i am going to tell you that how you can login with google in laravel 5.2.

To authenticate with google you need client id, client secret and api key so you must have these credentials before processing with login with google.

Here using this code, you can login and register via google.

Step1: Install Laravel 5.2

If Laravel is not installed in your system then first install with following command and get fresh Laravel project.

composer create-project --prefer-dist laravel/laravel blog
Step 2: Create users table and model

Now you will create a User table in your database, first go through with PHP artisan command to create migration file for user table.

So first fire this command :
php artisan make:migration create_users_table

After fire this command you will see a migration file in database/migrations. You will simply put following code in your migration file to create user table.


  1. use Illuminate\Database\Schema\Blueprint;
  2. use Illuminate\Database\Migrations\Migration;
  3. class CreateUsersTable extends Migration
  4. {
  5. /**
  6. * Run the migrations.
  7. *
  8. * @return void
  9. */
  10. public function up()
  11. {
  12. Schema::create('users', function (Blueprint $table) {
  13. $table->increments('id');
  14. $table->string('name');
  15. $table->string('email')->unique();
  16. $table->string('password');
  17. $table->rememberToken();
  18. $table->timestamps();
  19. });
  20. }
  21. /**
  22. * Reverse the migrations.
  23. *
  24. * @return void
  25. */
  26. public function down()
  27. {
  28. Schema::drop('users');
  29. }
  30. }

Save this migration file and run following command.

php artisan migrate
app/User.php

  1. namespace App;
  2. use Illuminate\Foundation\Auth\User as Authenticatable;
  3. class User extends Authenticatable
  4. {
  5. /**
  6. * The attributes that are mass assignable.
  7. *
  8. * @var array
  9. */
  10. protected $fillable = [
  11. 'name', 'email', 'password',
  12. ];
  13. /**
  14. * The attributes that should be hidden for arrays.
  15. *
  16. * @var array
  17. */
  18. protected $hidden = [
  19. 'password', 'remember_token',
  20. ];
  21. }
Step3: Update Composer File to add Google apiclient libraries

Put following line in your composer file and update your composer to download package:

"google/apiclient": "2.0.*"
Step4: Route File

Now add following routes in your routes.php


  1. Route::get('glogin',array('as'=>'glogin','uses'=>'UserController@googleLogin')) ;
  2. Route::get('google-user',array('as'=>'user.glist','uses'=>'UserController@listGoogleUser')) ;
Step5: User Controller

Now create User Controller file in following path app/Http/Controllers/

app/Http/Controllers/UserController.php

  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use App\Http\Controllers\Controller;
  5. use App\User;
  6. class UserController extends Controller
  7. {
  8. public function googleLogin(Request $request) {
  9. $google_redirect_url = route('glogin');
  10. $gClient = new \Google_Client();
  11. $gClient->setApplicationName(config('services.google.app_name'));
  12. $gClient->setClientId(config('services.google.client_id'));
  13. $gClient->setClientSecret(config('services.google.client_secret'));
  14. $gClient->setRedirectUri($google_redirect_url);
  15. $gClient->setDeveloperKey(config('services.google.api_key'));
  16. $gClient->setScopes(array(
  17. 'https://www.googleapis.com/auth/plus.me',
  18. 'https://www.googleapis.com/auth/userinfo.email',
  19. 'https://www.googleapis.com/auth/userinfo.profile',
  20. ));
  21. $google_oauthV2 = new \Google_Service_Oauth2($gClient);
  22. if ($request->get('code')){
  23. $gClient->authenticate($request->get('code'));
  24. $request->session()->put('token', $gClient->getAccessToken());
  25. }
  26. if ($request->session()->get('token'))
  27. {
  28. $gClient->setAccessToken($request->session()->get('token'));
  29. }
  30. if ($gClient->getAccessToken())
  31. {
  32. //For logged in user, get details from google using access token
  33. $guser = $google_oauthV2->userinfo->get();
  34. $request->session()->put('name', $guser['name']);
  35. if ($user =User::where('email',$guser['email'])->first())
  36. {
  37. //logged your user via auth login
  38. }else{
  39. //register your user with response data
  40. }
  41. return redirect()->route('user.glist');
  42. } else
  43. {
  44. //For Guest user, get google login url
  45. $authUrl = $gClient->createAuthUrl();
  46. return redirect()->to($authUrl);
  47. }
  48. }
  49. public function listGoogleUser(Request $request){
  50. $users = User::orderBy('id','DESC')->paginate(5);
  51. return view('users.list',compact('users'))->with('i', ($request->input('page', 1) - 1) * 5);;
  52. }
  53. }
Step 6: Create Laravel Blade File to list User who login with google

  1. @extends('layouts.default')
  2. @section('content')
  3. <div class="row">
  4. <div class="col-lg-12 margin-tb">
  5. <div class="pull-left">
  6. <h2>Logged Google User List</h2>
  7. </div>
  8. <div class="pull-right">
  9. <a class="btn btn-danger" href="{{ route('glogin') }}"> Login with Google</a>
  10. </div>
  11. </div>
  12. </div>
  13. <table class="table table-bordered">
  14. <tr>
  15. <th>No</th>
  16. <th>Name</th>
  17. <th>Login Time</th>
  18. </tr>
  19. @foreach ($users as $user)
  20. <tr>
  21. <td>{{ ++$i }}</td>
  22. <td>{{ $user->name }}</td>
  23. <td>{{ $user->updated_at->diffForHumans() }}</td>
  24. </tr>
  25. @endforeach
  26. </table>
  27. {!! $users->render() !!}
  28. @endsection

To get client_id, client_secret and api_key you have to create a app within google console and generate these tokens.

Now you can try this code in your application for google login and register in Laravel 5.2.

Click here to see the demo for login with google in Laravel 5.2

Phone: (+91) 8800417876
Noida, 201301
sakarya escort akyazı escort arifiye escort erenler escort eve gelen escort ferizli escort geyve escort hendek escort otele gelen escort sapanca escort söğütlü escort taraklı escort
sakarya escort - akyazı escort - arifiye escort - erenler escort - eve gelen escort - ferizli escort - geyve escort - hendek escort - karapürçek escort - karasu escort - kaynarca escort - kocaali escort - otele gelen escort - pamukova escort - sapanca escort - söğütlü escort - taraklı escort
sapanca escort hendek escort karasu escort escort sakarya sakarya escort bayan adapazarı escort sakarya escort sakarya escort sakarya escort sakarya escort sakarya escort sakarya escort