Social Authentication
- Installation
- Default Supported Auth Provide
- How Social Authentication Works
- Setup Social Authentication
- Support new Auth Provide
Under the hood this container uses Laravel Socialite.
Installation
In the following instructions we assume we have a fresh Apiato installation.
composer require apiato/social-auth-container
Now run php artisan migrate
Add this values to $fillable array in your User
model
protected $fillable = [
...
'social_provider',
'social_nickname',
'social_id',
'social_token',
'social_token_secret',
'social_refresh_token',
'social_expires_in',
'social_avatar',
'social_avatar_original',
...
];
Optionally add this to your user transformer to add social auth fields to your user repsonses:
'social_auth_provider' => $user->social_provider,
'social_nickname' => $user->social_nickname,
'social_id' => $user->social_id,
'social_avatar' => [
'avatar' => $user->social_avatar,
'original' => $user->social_avatar_original,
]
It is recommended to have 2 separate transformers (private & public) for the User e.g. UserPrivateProfileTransformer
& UserTransformer
and add above data to the private transformer not the public one. By doing it this way you can hide
your User's personal data.
Default Supported Auth Provide
How Social Authentication Works
- The Client (Mobile or Web) sends a request to the Social Auth Provider (Facebook, Twitter...).
- The Social Auth Provider returns a Token.
- The Client makes a call to the server (our server) and passes the Token.
- The Server fetches the user data from the Social Auth Provider using Token.
- The Server create new User from the collected social data and return the Authenticated User (If the user already created then it just returns it).
Setup Social Authentication
- Create an App on the supported Social Auth provider.
- For Facebook: https://developers.facebook.com/apps
- For Twitter: https://apps.twitter.com/app
- For Google: https://console.developers.google.com/apis/credentials
For the callback URL you can use this Apiato web endpoint http://apiato.test/auth/{provider}/callback
(replace the
provider with any of the supported providers facebook
, twitter
,...).
- For any supported provider you want to use, set Tokens and Secrets in the
.env
AUTH_FACEBOOK_CLIENT_ID=
AUTH_FACEBOOK_CLIENT_SECRET=
AUTH_FACEBOOK_CLIENT_REDIRECT=
AUTH_TWITTER_CLIENT_ID=
AUTH_TWITTER_CLIENT_SECRET=
AUTH_TWITTER_CLIENT_REDIRECT=
AUTH_GOOGLE_CLIENT_ID=
AUTH_GOOGLE_CLIENT_SECRET=
AUTH_GOOGLE_CLIENT_REDIRECT=
- Make a request from your client to get the
oauth
info.
Each Social provider returns different response and keys
Example Twitter Response:
{
"User": {
"tokentoken": "1212",
"tokentokenSecret": "3434",
"tokenid": "777",
"tokennickname": "John_Doe",
"tokenname": "John Doe",
"tokenemail": null,
"tokenavatar": "http://pbs.twimg.com/images/888/PENrcePC.jpg",
"tokenuser": "token",
"avatar_original": "http://pbs.twimg.com/images/999/PENrcePC.jpg"
}
}
This step should be done by your client App, which could be a Web, Mobile or other kind of client Apps.
-
Use
auth/{provider}
route and theoauth
info to make a call from your server to the Social Provider in order to get the User info. For more details about theauth/{provider}
route parameters checkout the generated documentation or visitapp/Containers/Vendor/Socialauth/UI/API/Routes/AuthenticateAll.v1.private.php
. -
The endpoint above should return the User and his Personal Access Token.
Example Google Response:
{
"data": {
// user data + additional social data if you have updated your transformer as mentioned above
.
.
.
"social_auth_provider": "google",
"social_id": "113834952367767922133",
"social_avatar": {
"avatar": "https:\/\/lh6.googleusercontent.com\/-OSItz6IHbSw\/AAA\/AMZuucltEs\/s96-c\/photo.jpg",
"original": "https:\/\/lh6.googleusercontent.com\/-OSItz6IHbSw\/AAA\/AMZuucltEs\/s96-c\/photo.jpg"
}
},
"meta": {
"include": [
"roles"
],
"custom": {
"token_type": "personal",
"access_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9...."
}
}
}
Support new Auth Provider
This container works out of the box perfectly but if you want to change its configs or modify the codes you MUST follow these steps:
1- Copy the container from Vendor
to AppSection
(or any of your custom sections) of your project
2- Fix the namespaces
3- Remove apiato/social-auth-container
dependency from project root composer.json
- Pick an Auth Provider from the supported providers by Socialite.
- Go to
app/Containers/YourSection/Socialauth/Tasks/FindUserSocialProfileTask.php
and support your provider.