diff --git a/README.md b/README.md index b34e761..dab44ba 100644 --- a/README.md +++ b/README.md @@ -33,45 +33,77 @@ 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);
+ //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'));
}
}
}
+
```