Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gives the ability to signup with Twitter #49

Open
wants to merge 3 commits into
base: v3.0
Choose a base branch
from
Open
Show file tree
Hide file tree
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
6 changes: 5 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,8 @@ SMS_PASSWORD=dummy

# QUEUES
IRON_PROJECT=dummy
IRON_TOKEN=dummy
IRON_TOKEN=dummy

# TWITTER
TWITTER_CLIENT_ID=dummy
TWITTER_CLIENT_SECRET=dummy
8 changes: 4 additions & 4 deletions app/TeenQuotes/Api/V1/Controllers/UsersController.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
<?php namespace TeenQuotes\Api\V1\Controllers;

use App, Auth, Config, DB, Input, Queue, Str;
use App, Auth, Config, DB, Input, Session, Str;
use Buonzz\GeoIP\Laravel4\Facades\GeoIP;
use Carbon\Carbon;
use Exception;
use Laracasts\Validation\FormValidationException;
use stojg\crop\CropEntropy;
use TeenQuotes\Countries\Models\Country;
use TeenQuotes\Exceptions\ApiNotFoundException;
Expand Down Expand Up @@ -52,9 +51,10 @@ public function store($doValidation = true)
$this->userValidator->validateSignup($data);

// Store the new user
$avatar = Session::get('avatar', null);

// If the new user has got a Gravatar, set the avatar
$avatar = null;
if (Gravatar::exists($data['email']))
if (is_null($avatar) AND Gravatar::exists($data['email']))
$avatar = Gravatar::src($data['email'], 150);

$user = $this->userRepo->create($data['login'], $data['email'], $data['password'],
Expand Down
7 changes: 4 additions & 3 deletions app/TeenQuotes/Auth/AuthServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ private function registerAuthRoutes()
$this->app['router']->group($this->getRouteGroupParams(), function() use ($controller) {
$this->app['router']->get('signin', ['as' => 'signin', 'uses' => $controller.'@getSignin']);
$this->app['router']->get('logout', ['as' => 'logout', 'uses' => $controller.'@getLogout']);
$this->app['router']->get('auth/twitter', ['as' => 'auth.twitter', 'uses' => $controller.'@getAuthTwitter']);
$this->app['router']->post('signin', $controller.'@postSignin');
});
}
Expand All @@ -57,7 +58,7 @@ private function registerAuthViewComposers()
$this->app['view']->composer([
'auth.signin',
'auth.signup'
], 'TeenQuotes\Tools\Composers\DeepLinksComposer');
], 'TeenQuotes\Tools\Composers\DeepLinksComposer');
}

private function registerReminderRoutes()
Expand All @@ -78,11 +79,11 @@ private function registerReminderViewComposers()
$this->app['view']->composer([
'password.reset'
], $this->getNamespaceComposers().'ResetComposer');

// For deeps link
$this->app['view']->composer([
'password.remind'
], 'TeenQuotes\Tools\Composers\DeepLinksComposer');
], 'TeenQuotes\Tools\Composers\DeepLinksComposer');
}

/**
Expand Down
49 changes: 40 additions & 9 deletions app/TeenQuotes/Auth/Controllers/AuthController.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
<?php namespace TeenQuotes\Auth\Controllers;

use BaseController;
use Carbon;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Lang;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\View;
use Auth, BaseController, Carbon, Input, Lang, Redirect, Request, Session, View;
use Laracasts\Validation\FormValidationException;
// use Laravel\Socialite\Contracts\Factory as Socialite;
use TeenQuotes\Users\Models\User;
use TeenQuotes\Users\Validation\UserValidator;

Expand All @@ -20,12 +13,18 @@ class AuthController extends BaseController {
*/
private $userValidator;

/**
* @var Laravel\Socialite\Contracts\Factory
*/
private $socialite;

public function __construct(UserValidator $userValidator)
{
$this->beforeFilter('guest', ['only' => 'getSignin']);
$this->beforeFilter('auth', ['only' => 'getLogout']);

$this->userValidator = $userValidator;
// $this->socialite = $socialite;
}

/**
Expand Down Expand Up @@ -102,4 +101,36 @@ public function getLogout()

return Redirect::route('home')->with('success', Lang::get('auth.logoutSuccessfull', compact('login')));
}

public function getAuthTwitter()
{
$hasCode = Request::has('code');

if (! $hasCode) return $this->getTwitterAuthorization();

$user = $this->getTwitterUser();
// Prefill the signup view
Session::flash('login', $user->getNickname());
Session::flash('email', $user->getEmail());
// Store the URL of the avatar is session
// It'll be saved when calling the API
Session::set('avatar', $user->getAvatar());

return Redirect::route('signup');
}

/**
* @return \Symfony\Component\HttpFoundation\RedirectResponse
*/
private function getTwitterAuthorization()
{
return $this->socialite->driver('twitter')->redirect();
}
/**
* @return \Laravel\Socialite\Contracts\User
*/
private function getTwitterUser()
{
return $this->socialite->driver('twitter')->user();
}
}
6 changes: 6 additions & 0 deletions app/config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,10 @@
'apiKey' => getenv('EASYREC_APIKEY'),
'tenantID' => 'teenquotes_'.App::environment()
],

'twitter' => [
'client_id' => getenv('TWITTER_CLIENT_ID'),
'client_secret' => getenv('TWITTER_CLIENT_SECRET'),
'redirect' => 'http://'.Config::get('app.domain').'/auth/twitter'
],
];