Laravel 5.8 Login with Google Using Socialite Package, Login with Google Laravel 5.8, Login with Google in Laravel 5.8 using Socialite Package
Hi Friends Today, I will tell you through this tutorial how you can login to the Laravel 5.7 Or Laravel 5.8 Framework by Google Plus Social Media Website.
Friends Laravel has a very power full Framework. In today’s day more developer is using the Laravel Framework. Because most functinality is already inbuilt in this framework. We just have to manage it by command. So we were talking that in google plus laravel 5.7 Or Laravel 5.8 How to login.
So Friends, I would like to tell you that Laravel has given a package of itself. The name of this is socialite package. By installing this package in our application, we can easily login from Google Plus. So let’s go, we will tell you step by step and together you will also explain the step of installetion.
Step 1: Make a project of Laravel 5.7 Or Laravel 5.8 Google Plus Login.
Step 2: Then create users table with column.
Step 3: Install Socialite Package For Google Plus
Step 4: add service provider and alias.
Step 5: Create Google App
Step 6: Create New Routes
Step 7: Create New GoogleloginController
Step 8: Create Blade File
Step 1: Make a project of Laravel 5.7 Or Laravel 5.8 Google Plus Login.
First of all we will install laravel 5.7 Or Laravel 5.8 application.The command to install laravel 5.7 or laravel 5.8 is given below.
composer create-project --prefer-dist laravel/laravel socialLogin
Step 2: Then Create users table and column.
Then after that, go to your database and create a table named users and column in it. You have been given the complete query of the users table below. You can select your database and go to sql and paste the query by creating the table.
CREATE TABLE `users` ( `id` int(100) NOT NULL, `fname` varchar(100) NOT NULL, `email` varchar(100) NOT NULL, `google_id` varchar(100) NOT NULL, `password` varchar(100) NOT NULL, `remember_token` varchar(100) NOT NULL, `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, `updated_at` int(100) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Step 3: Install Socialite Package For Google Plus
In this step we will learn to install socialite packege.
composer require laravel/socialite
Step 4: add service provider and alias.
After installing the Socialite Package, we will add the service of provider and alias to the config/app.php file.
'providers' => [ Laravel\Socialite\SocialiteServiceProvider::class, ], 'aliases' => [ 'Socialite' => Laravel\Socialite\Facades\Socialite::class, ],
Step 5: Create Google App
In the third step you will have to create a google app account. You can create this by visiting this link of the Goolgel app. After creating google account you will need cliend id and secret id.
https://console.developers.google.com/
You can create a client id and secret that you want to add to this config/services.php file. You can see below.
config/services.php
return [ .... 'google' => [ 'client_id' => env('GOOGLE_CLIENT_ID'), 'client_secret' => env('GOOGLE_CLIENT_SECRET'), 'redirect' => env('GOOGLE_CALLBACK_URL'), ], ]
Step 6: Pass URL into Routes File
Route::get('googlelogin', function () { return view('googlelogin'); }); Route::get('auth/google', 'GoogleloginController@redirectToGoogle'); Route::get('auth/google/callback', 'GoogleloginController@handleGoogleCallback');
Step 7: Create New GoogleloginController
we need to add new controller and method of google auth that method will handle google callback url and etc, first put bellow code on your GoogleloginController.php file.
app/Http/Controllers/GoogleloginController.php
<?php // GoogleloginController.php namespace App\Http\Controllers; use Illuminate\Http\Request; use Socialite; class GoogleloginController extends Controller { /** * Create a redirect method to google api. * * @return void */ public function redirect() { return Socialite::driver('google')->redirect(); } /** * Return a callback method from google api. * * @return callback URL from google */ public function callback() { $userSocial = Socialite::driver('google')->user(); //return $userSocial; $finduser = User::where('google_id', $userSocial->id)->first(); if($finduser){ Auth::login($finduser); return Redirect::to('/'); }else{ $new_user = User::create([ 'fname' => $userSocial->name, 'email' => $userSocial->email, 'google_id'=> $userSocial->id ]); Auth::login($new_user); return redirect()->back(); } } }
Step 8: Create Blade File
Ok, now at last we need to add blade view so first create new file googlelogin.blade.php file and put bellow code:
resources/views/googlelogin.blade.php
@extends('layouts.app') @section('content') <div class="container"> <div class="row"> <div class="col-md-12 row-block"> <a href="{{ url('auth/google') }}" class="btn btn-lg btn-primary btn-block"> <strong>Login With Google</strong> </a> </div> </div> </div> @endsection