2017 © Pedro Peláez
 

library laravel-google-auth

Laravel Authentication Driver for Google OAuth authentication

image

abidra/laravel-google-auth

Laravel Authentication Driver for Google OAuth authentication

  • Saturday, June 10, 2017
  • by abid
  • Repository
  • 0 Watchers
  • 0 Stars
  • 4 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 13 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

The Monkeys, (*1)

Laravel Google Authentication Driver

Allows you to use Google to authenticate users of your Laravel application., (*2)

Installation

To get the latest version of cachebuster simply require it in your composer.json file., (*3)

Note: This package depends on a non-packagist package, google-api-php-client, so you will need to manually add the following repository definition to your project's composer.json file before attempting to run composer update or composer install:, (*4)

    "repositories": [
        {
      "type": "package",
      "package": {
        "name": "google/google-api-php-client",
        "version": "0.6.7",
        "dist": {
          "url": "http://google-api-php-client.googlecode.com/files/google-api-php-client-0.6.7.tar.gz",
          "type": "tar"
        },
        "autoload": {
          "classmap": ["src/"]
        }
      }
        }
    ],
composer require themonkeys/laravel-google-auth:dev-master --no-update
composer update themonkeys/laravel-google-auth

Once the package is installed you need to register the service provider with the application. Open up app/config/app.php and find the providers key., (*5)

Delete the line for the AuthServiceProvider:, (*6)

'providers' => array(
        'Illuminate\Auth\AuthServiceProvider',
)

and replace it with:, (*7)

'providers' => array(
    'Themonkeys\LaravelGoogleAuth\LaravelGoogleAuthServiceProvider',
)

To configure the package, you can use the following command to copy the configuration file to app/config/packages/themonkeys/laravel-google-auth., (*8)

php artisan config:publish themonkeys/laravel-google-auth

Or you can just create a new file in that folder and only override the settings you need., (*9)

The settings themselves are documented inside config.php., (*10)

To make your configuration apply only to a particular environment, put your configuration in an environment folder such as app/config/packages/themonkeys/laravel-google-auth/environment-name/config.php., (*11)

Usage

To enable Google-based authentication for your app, you first need to select the 'google' authentication driver. Open up app/config/auth.php and edit the driver key:, (*12)

return array(
    'driver' => 'google',
);

For Google authentication, you need to add a Login page to your app which contains a link for the user to click on that will initiate the authentication process. The simplest way of doing that is to add the following to your routes.php file:, (*13)

Route::get('/login', function() {
    return View::make('login', array(
      'authUrl' => Auth::getAuthUrl()
    ));
});

Note: the getAuthUrl() is not present in other authentication drivers, so the above code will throw an error with other drivers., (*14)

Then use {{ $authUrl }} as the href for a link in your login.blade.php view:, (*15)

<a class='login' href='{{ $authUrl }}'>Connect Me!</a>

Then you need to add the 'before' filter 'google-finish-authentication' to the route that google redirects to after authentication is complete. Make sure this filter is applied first, before the 'auth' filter - otherwise the 'auth' filter will send the user back to the login page and their session will be lost., (*16)

Route::group(array('before' => array('google-finish-authentication', 'auth')), function() {
    Route::get('/', 'HomeController@showWelcome');
});

Adding a logout facility to your app is the same as with any other authentication driver - just add the following to your routes.php, then add a link to the URI /logout wherever you need it:, (*17)

Route::get('/logout', function() {
    Auth::logout();
    return Redirect::to('/');
});

All information available in the Google_Userinfo object is available via the user object returned from Auth::user(), for example Auth::user()->name:, (*18)

    <table>
        <tr>
            <th>Your ID:</th><td>{{ Auth::user()->id }}</td>
        </tr>
        <tr>
            <th>Your Full Name:</th><td>{{ Auth::user()->name }}</td>
        </tr>
        <tr>
            <th>Your Given Name:</th><td>{{ Auth::user()->given_name }}</td>
        </tr>
        <tr>
            <th>Your Family Name:</th><td>{{ Auth::user()->family_name }}</td>
        </tr>
        <tr>
            <th>Your Email Address:</th><td>{{ Auth::user()->email }}</td>
        </tr>
        <tr>
            <td></td>
            <td>Your Email Address has
                @if (Auth::user()->verified_email)
                been <strong>verified</strong>
                @else
                <strong>not</strong> been verified
                @endif
            </td>
        </tr>
        <tr>
            <th>Your hosted domain:</th><td>{{ Auth::user()->hd }}</td>
        </tr>
        <tr>
            <th>Your Locale:</th><td>{{ Auth::user()->locale }}</td>
        </tr>
    </table>

Contribute

In lieu of a formal styleguide, take care to maintain the existing coding style., (*19)

License

MIT License (c) The Monkeys, (*20)

The Versions

10/06 2017

dev-master

9999999-dev

Laravel Authentication Driver for Google OAuth authentication

  Sources   Download

MIT

The Requires

 

by Abid Rakhmansyah

laravel authentication auth google login oauth2