• Stars
    star
    374
  • Rank 114,346 (Top 3 %)
  • Language
    PHP
  • Created almost 11 years ago
  • Updated almost 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Braindead simple social login with Laravel and Eloquent.

Important: This package is not actively maintained. For bug fixes and new features, please fork.

Eloquent OAuth

This Project Has Been Deprecated. Code Climate Scrutinizer Code Quality Build Status

Eloquent OAuth is a package for Laravel designed to make authentication against various OAuth providers ridiculously brain-dead simple. Specify your client IDs and secrets in a config file, run a migration and after that it's just two method calls and you have OAuth integration.

Video Walkthrough

Screenshot

Basic example

// Redirect to Facebook for authorization
Route::get('facebook/authorize', function() {
    return OAuth::authorize('facebook');
});

// Facebook redirects here after authorization
Route::get('facebook/login', function() {
    
    // Automatically log in existing users
    // or create a new user if necessary.
    OAuth::login('facebook');

    // Current user is now available via Auth facade
    $user = Auth::user();

    return Redirect::intended();
});

Supported Providers

  • Facebook
  • GitHub
  • Google
  • LinkedIn
  • Instagram
  • SoundCloud

Feel free to open an issue if you would like support for a particular provider, or even better, submit a pull request.

Installation

Check the appropriate wrapper package for installation instructions for your version of Laravel.

Usage

Authentication against an OAuth provider is a multi-step process, but I have tried to simplify it as much as possible.

Authorizing with the provider

First you will need to define the authorization route. This is the route that your "Login" button will point to, and this route redirects the user to the provider's domain to authorize your app. After authorization, the provider will redirect the user back to your second route, which handles the rest of the authentication process.

To authorize the user, simply return the OAuth::authorize() method directly from the route.

Route::get('facebook/authorize', function() {
    return OAuth::authorize('facebook');
});

Authenticating within your app

Next you need to define a route for authenticating against your app with the details returned by the provider.

For basic cases, you can simply call OAuth::login() with the provider name you are authenticating with. If the user rejected your application, this method will throw an ApplicationRejectedException which you can catch and handle as necessary.

The login method will create a new user if necessary, or update an existing user if they have already used your application before.

Once the login method succeeds, the user will be authenticated and available via Auth::user() just like if they had logged in through your application normally.

use SocialNorm\Exceptions\ApplicationRejectedException;
use SocialNorm\Exceptions\InvalidAuthorizationCodeException;

Route::get('facebook/login', function() {
    try {
        OAuth::login('facebook');
    } catch (ApplicationRejectedException $e) {
        // User rejected application
    } catch (InvalidAuthorizationCodeException $e) {
        // Authorization was attempted with invalid
        // code,likely forgery attempt
    }

    // Current user is now available via Auth facade
    $user = Auth::user();

    return Redirect::intended();
});

If you need to do anything with the newly created user, you can pass an optional closure as the second argument to the login method. This closure will receive the $user instance and a SocialNorm\User object that contains basic information from the OAuth provider, including:

  • id
  • nickname
  • full_name
  • avatar
  • email
  • access_token
OAuth::login('facebook', function($user, $details) {
    $user->nickname = $details->nickname;
    $user->name = $details->full_name;
    $user->profile_image = $details->avatar;
    $user->save();
});

Note: The Instagram and Soundcloud APIs do not allow you to retrieve the user's email address, so unfortunately that field will always be null for those provider.

Advanced: Storing additional data

Remember: One of the goals of the Eloquent OAuth package is to normalize the data received across all supported providers, so that you can count on those specific data items (explained above) being available in the $details object.

But, each provider offers its own sets of additional data. If you need to access or store additional data beyond the basics of what Eloquent OAuth's default ProviderUserDetails object supplies, you need to do two things:

  1. Request it from the provider, by extending its scope:

    Say for example we want to collect the user's gender when they login using Facebook.

    In the config/eloquent-oauth.php file, set the [scope] in the facebook provider section to include the public_profile scope, like this:

       'scope' => ['email', 'public_profile'],

For available scopes with each provider, consult that provider's API documentation.

NOTE: By increasing the scope you will be asking the user to grant access to additional information. They will be informed of the scopes you're requesting. If you ask for too much unnecessary data, they may refuse. So exercise restraint when requesting additional scopes.

  1. Now where you do your OAuth::login, store the to your $user object by accessing the $details->raw()['KEY'] data:
       OAuth::login('facebook', function($user, $details) (
           $user->gender = $details->raw()['gender']; // Or whatever the key is
           $user->save();
       });

TIP: You can see what the available keys are by testing with dd($details->raw()); inside that same closure.

More Repositories

1

laracon2017

PHP
469
star
2

workcation

Vue
425
star
3

bootforms

Rapid form generation with Bootstrap 3 and Laravel.
PHP
419
star
4

vue-tailwind-examples

Vue
390
star
5

laravel-preset

PHP
277
star
6

sublime-phpunit

Run individual unit test files directly from Sublime
Python
276
star
7

theming-tailwind-demo

HTML
267
star
8

form

Super basic form HTML builder, only really exists so I can pull it in for some other more useful projects.
PHP
232
star
9

eloquent-oauth-l5

Eloquent OAuth service provider and assets for Laravel 5
PHP
214
star
10

