├── tests ├── .gitkeep ├── FunctionalTestCase.php ├── OAuthManagerTest.php ├── AuthenticatorTest.php └── IdentityStoreTest.php ├── .gitignore ├── .travis.yml ├── src ├── Facades │ └── OAuth.php ├── IdentityStore.php ├── Session.php ├── UserStore.php ├── OAuthIdentity.php ├── EloquentIdentityStore.php ├── OAuthManager.php └── Authenticator.php ├── phpunit.xml ├── todo.md ├── composer.json └── readme.md /tests/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor 2 | composer.phar 3 | composer.lock 4 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | 3 | php: 4 | - 5.5 5 | - 5.6 6 | 7 | before_script: 8 | - curl -s http://getcomposer.org/installer | php 9 | - php composer.phar install --dev 10 | 11 | script: phpunit 12 | -------------------------------------------------------------------------------- /src/Facades/OAuth.php: -------------------------------------------------------------------------------- 1 | store = $store; 12 | } 13 | 14 | public function get($key) 15 | { 16 | return $this->store->get($key); 17 | } 18 | 19 | public function put($key, $value) 20 | { 21 | return $this->store->put($key, $value); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/UserStore.php: -------------------------------------------------------------------------------- 1 | model = $model; 10 | } 11 | 12 | public function create() 13 | { 14 | $user = new $this->model; 15 | return $user; 16 | } 17 | 18 | public function store($user) 19 | { 20 | return $user->save(); 21 | } 22 | 23 | public function findByIdentity($identity) 24 | { 25 | return $identity->belongsTo($this->model, 'user_id')->firstOrFail(); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 13 | 14 | 15 | ./tests/ 16 | 17 | 18 | -------------------------------------------------------------------------------- /src/OAuthIdentity.php: -------------------------------------------------------------------------------- 1 | where('provider_user_id', $providerUser->id) 9 | ->first(); 10 | } 11 | 12 | public function flush($user, $provider) 13 | { 14 | OAuthIdentity::where('user_id', $user->getKey()) 15 | ->where('provider', $provider) 16 | ->delete(); 17 | } 18 | 19 | public function store($identity) 20 | { 21 | $identity->save(); 22 | } 23 | 24 | public function userExists($provider, $providerUser) 25 | { 26 | return (bool) $this->getByProvider($provider, $providerUser); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "adamwathan/eloquent-oauth", 3 | "description": "Stupid simple OAuth authentication with Laravel and Eloquent", 4 | "authors": [ 5 | { 6 | "name": "Adam Wathan", 7 | "email": "adam.wathan@gmail.com" 8 | } 9 | ], 10 | "license": "MIT", 11 | "require": { 12 | "php": ">=5.5.0", 13 | "illuminate/auth": "4.*|5.*|6.*", 14 | "illuminate/session": "4.*|5.*|6.*", 15 | "illuminate/database": "4.*|5.*|6.*", 16 | "illuminate/http": "4.*|5.*|6.*", 17 | "illuminate/routing": "4.*|5.*|6.*", 18 | "illuminate/support": "4.*|5.*|6.*", 19 | "socialnorm/socialnorm": "^0.2", 20 | "socialnorm/facebook": "^0.2", 21 | "socialnorm/github": "^0.2", 22 | "socialnorm/linkedin": "^0.2", 23 | "socialnorm/google": "^0.2", 24 | "socialnorm/instagram": "^0.2", 25 | "socialnorm/soundcloud": "^0.2" 26 | }, 27 | "require-dev": { 28 | "mockery/mockery": "~0.9", 29 | "phpunit/phpunit": "^4.8" 30 | }, 31 | "autoload": { 32 | "psr-4": { 33 | "AdamWathan\\EloquentOAuth\\": "src/" 34 | }, 35 | "classmap": [ 36 | "tests/FunctionalTestCase.php" 37 | ] 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /tests/FunctionalTestCase.php: -------------------------------------------------------------------------------- 1 | configureDatabase(); 10 | $this->migrateIdentitiesTable(); 11 | } 12 | 13 | protected function configureDatabase() 14 | { 15 | $db = new DB; 16 | $db->addConnection(array( 17 | 'driver' => 'sqlite', 18 | 'database' => ':memory:', 19 | 'charset' => 'utf8', 20 | 'collation' => 'utf8_unicode_ci', 21 | 'prefix' => '', 22 | )); 23 | $db->bootEloquent(); 24 | $db->setAsGlobal(); 25 | } 26 | 27 | public function migrateIdentitiesTable() 28 | { 29 | DB::schema()->create('oauth_identities', function($table) { 30 | $table->increments('id'); 31 | $table->integer('user_id')->unsigned(); 32 | $table->string('provider_user_id'); 33 | $table->string('provider'); 34 | $table->string('access_token'); 35 | $table->timestamps(); 36 | }); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/OAuthManager.php: -------------------------------------------------------------------------------- 1 | redirect = $redirect; 12 | $this->authenticator = $authenticator; 13 | $this->socialnorm = $socialnorm; 14 | } 15 | 16 | public function authorize($providerAlias) 17 | { 18 | return $this->redirect->to($this->socialnorm->authorize($providerAlias)); 19 | } 20 | 21 | public function login($providerAlias, $callback = null) 22 | { 23 | $details = $this->socialnorm->getUser($providerAlias); 24 | return $this->authenticator->login($providerAlias, $details, $callback, $remember = false); 25 | } 26 | 27 | public function loginForever($providerAlias, $callback = null) 28 | { 29 | $details = $this->socialnorm->getUser($providerAlias); 30 | return $this->authenticator->login($providerAlias, $details, $callback, $remember = true); 31 | } 32 | 33 | public function registerProvider($alias, $provider) 34 | { 35 | $this->socialnorm->registerProvider($alias, $provider); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /tests/OAuthManagerTest.php: -------------------------------------------------------------------------------- 1 | buildRedirector(); 20 | $authenticator = M::mock('AdamWathan\EloquentOAuth\Authenticator'); 21 | $socialnorm = M::mock('SocialNorm\SocialNorm'); 22 | $socialnorm->shouldReceive('authorize')->with('example')->andReturn('http://example.com/authorize'); 23 | 24 | $oauth = new OAuthManager($redirector, $authenticator, $socialnorm); 25 | $response = $oauth->authorize('example'); 26 | $this->assertEquals('http://example.com/authorize', $response->getTargetUrl()); 27 | } 28 | 29 | public function test_it_logs_the_user_in() 30 | { 31 | $providerAlias = 'twitbook'; 32 | $socialnormUser = new SocialNorm\User([]); 33 | $callback = function () {}; 34 | 35 | $redirector = $this->buildRedirector(); 36 | 37 | $authenticator = M::spy('AdamWathan\EloquentOAuth\Authenticator'); 38 | 39 | $socialnorm = M::mock('SocialNorm\SocialNorm'); 40 | $socialnorm->shouldReceive('getUser') 41 | ->with($providerAlias) 42 | ->andReturn($socialnormUser); 43 | 44 | $oauth = new OAuthManager($redirector, $authenticator, $socialnorm); 45 | $oauth->login($providerAlias, $callback); 46 | 47 | $authenticator->shouldHaveReceived('login')->with($providerAlias, $socialnormUser, $callback, false); 48 | } 49 | 50 | private function buildRedirector() 51 | { 52 | return new Redirector(new UrlGenerator(new RouteCollection, new Request)); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/Authenticator.php: -------------------------------------------------------------------------------- 1 | auth = $auth; 12 | $this->users = $users; 13 | $this->identities = $identities; 14 | } 15 | 16 | public function login($providerAlias, $userDetails, $callback = null, $remember = false) 17 | { 18 | $user = $this->getUser($providerAlias, $userDetails); 19 | $user = $this->runCallback($callback, $user, $userDetails); 20 | $this->updateUser($user, $providerAlias, $userDetails); 21 | $this->auth->login($user, $remember); 22 | } 23 | 24 | protected function getUser($providerAlias, $details) 25 | { 26 | if ($this->identities->userExists($providerAlias, $details)) { 27 | return $this->getExistingUser($providerAlias, $details); 28 | } 29 | return $this->users->create(); 30 | } 31 | 32 | protected function runCallback($callback, $user, $userDetails) 33 | { 34 | $callback = $callback ?: function () {}; 35 | $callbackUser = $callback($user, $userDetails); 36 | return $callbackUser ?: $user; 37 | } 38 | 39 | protected function updateUser($user, $providerAlias, $details) 40 | { 41 | $this->users->store($user); 42 | $this->storeProviderIdentity($user, $providerAlias, $details); 43 | } 44 | 45 | protected function getExistingUser($providerAlias, $details) 46 | { 47 | $identity = $this->identities->getByProvider($providerAlias, $details); 48 | return $this->users->findByIdentity($identity); 49 | } 50 | 51 | protected function storeProviderIdentity($user, $providerAlias, $details) 52 | { 53 | if ($this->identities->userExists($providerAlias, $details)) { 54 | $this->updateProviderIdentity($providerAlias, $details); 55 | } else { 56 | $this->addProviderIdentity($user, $providerAlias, $details); 57 | } 58 | } 59 | 60 | protected function updateProviderIdentity($providerAlias, $details) 61 | { 62 | $identity = $this->identities->getByProvider($providerAlias, $details); 63 | $identity->access_token = $details->access_token; 64 | $this->identities->store($identity); 65 | } 66 | 67 | protected function addProviderIdentity($user, $providerAlias, $details) 68 | { 69 | $identity = new OAuthIdentity; 70 | $identity->user_id = $user->getKey(); 71 | $identity->provider = $providerAlias; 72 | $identity->provider_user_id = $details->id; 73 | $identity->access_token = $details->access_token; 74 | $this->identities->store($identity); 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /tests/AuthenticatorTest.php: -------------------------------------------------------------------------------- 1 | shouldIgnoreMissing(); 23 | 24 | $identities->shouldReceive('userExists')->andReturn(false); 25 | $users->shouldReceive('create')->andReturn($user); 26 | 27 | $authenticator = new Authenticator($auth, $users, $identities); 28 | $authenticator->login('provider', $userDetails); 29 | 30 | $users->shouldHaveReceived('create'); 31 | $users->shouldHaveReceived('store')->with($user); 32 | $identities->shouldHaveReceived('store'); 33 | $auth->shouldHaveReceived('login')->with($user, false); 34 | } 35 | 36 | public function test_login_uses_existing_user_if_matching_user_exists() 37 | { 38 | $providerAlias = 'provider'; 39 | 40 | $userDetails = new SocialNormUser([]); 41 | $user = M::mock('Illuminate\Contracts\Auth\Authenticatable')->shouldIgnoreMissing(); 42 | 43 | $auth = M::spy(); 44 | 45 | $users = M::spy([ 46 | 'findByIdentity' => $user 47 | ]); 48 | 49 | $identities = M::spy([ 50 | 'userExists' => true, 51 | 'getByProvider' => new OAuthIdentity, 52 | ]); 53 | 54 | $authenticator = new Authenticator($auth, $users, $identities); 55 | $authenticator->login('provider', $userDetails); 56 | 57 | $users->shouldNotHaveReceived('create'); 58 | $users->shouldHaveReceived('store')->with($user); 59 | $identities->shouldHaveReceived('store'); 60 | $auth->shouldHaveReceived('login')->with($user, false); 61 | } 62 | 63 | public function test_if_a_user_is_returned_from_the_callback_that_user_is_used() 64 | { 65 | $providerAlias = 'provider'; 66 | 67 | $userDetails = new SocialNormUser([]); 68 | $user = M::mock('Illuminate\Contracts\Auth\Authenticatable')->shouldIgnoreMissing(); 69 | $otherUser = M::mock('Illuminate\Contracts\Auth\Authenticatable')->shouldIgnoreMissing(); 70 | 71 | $auth = M::spy(); 72 | 73 | $users = M::spy([ 74 | 'findByIdentity' => $user 75 | ]); 76 | 77 | $identities = M::spy([ 78 | 'userExists' => true, 79 | 'getByProvider' => new OAuthIdentity, 80 | ]); 81 | 82 | $authenticator = new Authenticator($auth, $users, $identities); 83 | $authenticator->login('provider', $userDetails, function () use ($otherUser) { 84 | return $otherUser; 85 | }); 86 | 87 | $users->shouldNotHaveReceived('create'); 88 | $users->shouldHaveReceived('store')->with($otherUser); 89 | $identities->shouldHaveReceived('store'); 90 | $auth->shouldHaveReceived('login')->with($otherUser, false); 91 | } 92 | 93 | public function test_if_nothing_is_returned_from_the_callback_the_found_or_created_user_is_used() 94 | { 95 | $providerAlias = 'provider'; 96 | 97 | $userDetails = new SocialNormUser([]); 98 | $foundUser = M::mock('Illuminate\Contracts\Auth\Authenticatable')->shouldIgnoreMissing(); 99 | $otherUser = M::mock('Illuminate\Contracts\Auth\Authenticatable')->shouldIgnoreMissing(); 100 | 101 | $auth = M::spy(); 102 | 103 | $users = M::spy([ 104 | 'findByIdentity' => $foundUser 105 | ]); 106 | 107 | $identities = M::spy([ 108 | 'userExists' => true, 109 | 'getByProvider' => new OAuthIdentity, 110 | ]); 111 | 112 | $authenticator = new Authenticator($auth, $users, $identities); 113 | $authenticator->login('provider', $userDetails, function () { 114 | return; 115 | }); 116 | 117 | $users->shouldNotHaveReceived('create'); 118 | $users->shouldHaveReceived('store')->with($foundUser); 119 | $identities->shouldHaveReceived('store'); 120 | $auth->shouldHaveReceived('login')->with($foundUser, false); 121 | } 122 | } 123 | -------------------------------------------------------------------------------- /tests/IdentityStoreTest.php: -------------------------------------------------------------------------------- 1 | 1, 21 | 'provider' => 'facebook', 22 | 'provider_user_id' => 'foobar', 23 | 'access_token' => 'abc123', 24 | )); 25 | OAuthIdentity::create(array( 26 | 'user_id' => 2, 27 | 'provider' => 'facebook', 28 | 'provider_user_id' => 'bazfoo', 29 | 'access_token' => 'def456', 30 | )); 31 | $details = new UserDetails(array( 32 | 'access_token' => 'new-token', 33 | 'id' => 'bazfoo', 34 | 'nickname' => 'john.doe', 35 | 'full_name' => 'John Doe', 36 | 'email' => 'john.doe@example.com', 37 | 'avatar' => 'http://example.com/photos/john_doe.jpg', 38 | )); 39 | $identities = new EloquentIdentityStore; 40 | $identity = $identities->getByProvider('facebook', $details); 41 | $this->assertEquals(2, $identity->user_id); 42 | $this->assertEquals('facebook', $identity->provider); 43 | $this->assertEquals('bazfoo', $identity->provider_user_id); 44 | $this->assertEquals('def456', $identity->access_token); 45 | } 46 | 47 | public function test_get_by_provider_when_no_match() 48 | { 49 | OAuthIdentity::create(array( 50 | 'user_id' => 1, 51 | 'provider' => 'facebook', 52 | 'provider_user_id' => 'foobar', 53 | 'access_token' => 'abc123', 54 | )); 55 | OAuthIdentity::create(array( 56 | 'user_id' => 2, 57 | 'provider' => 'facebook', 58 | 'provider_user_id' => 'bazfoo', 59 | 'access_token' => 'def456', 60 | )); 61 | $details = new UserDetails(array( 62 | 'access_token' => 'new-token', 63 | 'id' => 'missing-id', 64 | 'nickname' => 'john.doe', 65 | 'full_name' => 'John Doe', 66 | 'email' => 'john.doe@example.com', 67 | 'avatar' => 'http://example.com/photos/john_doe.jpg', 68 | )); 69 | $identities = new EloquentIdentityStore; 70 | $identity = $identities->getByProvider('facebook', $details); 71 | $this->assertNull($identity); 72 | } 73 | 74 | public function test_flush() 75 | { 76 | OAuthIdentity::create(array( 77 | 'user_id' => 1, 78 | 'provider' => 'facebook', 79 | 'provider_user_id' => 'foobar', 80 | 'access_token' => 'abc123', 81 | )); 82 | OAuthIdentity::create(array( 83 | 'user_id' => 2, 84 | 'provider' => 'facebook', 85 | 'provider_user_id' => 'bazfoo', 86 | 'access_token' => 'def456', 87 | )); 88 | 89 | $this->assertEquals(1, OAuthIdentity::where('provider', 'facebook')->where('user_id', 2)->count()); 90 | 91 | $identities = new EloquentIdentityStore; 92 | $user = M::mock(); 93 | $user->shouldReceive('getKey')->andReturn(2); 94 | $identities->flush($user, 'facebook'); 95 | 96 | $this->assertEquals(0, OAuthIdentity::where('provider', 'facebook')->where('user_id', 2)->count()); 97 | } 98 | 99 | public function test_store() 100 | { 101 | $identity = new OAuthIdentity(array( 102 | 'user_id' => 1, 103 | 'provider' => 'facebook', 104 | 'provider_user_id' => 'foobar', 105 | 'access_token' => 'abc123', 106 | )); 107 | 108 | $this->assertEquals(0, OAuthIdentity::count()); 109 | 110 | $identities = new EloquentIdentityStore; 111 | $identities->store($identity); 112 | 113 | $this->assertEquals(1, OAuthIdentity::count()); 114 | } 115 | 116 | public function test_user_exists_returns_true_when_user_exists() 117 | { 118 | OAuthIdentity::create(array( 119 | 'user_id' => 2, 120 | 'provider' => 'facebook', 121 | 'provider_user_id' => 'bazfoo', 122 | 'access_token' => 'def456', 123 | )); 124 | $details = new UserDetails(array( 125 | 'access_token' => 'new-token', 126 | 'id' => 'bazfoo', 127 | 'nickname' => 'john.doe', 128 | 'full_name' => 'John Doe', 129 | 'email' => 'john.doe@example.com', 130 | 'avatar' => 'http://example.com/photos/john_doe.jpg', 131 | )); 132 | $identities = new EloquentIdentityStore; 133 | $this->assertTrue($identities->userExists('facebook', $details)); 134 | } 135 | 136 | public function test_user_exists_returns_false_when_user_doesnt_exist() 137 | { 138 | OAuthIdentity::create(array( 139 | 'user_id' => 2, 140 | 'provider' => 'facebook', 141 | 'provider_user_id' => 'foobar', 142 | 'access_token' => 'def456', 143 | )); 144 | $details = new UserDetails(array( 145 | 'access_token' => 'new-token', 146 | 'id' => 'bazfoo', 147 | 'nickname' => 'john.doe', 148 | 'full_name' => 'John Doe', 149 | 'email' => 'john.doe@example.com', 150 | 'avatar' => 'http://example.com/photos/john_doe.jpg', 151 | )); 152 | $identities = new EloquentIdentityStore; 153 | $this->assertFalse($identities->userExists('facebook', $details)); 154 | } 155 | } 156 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | > **Important: This package is not actively maintained.** For bug fixes and new features, please fork. 2 | 3 | # Eloquent OAuth 4 | 5 | [![This Project Has Been Deprecated.](http://www.repostatus.org/badges/0.1.0/abandoned.svg)](http://www.repostatus.org/#abandoned) 6 | [![Code Climate](https://codeclimate.com/github/adamwathan/eloquent-oauth/badges/gpa.svg)](https://codeclimate.com/github/adamwathan/eloquent-oauth) 7 | [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/adamwathan/eloquent-oauth/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/adamwathan/eloquent-oauth/?branch=master) 8 | [![Build Status](https://api.travis-ci.org/adamwathan/eloquent-oauth.svg)](https://travis-ci.org/adamwathan/eloquent-oauth) 9 | 10 | > - Use the [Laravel 4 wrapper](https://github.com/adamwathan/eloquent-oauth-l4) for easy integration with Laravel 4. 11 | > - Use the [Laravel 5 wrapper](https://github.com/adamwathan/eloquent-oauth-l5) for easy integration with Laravel 5. 12 | 13 | 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. 14 | 15 | #### Video Walkthrough 16 | 17 | [![Screenshot](https://cloud.githubusercontent.com/assets/4323180/6274884/ac824c48-b848-11e4-8e4d-531e15f76bc0.png)](https://vimeo.com/120085196) 18 | 19 | #### Basic example 20 | 21 | ```php 22 | // Redirect to Facebook for authorization 23 | Route::get('facebook/authorize', function() { 24 | return OAuth::authorize('facebook'); 25 | }); 26 | 27 | // Facebook redirects here after authorization 28 | Route::get('facebook/login', function() { 29 | 30 | // Automatically log in existing users 31 | // or create a new user if necessary. 32 | OAuth::login('facebook'); 33 | 34 | // Current user is now available via Auth facade 35 | $user = Auth::user(); 36 | 37 | return Redirect::intended(); 38 | }); 39 | ``` 40 | 41 | #### Supported Providers 42 | 43 | - Facebook 44 | - GitHub 45 | - Google 46 | - LinkedIn 47 | - Instagram 48 | - SoundCloud 49 | 50 | >Feel free to open an issue if you would like support for a particular provider, or even better, submit a pull request. 51 | 52 | ## Installation 53 | 54 | Check the appropriate wrapper package for installation instructions for your version of Laravel. 55 | 56 | - [Laravel 4 wrapper](https://github.com/adamwathan/eloquent-oauth-l4) 57 | - [Laravel 5 wrapper](https://github.com/adamwathan/eloquent-oauth-l5) 58 | 59 | ## Usage 60 | 61 | Authentication against an OAuth provider is a multi-step process, but I have tried to simplify it as much as possible. 62 | 63 | ### Authorizing with the provider 64 | 65 | 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. 66 | 67 | To authorize the user, simply return the `OAuth::authorize()` method directly from the route. 68 | 69 | ```php 70 | Route::get('facebook/authorize', function() { 71 | return OAuth::authorize('facebook'); 72 | }); 73 | ``` 74 | 75 | ### Authenticating within your app 76 | 77 | Next you need to define a route for authenticating against your app with the details returned by the provider. 78 | 79 | For basic cases, you can simply call `OAuth::login()` with the provider name you are authenticating with. If the user 80 | rejected your application, this method will throw an `ApplicationRejectedException` which you can catch and handle 81 | as necessary. 82 | 83 | The `login` method will create a new user if necessary, or update an existing user if they have already used your application 84 | before. 85 | 86 | Once the `login` method succeeds, the user will be authenticated and available via `Auth::user()` just like if they 87 | had logged in through your application normally. 88 | 89 | ```php 90 | use SocialNorm\Exceptions\ApplicationRejectedException; 91 | use SocialNorm\Exceptions\InvalidAuthorizationCodeException; 92 | 93 | Route::get('facebook/login', function() { 94 | try { 95 | OAuth::login('facebook'); 96 | } catch (ApplicationRejectedException $e) { 97 | // User rejected application 98 | } catch (InvalidAuthorizationCodeException $e) { 99 | // Authorization was attempted with invalid 100 | // code,likely forgery attempt 101 | } 102 | 103 | // Current user is now available via Auth facade 104 | $user = Auth::user(); 105 | 106 | return Redirect::intended(); 107 | }); 108 | ``` 109 | 110 | If you need to do anything with the newly created user, you can pass an optional closure as the second 111 | argument to the `login` method. This closure will receive the `$user` instance and a `SocialNorm\User` 112 | object that contains basic information from the OAuth provider, including: 113 | 114 | - `id` 115 | - `nickname` 116 | - `full_name` 117 | - `avatar` 118 | - `email` 119 | - `access_token` 120 | 121 | ```php 122 | OAuth::login('facebook', function($user, $details) { 123 | $user->nickname = $details->nickname; 124 | $user->name = $details->full_name; 125 | $user->profile_image = $details->avatar; 126 | $user->save(); 127 | }); 128 | ``` 129 | 130 | > 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. 131 | 132 | ### Advanced: Storing additional data 133 | 134 | 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. 135 | 136 | 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: 137 | 138 | 1. Request it from the provider, by extending its scope: 139 | 140 | Say for example we want to collect the user's gender when they login using Facebook. 141 | 142 | In the `config/eloquent-oauth.php` file, set the `[scope]` in the `facebook` provider section to include the `public_profile` scope, like this: 143 | 144 | ```php 145 | 'scope' => ['email', 'public_profile'], 146 | ``` 147 | 148 | > For available scopes with each provider, consult that provider's API documentation. 149 | 150 | > 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. 151 | 152 | 2. Now where you do your `OAuth::login`, store the to your `$user` object by accessing the `$details->raw()['KEY']` data: 153 | 154 | ```php 155 | OAuth::login('facebook', function($user, $details) ( 156 | $user->gender = $details->raw()['gender']; // Or whatever the key is 157 | $user->save(); 158 | }); 159 | ``` 160 | 161 | > TIP: You can see what the available keys are by testing with `dd($details->raw());` inside that same closure. 162 | 163 | --------------------------------------------------------------------------------