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
okey metin2 pvp serverler metin2 pvp
ataşehir escort maltepe escort bostancı escort kadıköy escort maltepe escort ataşehir escort tuzla escort pendik escort ümraniye escort kartal escort
pendik escort
izmir escort izmir escort izmir escort izmir escort denizli escort antalya escort antalya escort antalya escort izmir escort izmir escort izmir escort izmir escort izmir escort
antalya escort akseki escort aksu escort alanya escort bayan antalya escort antalya grup escort antalya masöz antalya rus escort antalya sınırsız escort antalya travesti escort bdsm escort beldibi escort bayan belek escort bayan çallı escort bayan çamyuva escort bayan demre escort bayan elmalı escort bayan fethiye escort bayan finike escort bayan göynük escort bayan güllük escort bayan ışıklar escort bayan kaş escort bayan kemer escort bayan konyaaltı altınkum escort bayan konyaaltı arapsuyu escort bayan konyaaltı escort bayan konyaaltı liman escort korkuteli escort bayan kültür escort kundu escort bayan lara escort bayan manavgat escort bayan muratpaşa escort bayan örnekköy escort bayan rus escort bayan serik escort bayan side escort bayan suriyeli escort tekirova escort bayan üniversiteli escort alanya escort mahmutlar escort alanya escort mahmutlar escort alanya escort mahmutlar escort istanbul escort avrupa yakası escort şişli escort topkapı escort istanbul vip escort maslak escort merter escort aksaray escort arnavutköy escort ataköy escort bağcılar escort bahçelievler escort bakırköy escort beşitaş escort beylikdüzü escort beyoğlu escort halkalı escort istanbul sınırsız escort kağıthane escort mecidiyeköy escort nişantaşı escort escort istanbul ataşehir escort beykoz escort çekmeköy escort kadıköy escort maltepe escort pendik escort sancaktepe escort şile escort sultanbeyli escort tuzla escort ümraniye escort üsküdar escort
beylikdüzü escort istanbul escort şişli escort beylikdüzü escort istanbul escort
kocaeli escort izmit escort bayan gebze escort izmit sınırsız escort escort bursa tuzla escort aydınlı escort orhanlı escort bursa escort escort bayan istanbul escort istanbul sınırsız escort istanbul bayan escort izmit escort izmit escort kocaeli escort sisli escort mecidiyekoy escort bursa escort bayan bursa görükle escort maltepe escort goztepe escort yakacik-yeni-escort bursa sınırsız escort sisli escort bayan taksim escort bursa escort porno izle sikiş izle atasehir escort antalya escort
ankara escort marmaris escort bursa escort mersin escort antalya escort izmir escort samsun escort bodrum escort çeşme escort kuşadası escort adana escort eskişehir escort gaziantep escort esenyurt escort pendik escort tuzla escort fatih escort ümraniye escort mecidiyeköy escort kartal escort bahçelievler escort bahçeşehir escort ataşehir escort bakırköy escort nişantaşı escort avcılar escort büyükçekmece escort kurtkoy escort konya escort beşiktaş escort taksim escort beylikdüzü escort malatya escort sakarya escort şişli escort maltepe escort kadıköy escort ataköy escort bostancı escort şirinevler escort bağcılar escort üsküdar escort etiler escort başaksehir escort ortaköy escort kağıthane escort sultangazi escort halkalı escort çekmeköy escort şaşkınbakkal escort mamak escort sincan escort keçiören escort çankaya escort beypazarı escort akyurt escort pursaklar escort kahramankazan escort göztepe escort alsancak escort bornova escort buca escort karabaglar escort karşıyaka escort konak escort narlıdere escort izmit escort başiskele escort kandira escort körfez escort karamürsel escort dilovası escort gölcük escort gebze escort kocaeli escort kayseri escort elazığ escort denizli escort diyarbakir escort adana escort ankara escort izmir escort antalya escort gaziantep escort kayseri escort konya escort mersin escort denizli escort kocaeli escort diyarbakır escort alanya escort manavgat escort bursa escort eskişehir escort sakarya escort samsun escort malatya escort trabzon escort adıyaman escort afyon escort ağrı escort aksaray escort amasya escort ardahan escort artvin escort aydın escort balikesir escort bartın escort batman escort bayburt escort bilecik escort bingöl escort bitlis escort bolu escort burdur escort çanakkale escort çankırı escort çorum escort düzce escort edirne escort elazığ escort erzincan escort erzurum escort giresun escort gümüşhane escort hatay escort ığdır escort ısparta escort maraş escort karabük escort karaman escort kars escort kastamonu escort kilis escort kırıkkale escort kırklareli escort kırşehir escort kütahya escort manisa escort mardin escort muğla escort muş escort nevşehir escort niğde escort ordu escort osmaniye escort rize escort urfa escort siirt escort sinop escort şırnak escort sivas escort ordu escort tokat escort tunceli escort usak escort van escort yalova escort yozgat escort zonguldak escort kıbrıs escort
adana escort ankara escort bursa escort diyarbakir escort eskisehir escort kayseri escort kocaeli escort konya escort malatya escort maras escort mersin escort ordu escort sakarya escort samsun escort hatay escort mardin escort tekirdag escort antalya escort aydin escort nigde escort nevsehir escort elazig escort manisa escort osmaniye escort denizli escort mugla escort
bedava bahis
tahmin
ankara escort - ankara escort
ataşehir escort ataşehir anadolu yakası escort ankara escort antalya escort antalya escort

flynta 5 mg

flynta 5 mg fiyat

orcafil 5 mg

lifta

degra

sildegra

combo 100 mg

degra 100 mg

vigrande

sinegra

jeligra

lifta 20

vigrande 100 mg

lifta 20 mg

lifta 5 mg

sildegra 100 mg

vigaroo

flynta 20 mg

viagra 100 mg fiyat

orijinal viagra

viagra 100 mg

eczane viagra

viagra sipariş

viagra satın al

viagra nedir

viagra yorum

viagra fiyat

viagra fiyatı 2021

cialis

cialis fiyat

cialis 20 mg

cialis 100 mg

cialis nedir

cialis 5 mg fiyatı

cialis hap

cialis 5 mg

cialis 20 mg eczane fiyatı

cialis 100 mg fiyat

cialis satış

izmir escort
ataşehir escort
pendik aktar