From 1347a768359796c755d3f6014e186d6219d0cc7a Mon Sep 17 00:00:00 2001 From: Kenyon Date: Fri, 4 Apr 2014 13:27:29 -0700 Subject: [PATCH 1/2] Update README.md Wrote an example that is an actual controller, using proper Laravel4 techniques --- README.md | 80 +++++++++++++++++++++++++++++++++---------------------- 1 file changed, 48 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index b34e761..e7e159c 100644 --- a/README.md +++ b/README.md @@ -33,45 +33,61 @@ After that, run composer install to install Laravel OAuth 2.0. http://example.com/auth/session/facebook ```php - + 'your-client-id', - 'secret' => 'your-client-secret', - )); - - if ( ! isset($_GET['code'])) - { - // By sending no options it'll come back here - return $provider->authorize(); - } - else - { - // Howzit? - try - { - $params = $provider->access($_GET['code']); - - $token = new Token_Access(array( - 'access_token' => $params->access_token - )); - $user = $provider->get_user_info($token); +/** + * + * Controller containing user functions & actions + * + * =========================================================== + * Must be using PHP>=5.4 because using [] instead of array() + * =========================================================== + * + */ +class UserController extends BaseController { + + /** + * oAuth2 login + * + * @param string $provider + * @return response + */ + public function oauth2Login($provider) { + switch ($provider) { + case 'facebook': + $credentials = [ + 'id' => 'client-id', + 'secret' => 'client-secret' + ]; + break; + default: + // Google + $credentials = [ + 'id' => 'client-id', + 'secret' => 'client-secret' + ]; + } + + $provider = OAuth2::provider($provider, $credentials); + + if (!Input::has('code')) { + // Authorize the user - redirect back here with a code to retrieve the users information + return $provider->authorize(); + } else { + // Get the user JSON array from the provider + $user = $provider->get_user_info((new Token_Access([ + 'access_token' => $provider->access(Input::get('code'))->access_token + ]))); // Here you should use this information to A) look for a user B) help a new user sign up with existing data. - // If you store it all in a cookie and redirect to a registration page this is crazy-simple. - echo "
";
-			var_dump($user);
-		}
-		
-		catch (OAuth2_Exception $e)
-		{
-			show_error('That didnt work: '.$e);
+            // All you have to do from here is User::create([]) with the required informaion - assuming you're using laravel defaults.
+            //dd('
' . var_dump($user) . '
'); + Redirect::to(action('SomeController@afterLogin')); //Where ever you want to go after you registered &/or logged them in } } } + ``` From 9d67a590b1d91307b61f598e99f7e52622dac336 Mon Sep 17 00:00:00 2001 From: Kenyon Date: Fri, 4 Apr 2014 13:47:47 -0700 Subject: [PATCH 2/2] Update README.md Added example that implements the database. --- README.md | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index e7e159c..dab44ba 100644 --- a/README.md +++ b/README.md @@ -55,7 +55,7 @@ class UserController extends BaseController { * @param string $provider * @return response */ - public function oauth2Login($provider) { +public function oauth2Login($provider) { switch ($provider) { case 'facebook': $credentials = [ @@ -83,9 +83,25 @@ class UserController extends BaseController { ]))); // Here you should use this information to A) look for a user B) help a new user sign up with existing data. - // All you have to do from here is User::create([]) with the required informaion - assuming you're using laravel defaults. - //dd('
' . var_dump($user) . '
'); - Redirect::to(action('SomeController@afterLogin')); //Where ever you want to go after you registered &/or logged them in + //dd('
' . var_dump($user) . '
'); + + $name = explode(' ', $user['name']); + $email = $user['email']; + $user = User::whereRaw('first_name = ? and last_name = ? and email = ?', [$name[0], end($name), $email]); + + if (0 == $user->count()) { + $user = User::create([ + 'first_name' => $name[0], + 'last_name' => end($name), + 'email' => $email + ]); + + Auth::login($user); + } else { + Auth::login($user->first()); + } + + return Redirect::to(action('GeneralController@showHome')); } } }