Skip to content

Commit

Permalink
Added social sign in
Browse files Browse the repository at this point in the history
  • Loading branch information
ssddanbrown committed Sep 4, 2015
1 parent 48814b8 commit 2dcc510
Show file tree
Hide file tree
Showing 20 changed files with 578 additions and 32 deletions.
8 changes: 6 additions & 2 deletions app/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ class Handler extends ExceptionHandler
*
* This is a great spot to send exceptions to Sentry, Bugsnag, etc.
*
* @param \Exception $e
* @return void
* @param \Exception $e
*/
public function report(Exception $e)
{
Expand All @@ -39,6 +38,11 @@ public function report(Exception $e)
*/
public function render($request, Exception $e)
{
if($e instanceof NotifyException) {
\Session::flash('error', $e->message);
return response()->redirectTo($e->redirectLocation);
}

return parent::render($request, $e);
}
}
21 changes: 21 additions & 0 deletions app/Exceptions/NotifyException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php namespace Oxbow\Exceptions;


class NotifyException extends \Exception
{

public $message;
public $redirectLocation;

/**
* NotifyException constructor.
* @param string $message
* @param string $redirectLocation
*/
public function __construct($message, $redirectLocation)
{
$this->message = $message;
$this->redirectLocation = $redirectLocation;
parent::__construct();
}
}
6 changes: 6 additions & 0 deletions app/Exceptions/SocialDriverNotConfigured.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php namespace Oxbow\Exceptions;


class SocialDriverNotConfigured extends \Exception
{
}
7 changes: 7 additions & 0 deletions app/Exceptions/UserNotFound.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php namespace Oxbow\Exceptions;


class UserNotFound extends NotifyException
{

}
123 changes: 114 additions & 9 deletions app/Http/Controllers/Auth/AuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

namespace Oxbow\Http\Controllers\Auth;

use Oxbow\Exceptions\SocialDriverNotConfigured;
use Oxbow\Exceptions\UserNotFound;
use Oxbow\Repos\UserRepo;
use Oxbow\User;
use Validator;
use Oxbow\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
use Laravel\Socialite\Contracts\Factory as Socialite;

class AuthController extends Controller
{
Expand All @@ -27,44 +31,145 @@ class AuthController extends Controller
protected $redirectPath = '/';
protected $redirectAfterLogout = '/login';

protected $validSocialDrivers = ['google', 'github'];

protected $socialite;
protected $userRepo;

/**
* Create a new authentication controller instance.
*
* @return void
* @param Socialite $socialite
* @param UserRepo $userRepo
*/
public function __construct()
public function __construct(Socialite $socialite, UserRepo $userRepo)
{
$this->middleware('guest', ['except' => 'getLogout']);
$this->socialite = $socialite;
$this->userRepo = $userRepo;
}

/**
* Get a validator for an incoming registration request.
*
* @param array $data
* @param array $data
* @return \Illuminate\Contracts\Validation\Validator
*/
protected function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|confirmed|min:6',
]);
}

/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @param array $data
* @return User
*/
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
}

/**
* Show the application login form.
*
* @return \Illuminate\Http\Response
*/
public function getLogin()
{

if (view()->exists('auth.authenticate')) {
return view('auth.authenticate');
}

$socialDrivers = $this->getActiveSocialDrivers();

return view('auth.login', ['socialDrivers' => $socialDrivers]);
}

/**
* Redirect to the relevant social site.
* @param $socialDriver
* @return \Symfony\Component\HttpFoundation\RedirectResponse
*/
public function getSocialLogin($socialDriver)
{
$driver = $this->validateSocialDriver($socialDriver);
return $this->socialite->driver($driver)->redirect();
}

/**
* The callback for social login services.
*
* @param $socialDriver
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
* @throws UserNotFound
*/
public function socialCallback($socialDriver)
{
$driver = $this->validateSocialDriver($socialDriver);
// Get user details from social driver
$socialUser = $this->socialite->driver($driver)->user();
$user = $this->userRepo->getByEmail($socialUser->getEmail());

// Redirect if the email is not a current user.
if ($user === null) {
throw new UserNotFound('A user with the email ' . $socialUser->getEmail() . ' was not found.', '/login');
}

\Auth::login($user, true);
return redirect($this->redirectPath);
}

/**
* Ensure the social driver is correct and supported.
*
* @param $socialDriver
* @return string
* @throws SocialDriverNotConfigured
*/
protected function validateSocialDriver($socialDriver)
{
$driver = trim(strtolower($socialDriver));

if (!in_array($driver, $this->validSocialDrivers)) abort(404, 'Social Driver Not Found');
if(!$this->checkSocialDriverConfigured($driver)) throw new SocialDriverNotConfigured;

return $driver;
}

/**
* Check a social driver has been configured correctly.
* @param $driver
* @return bool
*/
protected function checkSocialDriverConfigured($driver)
{
$upperName = strtoupper($driver);
$config = [env($upperName . '_APP_ID', false), env($upperName . '_APP_SECRET', false), env('APP_URL', false)];
return (!in_array(false, $config) && !in_array(null, $config));
}

/**
* Gets the names of the active social drivers.
* @return array
*/
protected function getActiveSocialDrivers()
{
$activeDrivers = [];
foreach($this->validSocialDrivers as $driverName) {
if($this->checkSocialDriverConfigured($driverName)) {
$activeDrivers[$driverName] = true;
}
}
return $activeDrivers;
}
}
4 changes: 4 additions & 0 deletions app/Http/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@
Route::get('/login', 'Auth\AuthController@getLogin');
Route::post('/login', 'Auth\AuthController@postLogin');
Route::get('/logout', 'Auth\AuthController@getLogout');
// Login using social authentication
Route::get('/login/service/{socialService}', 'Auth\AuthController@getSocialLogin');
Route::get('/login/service/{socialService}/callback', 'Auth\AuthController@socialCallback');

// Password reset link request routes...
Route::get('/password/email', 'Auth\PasswordController@getEmail');
Route::post('/password/email', 'Auth\PasswordController@postEmail');
Expand Down
24 changes: 24 additions & 0 deletions app/Repos/UserRepo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php namespace Oxbow\Repos;


use Oxbow\User;

class UserRepo
{

protected $user;

/**
* UserRepo constructor.
* @param $user
*/
public function __construct(User $user)
{
$this->user = $user;
}


public function getByEmail($email) {
return $this->user->where('email', '=', $email)->first();
}
}
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"php": ">=5.5.9",
"laravel/framework": "5.1.*",
"intervention/image": "^2.3",
"barryvdh/laravel-ide-helper": "^2.1"
"barryvdh/laravel-ide-helper": "^2.1",
"laravel/socialite": "^2.0"
},
"require-dev": {
"fzaninotto/faker": "~1.4",
Expand Down
Loading

0 comments on commit 2dcc510

Please sign in to comment.