Add Google oauth2 login in your Laravel application

by Ideabuzz

php_google_oauth_login.jpg

Do you want to add a special google login features in your websites or applications? Your user may forget their passwords. So if you add login with google oauth2 in your Laravel application a user can easily login to his account. It is not so very hard. First you need to install socialite in your application. To install go to your project root and run the below command.

composer require laravel/socialite

Now go to config/services.php and the following code

'github' => [
    'client_id' => env('GITHUB_CLIENT_ID'),
    'client_secret' => env('GITHUB_CLIENT_SECRET'),
    'redirect' => 'http://your-callback-url',
],

Add the following key in .env

GOOGLE_CLIENT_ID=
GOOGLE_CLIENT_SECRET=
GOOGLE_REDIRECT=
GOOGLE_API_KEY=

As Laravel works with routes, so after that you need to add routes and controller. I give here a example.

Route::get('login/google', 'LoginController@redirectToProvider')->name('googleLogin');
Route::get('login/google/callback', ' LoginController@handleProviderCallback');

You need to write the following method like this in the controller

public function redirectToProvider()
{
    return Socialite::driver('google')->redirect();
}

public function handleProviderCallback()
{
    $user = Socialite::driver('google')->user();
    return your views.
}

Add a button in your login form with your created route

<a href="{{route('googlelogin')}}" id="google-login-button">
    <button type="button" class="btn btn-info" >
        <i class="fab fa-google-plus-g"></i> Sign in with Google+
    </button>
</a>

This login with google oauth2 in your application help your application user to login easily with using google email account credentials. It will be helpful for increasing users in your applications/website.

How to parse srt file and insert it in WordPress database

Authentication and authorization are often confused with each other but can be more easily understood if you think about them from the perspective of an application.

OAuth2 was designed as an authorization protocol, so the end result of every OAuth2 flow is the app obtains an access token in order to be able to access or modify something about the user’s account. The access token itself says nothing about who the user is.

There are several ways and different services that provide a way for an app to find out the identity of the user.

Leave a Reply