this tutorial is explain how you can create dynamic login and sign up
Laravel 4 frameworks and Sentry 2.0 agnostic authentication & authorization system!
Add this lines to your composer.json file:
"require": { "cartalyst/sentry": "2.0.*", "artdarek/oauth-4-laravel": "dev-master" },
Registering the Package:
Register the service provider within the providers
array found in app/config/app.php
:
'providers' => array( 'Cartalyst\Sentry\SentryServiceProvider', 'Artdarek\OAuth\OAuthServiceProvider', )
Add an alias within the aliases
array found in app/config/app.php
:
'aliases' => array( 'Sentry' => 'Cartalyst\Sentry\Facades\Laravel\Sentry', 'OAuth' => 'Artdarek\OAuth\Facade\OAuth', )
Create configuration file for package using artisan command:
php artisan config:publish artdarek/oauth-4-laravel
link your application and register your oauth (google, github, linkedin) :
- https://console.developers.google.com/
- https://github.com/settings/applications/
- https://www.linkedin.com/secure/developer
Configure your consumers:
'consumers' => array( /** * Google */ 'Google' => array( 'client_id' => '629574582444-88t193ull6utjkv15m*************', 'client_secret' => 'aF3WWO************', 'scope' => array('userinfo_email', 'userinfo_profile'), ), /** * Github */ 'GitHub' => array( 'client_id' => 'd5864d1c**********', 'client_secret' => '630485d3741bd8************' ), /** * Linkedin */ 'Linkedin' => array( 'client_id' => '77cd********', 'client_secret' => 'IDnlp************', 'scope' => array('r_emailaddress', 'r_basicprofile'), ), )
Google function:
public function loginWithGoogle() { // get data from input $code = Input::get( 'code' ); // get google service $googleService = OAuth::consumer( 'Google' ); // check if code is valid // if code is provided get user data and sign in if ( !empty( $code ) ) { // This was a callback request from google, get the token $token = $googleService->requestAccessToken( $code ); // Send a request with it $result = json_decode( $googleService->request( 'https://www.googleapis.com/oauth2/v1/userinfo' ), true ); if(!empty($token)){ try{ // Find the user using the user id $user = Sentry::findUserByLogin($result['email']); // Log the user in Sentry::login($user, false); return Redirect::route('home'); } catch (Cartalyst\Sentry\Users\UserNotFoundException $e) { // Register the user $user = Sentry::register(array( 'activated' => 1, 'email' => $result['email'], 'password' => Hash::make(uniqid(time())), 'first_name' => $result['name'], 'avatar' => $result['picture'], 'country' => (!empty($result['location'])) ? $result['location'] : false )); $usergroup = Sentry::getGroupProvider()->findById(2); $user->addGroup($usergroup); Sentry::login($user, false); return Redirect::route('account'); } } } // if not ask for permission first else { // get googleService authorization $url = $googleService->getAuthorizationUri(); // return to facebook login url return Redirect::to( (string)$url ); } }
default json array you will get it after authentication
["id"]=> string(21) "100647680310191018719" ["email"]=> string(23) "[email protected]" ["verified_email"]=> bool(true) ["name"]=> string(22) "hany alsamman (codexc)" ["given_name"]=> string(4)"hany" ["family_name"]=> string(8) "alsamman" ["link"]=> string(37) "https://plus.google.com/+hanyalsamman" ["picture"]=> string(92) "https://lh5.googleusercontent.com/-9DMuVgr7sdk/AAAAAAAAAAI/AAAAAAAAAM8/iEUTGqqEqo0/photo.jpg" ["gender"]=> string(4) "male" ["locale"]=> string(2) "en" }
Github function:
public function loginWithGithub() { // get data from input $code = Input::get( 'code' ); $GitHubService = OAuth::consumer('GitHub'); if ( !empty( $code ) ) { // This was a callback request from linkedin, get the token $token = $GitHubService->requestAccessToken( $code ); // Send a request with it. Please note that XML is the default format. $result = json_decode($GitHubService->request('user'), true); if(!empty($token)){ try{ // Find the user using the user id $user = Sentry::findUserByLogin($result['email']); // Log the user in Sentry::login($user, false); return Redirect::route('home'); } catch (Cartalyst\Sentry\Users\UserNotFoundException $e) { // Register the user $user = Sentry::register(array( 'activated' => 1, 'email' => $result['email'], 'password' => Hash::make(uniqid(time())), 'first_name' => $result['name'], 'avatar' => $result['avatar_url'], 'country' => (!empty($result['location'])) ? $result['location'] : false )); $usergroup = Sentry::getGroupProvider()->findById(2); $user->addGroup($usergroup); Sentry::login($user, false); return Redirect::route('account'); } } }// if not ask for permission first else { // get linkedinService authorization $url = $GitHubService->getAuthorizationUri(); // return to linkedin login url return Redirect::to( (string)$url ); } }
default json array you will get it after authentication
["login"]=> string(10) "codex-corp" ["id"]=>int(775654) ["avatar_url"]=>string(47) "https://avatars.githubusercontent.com/u/775654?" ["gravatar_id"]=>string(32) "ecd8cdc1a545bad2c8b67ce46d4ef2a9" ["url"]=>string(39) "https://api.github.com/users/codex-corp" ["html_url"]=>string(29) "https://github.com/codex-corp"string(4) "User" ["site_admin"]=> bool(false) ["name"]=>string(13) "Hany alsamman" ["company"]=>string(12) "Code Experts" ["blog"]=>string(29) "http://osrc.dfm.io/codex-corp" ["location"]=>string(0) "" ["email"]=>string(23) "[email protected]" ["created_at"]=>string(20) "2011-05-08T21:06:52Z" ["updated_at"]=>string(20) "2014-05-19T22:41:59Z"
LinkedIn function :
public function loginWithLinkedin() { // get data from input $code = Input::get( 'code' ); $linkedinService = OAuth::consumer('Linkedin'); if ( !empty( $code ) ) { // This was a callback request from linkedin, get the token $token = $linkedinService->requestAccessToken( $code ); // Send a request with it. Please note that XML is the default format. $result = json_decode($linkedinService->request('/people/~:(id,first-name,last-name,headline,member-url-resources,picture-urls::(original),location,public-profile-url,email-address)?format=json'), true); if(!empty($token)){ try{ // Find the user using the user id $user = Sentry::findUserByLogin($result['emailAddress']); //TODO: run logout function // Log the user in Sentry::login($user, false); return Redirect::route('home'); } catch (Cartalyst\Sentry\Users\UserNotFoundException $e) { // Register the user $user = Sentry::register(array( 'activated' => 1, 'email' => $result['emailAddress'], 'password' => Hash::make(uniqid(time())), 'first_name' => $result['firstName'], 'last_name' => $result['lastName'], 'avatar' => $result['pictureUrls']['values'][0], 'country' => $result['location']['name'] )); $usergroup = Sentry::getGroupProvider()->findById(2); $user->addGroup($usergroup); Sentry::login($user, false); return Redirect::route('account'); } } }// if not ask for permission first else { // get linkedinService authorization $url = $linkedinService->getAuthorizationUri(array('state'=>'DCEEFWF45453sdffef424')); // return to linkedin login url return Redirect::to( (string)$url ); } }
default json array you will get it after authentication
["emailAddress"]=> string(23) "[email protected]" ["firstName"]=> string(4) "Hany" ["headline"]=> string(108) "Web programmer founder of codexc.com , Android Development founder of AOSB Project , founder of ncryptd.com" ["id"]=> string(10) "FFerT0j8mm" ["lastName"]=> string(8) "Alsamman" ["location"]=> array(2) { ["country"]=> array(1) { ["code"]=> string(2) "sy" } ["name"]=> string(5) "Turkey" } ["pictureUrls"]=> array(2) { ["_total"]=> int(1) ["values"]=> array(1) { [0]=> string(96) ["publicProfileUrl"]=> string(39)
Set your routes:
Route::get('linkedin', array('as' => 'linkedin', 'uses' => 'HomeController@loginWithLinkedin')); Route::get('github', array('as' => 'github', 'uses' => 'HomeController@loginWithGithub')); Route::get('google', array('as' => 'google', 'uses' => 'HomeController@loginWithGoogle'));
Create views :
you may use bootstrap social buttons
<div class="column-container"> <!-- One Third --> <div class="column-one-third"> <a style="color: #ffffff" href="{{{ URL::route('google') }}}"><button class="btn btn-large btn-google-plus"> <i class="fa fa-google-plus fa fa-lg"></i> | Connect with Google</button></a> </div> <!-- One Third --> <div class="column-one-third"> <a style="color: #000" href="{{{ URL::route('github') }}}"><button class="btn btn-large btn-github"> <i style="color: #000" class="fa fa-github fa fa-lg"></i> | Connect with Github</button></a> </div> <!-- One Third --> <div class="column-one-third"> <a style="color: #ffffff" href="{{{ URL::route('linkedin') }}}"><button class="btn btn-large btn-linkedin"> <i class="fa fa-linkedin fa fa-lg"></i> | Connect with LinkedIn</button></a> </div> </div>
Do not forget to change user group ID at this line
$usergroup = Sentry::getGroupProvider()->findById(2);
then add this two fields into your users table (avatar , country)
Regards !
That is good example but what if I have fb login and google login on the same time?
This will create 2 users?
Hi,
No problems or duplicated account in DB , if the user using same email with FB and Google
Hany, thanks for this tutorial. It is very helpful. I am having one problem – when a user email does not exist in the db, the error “A user could not be found with a login value” is thrown and the script does not attempt to register the new user. How do I get the script to trigger the registration attempt in the catch?
i don’t think there a social network has any user without email address ?, can you clarify where the problem is (FB , G+ …)
I developed a work around, but the problem was that if a user wasn’t in my db than sentry was throwing the user not found exception. To solve it: After the if (!empty($token) I added: $existing = User::where(’email’, ‘=’, ($result[’email’]))->get()->first();
if(!is_null($existing)){…continue code}
$existing = Sentry::findUserByLogin($result[’email’]);
Should be
that depends on the scope depth , so it should be like this in your case
if i want to register account,by checking if email exist. how can i do that?
if i want to register account,by checking if email exist. how can i do that? register it to 3 api’s
Which data I have to include in the scope?
I introduced the email displayed in the Client ID for web application (google – doesn’t work), my user email (doesn’t work).
OAuth OAuth2
Service Exception
InvalidScopeException
Scope Google+ API is not valid for service OAuthOAuth2ServiceGoogle
Hi sofia ,
did you set the scope in consumers config file like this, ‘scope’ => array(‘userinfo_email’, ‘userinfo_profile’)
the public profile info is returned with the email scope.
HI, i have tried your code for login with google, but i always get Error in retrieving token: “invalid_grant”. I’ve also tried with email (…@developer.gserviceaccount.com) instead client_id, but same problem. Can you help please?
Mobitsolutions is one of the best Joomla development company with high profile Joomla developers, they can design high quality Joomla websites