Social site authentication (sign up and sign in ) in Laravel 5:
In this blog we are authenticate the users by twitter, facebook, google etc.
We can login and registration via using these account in our Laravel 5 app.
1) Include package :- First we need to include socialite package to get started with Socialite, for include the package go to your composer.json file and below code to require section :-
"laravel/socialite": "~2.0"
Example :- so now your file code of require section looks like:-
"require": {
"laravel/framework": "5.0.*",
"laravel/socialite": "~2.0"
},
2) Register provider and facades :- to register provider you need to add this Laravel Framework Service Providers list. for this go to config/app.php and add below code to “providers” array to further use:
'Laravel\Socialite\SocialiteServiceProvider',
Example :- so now your file code of provider array section looks like:-
'providers' => [
/* more already here */
'Laravel\Socialite\SocialiteServiceProvider',
Facades :- To register facades go to config/app.php and add below code to “aliases” array :
'Socialize' => 'Laravel\Socialite\Facades\Socialite',
Example :- so now your file code of aliases array section looks like:-
'aliases' => [
/* more already here */
'Laravel\Socialite\SocialiteServiceProvider',
3) Get package from remote :- Now after add packages we need to install Socialite package in our application for this you need to run composer update command. for this pick up your laravel application directory path in CLI or terminal and run.
composer update
After install Socialite packages successfully we need to setup oauth providers credentials, routes and controllers
4) Setup OAuth providers credentials :- Now we need to create OAuth applications on providers like facebook, google, twitter, github etc. to get client id and client secret which need to authenticate with providers.
After getting client id and secret we need to setup credentials. To setup OAuth providers credentials go to config/services.php and add your provider config to existing “return” array like below
'github' => [
'client_id' => 'your-github-app-id',
'client_secret' => 'your-github-app-secret',
'redirect' => 'http://your-callback-url',
],
Note:- for all provider add configuration(client id, client secret and redirect) for others like ‘facebook’, ‘google’, ‘twitter’.
5) Routes :- Now You will need two routes: one for redirecting the user to the OAuth provider, and another for receiving the callback from the provider after authentication. so add routes in app/Http/routes.php like :-
// Redirect to github to authenticate
Route::get('github', 'AccountController@github_redirect');
// Get back to redirect url
Route::get('account/github', 'AccountController@github');
6) Controller :- so according to routes you need to set your controller function and make social authentication with use of facades like
public function redirectToProvider() {
return Socialize::with('github')->redirect();
}
public function handleProviderCallback() {
$user = Socialize::with('github')->user();
}
7) Retrieve user details :- After get success authenticate you can get all user details with $user object like
$user->getId();
$user->getNickname();
$user->getName();
$user->getEmail();
$user->getAvatar();
Now you have successfully setup-ed social authentication in laravel 5 using laravel socialite. Below we see an example of github social authentication.
Thanks
In this blog we are authenticate the users by twitter, facebook, google etc.
We can login and registration via using these account in our Laravel 5 app.
1) Include package :- First we need to include socialite package to get started with Socialite, for include the package go to your composer.json file and below code to require section :-
"laravel/socialite": "~2.0"
Example :- so now your file code of require section looks like:-
"require": {
"laravel/framework": "5.0.*",
"laravel/socialite": "~2.0"
},
2) Register provider and facades :- to register provider you need to add this Laravel Framework Service Providers list. for this go to config/app.php and add below code to “providers” array to further use:
'Laravel\Socialite\SocialiteServiceProvider',
Example :- so now your file code of provider array section looks like:-
'providers' => [
/* more already here */
'Laravel\Socialite\SocialiteServiceProvider',
Facades :- To register facades go to config/app.php and add below code to “aliases” array :
'Socialize' => 'Laravel\Socialite\Facades\Socialite',
Example :- so now your file code of aliases array section looks like:-
'aliases' => [
/* more already here */
'Laravel\Socialite\SocialiteServiceProvider',
3) Get package from remote :- Now after add packages we need to install Socialite package in our application for this you need to run composer update command. for this pick up your laravel application directory path in CLI or terminal and run.
composer update
After install Socialite packages successfully we need to setup oauth providers credentials, routes and controllers
4) Setup OAuth providers credentials :- Now we need to create OAuth applications on providers like facebook, google, twitter, github etc. to get client id and client secret which need to authenticate with providers.
After getting client id and secret we need to setup credentials. To setup OAuth providers credentials go to config/services.php and add your provider config to existing “return” array like below
'github' => [
'client_id' => 'your-github-app-id',
'client_secret' => 'your-github-app-secret',
'redirect' => 'http://your-callback-url',
],
Note:- for all provider add configuration(client id, client secret and redirect) for others like ‘facebook’, ‘google’, ‘twitter’.
5) Routes :- Now You will need two routes: one for redirecting the user to the OAuth provider, and another for receiving the callback from the provider after authentication. so add routes in app/Http/routes.php like :-
// Redirect to github to authenticate
Route::get('github', 'AccountController@github_redirect');
// Get back to redirect url
Route::get('account/github', 'AccountController@github');
6) Controller :- so according to routes you need to set your controller function and make social authentication with use of facades like
public function redirectToProvider() {
return Socialize::with('github')->redirect();
}
public function handleProviderCallback() {
$user = Socialize::with('github')->user();
}
7) Retrieve user details :- After get success authenticate you can get all user details with $user object like
$user->getId();
$user->getNickname();
$user->getName();
$user->getEmail();
$user->getAvatar();
Now you have successfully setup-ed social authentication in laravel 5 using laravel socialite. Below we see an example of github social authentication.
Thanks
No comments:
Post a Comment