Download the PHP package audunru/social-accounts without Composer
On this page you can find all versions of the php package audunru/social-accounts. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package social-accounts
Social Accounts for Laravel
Add social login (Google, Facebook, and others) to your Laravel app.
This package uses Laravel Socialite to authenticate users, and takes care of storing the provider (eg. Google) and provider user ID (eg. 123456789) as a SocialAccount (a related model of the User model).
Your users can add one or more social logins to their account. It's up to you if you want them to sign up with a normal username and password first, or if they can sign up just by signing in with a provider.
The package also has a JSON API so you can display which social accounts a user has logged in with, and allow them to remove them.
Installation
Step 1: Install with Composer
Step 2: Make changes to your code
First, you must add the HasSocialAccounts
trait to your User
model:
Second, you need to specify which providers you are going to support. Publish the configuration, and open up config/social-accounts.php
and add them to the array.
There is an array called "providers" where you can specify the ones you want:
Third, you need to add credentials for your supported social login providers to config/services.php
. To login with Google, you would add the following to config/services.php
:
Fourth, you should call the SocialAccounts::routes
method within the boot method of your AuthServiceProvider. This method will register the routes necessary to login with your configured providers. It will also register the API routes necessary for a user to retrieve their social accounts and remove them.
Optional: You can add web and API routes in separate steps.
Step 3: Configuration and customization
You can find the configuration and documentation of all options in config/social-accounts.php.
Step 4: Run migrations
The migrations will create a social_accounts
table, which will hold all added social accounts.
If you set the "automatically_create_users" option in config/social-accounts.php
to true
, the email
and password
columns in your users
table will be made nullable. Not all providers require users to have an email address, and the password
column must be nullable because users who sign up this way won't have password.
Usage
Adding social login to existing users
If you want to allow your existing users to log in with Google, add a link to /social-accounts/login/google
somewhere in your application:
After clicking on this link, the user will be redirected to Google, where they must authorize the request. Afterwards they will be returned to your app. Then, a new SocialAccount
will be added as related model of the User
.
Signing up users
If you want to allow users to sign up with this package, you must first publish the configuration file and then set automatically_create_users
to true
.
Then, run the migrations so that the email and password columns are made nullable.
Then add a link to /social-accounts/login/google
:
Logging in
For users who are not signed in, simply add a link to /social-accounts/login/google
:
Remember Me
Add "remember" to the login URL to keep the user signed in:
API
The JSON API, which by default is accessible at /social-accounts
, allows authenticated users to retrieve their social accounts and remove them.
To retrieve an array of social accounts, make a GET request to /social-accounts
.
To retrieve a single social account, make a GET request to /social-accounts/123
, where 123 is the ID of the account.
To delete a social account, make a DELETE request to /social-accounts/123
.
Users can't update social accounts through the API, they will have to delete them first and then authorize again.
Optional Parameters, Access Scopes and Stateless Authentication
To include optional parameters in the request, call the registerProviderSettings
method on the facade. The method takes three parameters, the provider name, the name of the method to call on the provider object, and an array of parameters.
For example, you can use this to limit the domains a Google user can choose to sign in with to just one:
Gates
You can use gates to allow or deny certain actions. Gates should be defined within the boot method of your AuthServiceProvider.
Events
When a user is created after authenticating with a provider, a SocialUserCreated
event is dispatched. The event receives the User, SocialAccount and ProviderUser model.
When a social account is added to an existing user, a SocialAccountAdded
event is dispatched. The event receives the User, SocialAccount and ProviderUser model.
For instance, you could listen for an event and grab more details about the user.
Alternatives
Development
Testing
Run tests:
All versions of social-accounts with dependencies
laravel/framework Version ^11.0
laravel/socialite Version ^5.0
spatie/laravel-package-tools Version ^1.9