1 頁 (共 1 頁)

Social authentication in laravel 5

發表於 : 2015-12-06 22:05:52
yehlu
http://tutsnare.com/social-authentication-in-laravel-5/

Social authentication in laravel 5 using laravel socialite

A new package added laravel socialite to provide social authentication in laravel 5. Before laravel 5 their is only form based authentication now using Laravel Socialite it’s provides a simple, convenient way to authenticate with OAuth providers. laravel socialite currently supports 5 OAuth providers authentication in laravel 5, which are Facebook, Twitter, Google, GitHub and Bitbucket. So you can work with both type of user login process form based authentication and social authentication in laravel 5.

How to setup social authentication in laravel 5 :-

To setup socialite we need to add some packages and Facades below i am sharing steps to setup laravel socialite.

Social authentication in laravel 5

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.

Social authentication in laravel 5 with Github :-

Now we will explore step by step an example of github login integration in laravel 5.

1. Create OAuth App :-
A. To create an application signin in your github account.
B. Click on “settings” icon then click on “Applications” link
C. Then Click on “Register New Application” button at top right corner.
D. Now Add Application name, Homepage URL, Authorization callback URL and click on submit this will generate your Client ID and Client Secret.

2. Setup OAuth 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' => 'f285091e4d346f1ac05459',
'client_secret' => '866e847c2343fb77dc21a68f5cbc234fea41d7549e8c62',
'redirect' => 'http://localhost/mylara5/account/github',
],
3. Routes :- Now you need to create routes so add routes in app/Http/routes.php.


Route::get('github', 'AccountController@github_redirect');
Route::get('account/github', 'AccountController@github');
4. Controllers :- Now you need to add authentication and redirection code with facades to get authenticate with github


<?php namespace App\Http\Controllers;
use Redirect;
use Socialize;
class AccountController extends Controller {
// To redirect github
public function github_redirect() {
return Socialize::with('github')->redirect();
}
// to get authenticate user data
public function github() {
$user = Socialize::with('github')->user();
// Do your stuff with user data.
print_r($user);die;
}
}
5. View :- Add a link or button to your view where you want to add github authentication link like add below link to resources/views/login.blade.php.


<a href="{!!URL::to('github')!!}">Login with Github</a>
Now click on login link it will redirect you to github. then after login redirect back to your redirect url and you will get user data, now add your stuff to user login process. So now you can easily add other providers like facebook, twitter to add social authentication in laravel 5.