This repository was archived by the owner on Oct 6, 2020. It is now read-only.
forked from philsturgeon/codeigniter-oauth2
-
Notifications
You must be signed in to change notification settings - Fork 34
Update README.md #12
Open
Ryuske
wants to merge
2
commits into
madewithlove:master
Choose a base branch
from
Ryuske:patch-1
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Update README.md #12
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,45 +33,77 @@ After that, run composer install to install Laravel OAuth 2.0. | |
| http://example.com/auth/session/facebook | ||
|
|
||
| ```php | ||
|
|
||
| <?php | ||
| use OAuth2\OAuth2; | ||
| use OAuth2\Token_Access; | ||
| use OAuth2\Exception as OAuth2_Exception; | ||
|
|
||
| public function action_session($provider) | ||
| { | ||
| $provider = OAuth2::provider($provider, array( | ||
| 'id' => '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: | ||
| $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 "<pre>"; | ||
| var_dump($user); | ||
| } | ||
|
|
||
| catch (OAuth2_Exception $e) | ||
| { | ||
| show_error('That didnt work: '.$e); | ||
| //dd('<pre>' . var_dump($user) . '</pre>'); | ||
|
|
||
| $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()) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This whole condition and the line before should probably just be : $user = User::firstOrCreate(array(
'first_name' => $name[0],
'last_name' => end($name),
'email' => $email,
));
Auth::login($user); |
||
| $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')); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| ``` | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks kind of weird no ? Why not use the query builder ?