Git Product home page Git Product logo

laravel-google-auth's Introduction

This is a fork of

The Monkeys

Laravel Google Authentication Driver

This driver Allows you to use Google to authenticate users of your Laravel application.

[JScarton] I've started this fork because I want to adapt the code for a new oauth driver for special purpose

Installation

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:

	"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/"]
        }
      }
		},
		{
      "type": "package",
      "package": {
        "name": "jscarton/laravel-google-auth",
        "version": "0.1.0",
        "dist": {
          "url": "https://github.com/jscarton/laravel-google-auth/archive/master.zip",
          "type": "zip"
        },
        "autoload": {
          "classmap": ["src/"]
        }
      }
		}
	],
composer require google/google-api-php-client --no-update
composer require jscarton/laravel-google-auth --no-update
composer update

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.

Delete the line for the AuthServiceProvider:

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

and replace it with:

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

To configure the package, you can use the following command to copy the configuration file to app/config/packages/jscarton/laravel-google-auth.

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

Or you can just create a new file in that folder and only override the settings you need.

The settings themselves are documented inside config.php.

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

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:

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:

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.

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

<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.

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:

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:

    <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>

License

MIT License (c) [JScarton]

laravel-google-auth's People

Contributors

felthy avatar grayxr avatar jscarton avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.