Skip to content
This repository was archived by the owner on Oct 6, 2020. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 64 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
// 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 "<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]);
Copy link
Member

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 ?


if (0 == $user->count()) {
Copy link
Member

Choose a reason for hiding this comment

The 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'));
}
}
}

```