HITENDRA PATEL

LOGIN WITH GOOGLE IN LARAVEL

Published 4 years ago 1831 Views
image

In this tutorial we are going to add authentication via Google.

We’re going to use Socialite.

Step 1 : Install Package

Install following package from official laravel site

composer require laravel/socialite

Step 2 : Add provider

Add service provider & alias in config file

<?php
return [
    "providers" => [
        Laravel\Socialite\SocialiteServiceProvider::class,
    ],
    "aliases"   => [
        "Socialite" => Laravel\Socialite\Facades\Socialite::class,
    ],
];

Step 3 : Config file

These providers follow the OAuth 2.0.

Google authenticator require a client_id, client_secret and redirect url.

First, add the values to the config file because socialite will be looking for them in following file : config/services.php

<?php
return [
    "google" => [
        "client_id"     => env("GOOGLE_CLIENT_ID"),
        "client_secret" => env("GOOGLE_CLIENT_SECRET"),
        "redirect"      => "/auth/google/callback",
    ],
];

Step 4 : Create OAuth client ID in Google

Create project in google console & create OAuth client ID form following link : 

https://console.developers.google.com/apis/credentials

and add created credential step 3


Step 5 : Add Routes

add following routes in routes/web.php

<?php
Route::get("/auth/google/redirect", "Front\AuthGoogleController@redirect");
Route::get("/auth/google/callback", "Front\AuthGoogleController@callback");

Step 6 : Create controller

Create controller & add following methods in that.

<?php
namespace App\Http\Controllers\Front;
use App\Http\Controllers\Controller;
use App\User;
use Auth;
use Exception;
use Socialite;

class AuthGoogleController extends Controller {
    public function redirect() {
        return Socialite::driver("google")->redirect();
    }
    public function callback() {
        try {
            /*...get data from google...*/
            $googleUserDetail = Socialite::driver("google")->user();
            /*...user detail...*/
            $userDetail = User::where("email", $googleUserDetail->email)->first();
            if (empty($userDetail) || $userDetail->count() < 0) {
                /*...create new user...*/
                $userDetail            = new User;
                $userDetail->name      = $googleUserDetail->name;
                $userDetail->email     = $googleUserDetail->email;
                $userDetail->google_id = $googleUserDetail->id;
                $userDetail->password  = bcrypt($googleUserDetail->email);
                $userDetailSave        = $userDetail->save();
                if ($userDetailSave) {
                    return redirect()->route("admin_login")->with(["error-message" => "Something went wrong! Please try again."]);
                }
            }
            Auth::loginUsingId($userDetail->id);
            return redirect()->route("admin_profile_view");
        } catch (Exception $e) {
            return redirect()->route("admin_login")->with(["error-message" => "Something went wrong! Please try again."]);
        }
    }
}

That's all friends.

A BLOG ABOUT DEVELOPMENT

Subscribe and get my latest blog post in your inbox.
image
DIGITAL OCEAN SETUP LARAVEL

Install & configure laravel project into live digital ocean server.

image
LARAVEL PUSH NOTIFICATION

Laravel push notification for devices with FCM

image
INSTALL COMPOSER ON MACOS

Install Composer on MacOS