tailwind-take-home-project

Vue
132
star
11

vue-shopify-sortable-demo

HTML
113
star
12

fullstackradio.com

JavaScript
90
star
13

test-driven-laravel-from-scratch

PHP
81
star
14

tailwind-css-variable-text-opacity-demo

JavaScript
77
star
15

entypo-optimized

61
star
16

tailwindcss-dark-mode-prototype

JavaScript
58
star
17

workcation-theme-ui

JavaScript
38
star
18

vueconfto-demo

Vue
38
star
19

rebuilding-netlify

JavaScript
32
star
20

sublime-themes

Personal mods for ST themes I use
31
star
21

tailwind-animation-playground

HTML
27
star
22

laracon-madrid

The source code for my "Advanced Vue Component Design" at Laracon EU Madrid.
Vue
27
star
23

cra-tailwind-3

HTML
27
star
24

tailwind-jit-vite-example

Vue
25
star
25

tailwind-starter

CSS
24
star
26

rebuilding-allbirds-with-tailwindcss

JavaScript
24
star
27

laracon-2020

Vue
24
star
28

faktory

An attempt to bring the wonders of FactoryGirl to PHP.
PHP
22
star
29

rebuilding-transistor

JavaScript
22
star
30

tailwindcss-jit-next

JavaScript
17
star
31

tailwind-ui-navbar-react

JavaScript
16
star
32

nuxt-storybook

Vue
16
star
33

workcation-splash-page

HTML
15
star
34

vue-sidebar-layout

Vue
15
star
35

next10-tailwind

JavaScript
14
star
36

cra-tailwind-demo

JavaScript
12
star
37

next-nested-routing

JavaScript
12
star
38

laracon-online-2019

PHP
10
star
39

jit-vite-preact

TypeScript
9
star
40

nuxt-nested-layouts

Vue
9
star
41

tweeter-eu

PHP
8
star
42

tailwind-jit-cra-example

JavaScript
7
star
43

tailwind-cra-next

JavaScript
7
star
44

jit-postcss-demo

HTML
7
star
45

vue-rename-tailwind-config

JavaScript
7
star
46

activerest

ActiveRecord wrapper for REST APIs
PHP
7
star
47

headbangstagram

JavaScript
6
star
48

postcss8-tailwind

JavaScript
6
star
49

dotfiles

JavaScript
6
star
50

solid-tailwind-setup-example

JavaScript
5
star
51

tailwind-browserslist-test

HTML
5
star
52

tailwind-color-name-test

JavaScript
5
star
53

facktory

An attempt to bring the wonders of FactoryGirl to PHP
PHP
5
star
54

phxtailwindui

Elixir
4
star
55

tw3-cra-typescript-example

HTML
4
star
56

tw-devtools-perf

Vue
4
star
57

tailwind-color-function-demo

JavaScript
4
star
58

socialnorm

PHP
4
star
59

eloquent-oauth-l4

Eloquent OAuth service provider and assets for Laravel 4
PHP
4
star
60

tailwind-2-aspect-ratio

CSS
4
star
61

upgrade-config

JavaScript
4
star
62

next-cssnano-issue

CSS
3
star
63

sveltekit-forms-esm-demo

Svelte
3
star
64

at-apply-stuck

JavaScript
3
star
65

game-of-life

HTML
3
star
66

tailwind-plugin-color-extend-example

JavaScript
3
star
67

vite-issue-repro

Vue
3
star
68

micheltazartes

JavaScript
3
star
69

lets-refactor-zendcon

PHP
3
star
70

tailwindcss-i2369

JavaScript
2
star
71

tailwind-repro-8597

HTML
2
star
72

base-comment-delete-test

CSS
2
star
73

focus-visible-test

CSS
2
star
74

socialnorm-google

Google provider for SocialNorm
PHP
2
star
75

eloquent-oauth-example

Basic example Laravel app with minimal setup required for Eloquent OAuth
PHP
2
star
76

issue-10419

JavaScript
2
star
77

tailwind-repro-8661

CSS
2
star
78

vue-tailwind-library

Vue
1
star
79

thready-site

HTML
1
star
80

next-color-import-test

JavaScript
1
star
81

next-tailwind-hot-reload-issue

JavaScript
1
star
82

tw-postcss7-purge-test

CSS
1
star
83

tailwind-issue-7685

HTML
1
star
84

simple-design-ssphp16

PHP
1
star
85

vite-vue2-jit-issue

HTML
1
star
86

gulp-postcss-dependency-bug

JavaScript
1
star
87

vagrant-php

Just a little provisioning script and Vagrantfile, I'm sure I'll refine it over time.
Shell
1
star
88

parcel-2-postcss-dependency-bug

JavaScript
1
star
89

tailwindcss-forms-class-strategy-test

CSS
1
star
90

tw-postcss8-issue

JavaScript
1
star
91

eloquent-oauth-l5-example

PHP
1
star
92

larawedding

PHP
1
star
93

socialnorm-linkedin

LinkedIn provider for SocialNorm
PHP
1
star
94

postcss-normalize-bug

CSS
1
star
95

postcss-use-bug

JavaScript
1
star
96

vite-jsx-build-fail

Vue
1
star
97

turbopack-test

CSS
1
star
98

metamarkdown

PHP
1
star