3 namespace Oxbow\Http\Controllers\Auth;
5 use Oxbow\Exceptions\SocialDriverNotConfigured;
6 use Oxbow\Exceptions\UserNotFound;
7 use Oxbow\Repos\UserRepo;
10 use Oxbow\Http\Controllers\Controller;
11 use Illuminate\Foundation\Auth\ThrottlesLogins;
12 use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
13 use Laravel\Socialite\Contracts\Factory as Socialite;
15 class AuthController extends Controller
18 |--------------------------------------------------------------------------
19 | Registration & Login Controller
20 |--------------------------------------------------------------------------
22 | This controller handles the registration of new users, as well as the
23 | authentication of existing users. By default, this controller uses
24 | a simple trait to add these behaviors. Why don't you explore it?
28 use AuthenticatesAndRegistersUsers, ThrottlesLogins;
30 protected $loginPath = '/login';
31 protected $redirectPath = '/';
32 protected $redirectAfterLogout = '/login';
34 protected $validSocialDrivers = ['google', 'github'];
40 * Create a new authentication controller instance.
41 * @param Socialite $socialite
42 * @param UserRepo $userRepo
44 public function __construct(Socialite $socialite, UserRepo $userRepo)
46 $this->middleware('guest', ['except' => 'getLogout']);
47 $this->socialite = $socialite;
48 $this->userRepo = $userRepo;
52 * Get a validator for an incoming registration request.
55 * @return \Illuminate\Contracts\Validation\Validator
57 protected function validator(array $data)
59 return Validator::make($data, [
60 'name' => 'required|max:255',
61 'email' => 'required|email|max:255|unique:users',
62 'password' => 'required|confirmed|min:6',
67 * Create a new user instance after a valid registration.
72 protected function create(array $data)
75 'name' => $data['name'],
76 'email' => $data['email'],
77 'password' => bcrypt($data['password']),
82 * Show the application login form.
84 * @return \Illuminate\Http\Response
86 public function getLogin()
89 if (view()->exists('auth.authenticate')) {
90 return view('auth.authenticate');
93 $socialDrivers = $this->getActiveSocialDrivers();
95 return view('auth.login', ['socialDrivers' => $socialDrivers]);
99 * Redirect to the relevant social site.
100 * @param $socialDriver
101 * @return \Symfony\Component\HttpFoundation\RedirectResponse
103 public function getSocialLogin($socialDriver)
105 $driver = $this->validateSocialDriver($socialDriver);
106 return $this->socialite->driver($driver)->redirect();
110 * The callback for social login services.
112 * @param $socialDriver
113 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
114 * @throws UserNotFound
116 public function socialCallback($socialDriver)
118 $driver = $this->validateSocialDriver($socialDriver);
119 // Get user details from social driver
120 $socialUser = $this->socialite->driver($driver)->user();
121 $user = $this->userRepo->getByEmail($socialUser->getEmail());
123 // Redirect if the email is not a current user.
124 if ($user === null) {
125 throw new UserNotFound('A user with the email ' . $socialUser->getEmail() . ' was not found.', '/login');
128 \Auth::login($user, true);
129 return redirect($this->redirectPath);
133 * Ensure the social driver is correct and supported.
135 * @param $socialDriver
137 * @throws SocialDriverNotConfigured
139 protected function validateSocialDriver($socialDriver)
141 $driver = trim(strtolower($socialDriver));
143 if (!in_array($driver, $this->validSocialDrivers)) abort(404, 'Social Driver Not Found');
144 if(!$this->checkSocialDriverConfigured($driver)) throw new SocialDriverNotConfigured;
150 * Check a social driver has been configured correctly.
154 protected function checkSocialDriverConfigured($driver)
156 $upperName = strtoupper($driver);
157 $config = [env($upperName . '_APP_ID', false), env($upperName . '_APP_SECRET', false), env('APP_URL', false)];
158 return (!in_array(false, $config) && !in_array(null, $config));
162 * Gets the names of the active social drivers.
165 protected function getActiveSocialDrivers()
168 foreach($this->validSocialDrivers as $driverName) {
169 if($this->checkSocialDriverConfigured($driverName)) {
170 $activeDrivers[$driverName] = true;
173 return $activeDrivers;