Laravel login using google – Laravel 5.4 login using google oauth apiclient example
In this post we will show you how to Laravel login using google, we will show you how to Laravel 5.4 login with google oauth apiclient example.
for Laravel login using google, To manifest using google we would like client id, client secret and api key thus we need to have these credentials before process with login using google.
Here victimization this code, we’ll be able to login and register via google.
Step 1 : Install Laravel 5.4 for Laravel login using google
If Laravel isn’t put in in your system then initial install with following command and acquire recent Laravel project.
// run this composer create-project --prefer-dist laravel/laravel blog
Step 2 : produce users table and model for Laravel login using google
Now we’ll produce a User table in your information, initial bear with PHP trained worker command to form migration file for user table.
So first run this command :
// run this php artisan make:migration create_users_table
After hearth this command we’ll see a migration come in database/migrations
. we’ll merely place following code in your migration file to form user table.
// run migrations for users table for Laravel login using google
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateNewUsersTable extends Migration
{
/**
* Run the migrations for CreateNewUsersTable.
*
* @return void
*/
// function up
public function up()
{
// create table
Schema::create(‘users’, function (Blueprint $table) {
// users id
$table->increments(‘id’);
// users name
$table->string(‘name’);
// users email
$table->string(’email’)->unique();
// users password
$table->string(‘password’);
// users rememberToken
$table->rememberToken();
// users timestamps
$table->timestamps();
});
}
/**
* Reverse the migrations for CreateNewUsersTable.
*
* @return void
*/
// function down
public function down()
{
// Schema for users
Schema::drop(‘users’);
}
}
Save this migration file and run following command.
// run this php artisan migrate
app/User.php
namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { /** * The attributes that are mass assignable. * * @var array */ protected $fillable = [ 'name', 'email', 'password', ]; /** * The attributes that should be hidden for arrays. * * @var array */ protected $hidden = [ 'password', 'remember_token', ]; }
Step 3: Update Composer File to add Google apiclient libraries for Laravel login using google
Put following line in over composer file and update over composer to download package:
// run this "google/apiclient": "2.0.*"
Step 4: Route File for Laravel login using google
Now add following routes in over routes/web.php
// googlelogin - google login Route::get('googlelogin',array('as'=>'googlelogin','uses'=>'UserGoogleController@googleNewLogin')) ; // google-user - google get user detail Route::get('google-user',array('as'=>'user.glist','uses'=>'UserGoogleController@listNewGoogleUser')) ;
Step 5: User Controller for Laravel login using google
Now create User Controller file in following path app/Http/Controllers/
app/Http/Controllers/UserGoogleController.php
<?php // Controller for Laravel login using google namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Http\Controllers\Controller; use App\User; class UserGoogleController extends Controller { public function googleNewLogin(Request $user_request) { $google_redirect_url = route('googlelogin'); $goole_client = new \Google_Client(); $goole_client->setApplicationName(config('services.google.app_name')); $goole_client->setClientId(config('services.google.client_id')); $goole_client->setClientSecret(config('services.google.client_secret')); $goole_client->setRedirectUri($google_redirect_url); $goole_client->setDeveloperKey(config('services.google.api_key')); $goole_client->setScopes(array( 'https://www.googleapis.com/auth/plus.me', 'https://www.googleapis.com/auth/userinfo.email', 'https://www.googleapis.com/auth/userinfo.profile', )); $get_google_oauthV2 = new \Google_Service_Oauth2($goole_client); if ($user_request->get('code')){ $goole_client->authenticate($user_request->get('code')); $user_request->session()->put('token', $goole_client->getAccessToken()); } if ($user_request->session()->get('token')) { $goole_client->setAccessToken($user_request->session()->get('token')); } if ($goole_client->getAccessToken()) { //For logged in user, get details from google using access token $goole_user = $get_google_oauthV2->userinfo->get(); $user_request->session()->put('name', $goole_user['name']); if ($set_user =User::where('email',$goole_user['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 $get_authUrl = $goole_client->createAuthUrl(); return redirect()->to($get_authUrl); } } public function listNewGoogleUser(Request $user_request) { // get valu of user DESC order $get_users = User::orderBy('id','DESC')->paginate(5); // return view of user return view('users.list',compact('users'))->with('i', ($user_request->input('page', 1) - 1) * 5);; } }
Step 6: Create Laravel Blade File to list User who login with google for Laravel login with google
@extends('layouts.default') @section('content') <div class="row gdata"> <div class="col-lg-12 margin-tb gdata"> <div class="pull-left gdata"> <h2>Laravel login using google - onlinecode</h2> </div> <div class="pull-right gdata"> <a class="btn btn-danger gdata" href="{{ route('googlelogin') }}"> Google Login</a> </div> </div> </div> <h2>Laravel login using google</h2> <table class="table table-bordered gdata"> <tr> <th>No</th> <th>Name</th> <th>Login Time</th> </tr> @foreach ($get_users as $val_user) <tr> <td>{{ ++$i }}</td> <td>{{ $val_user->name }}</td> <td>{{ $val_user->updated_at->diffForHumans() }}</td> </tr> @endforeach </table> {!! $get_users->render() !!} @endsection
Laravel Load More Data on Page Scroll AND Laravel Custom Helpers AND how to laravel latest version