3 namespace Oxbow\Http\Controllers\Auth;
5 use Illuminate\Http\Request;
6 use Oxbow\Exceptions\SocialSignInException;
7 use Oxbow\Exceptions\UserRegistrationException;
8 use Oxbow\Repos\UserRepo;
9 use Oxbow\Services\EmailConfirmationService;
10 use Oxbow\Services\Facades\Setting;
11 use Oxbow\Services\SocialAuthService;
14 use Oxbow\Http\Controllers\Controller;
15 use Illuminate\Foundation\Auth\ThrottlesLogins;
16 use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
18 class AuthController extends Controller
21 |--------------------------------------------------------------------------
22 | Registration & Login Controller
23 |--------------------------------------------------------------------------
25 | This controller handles the registration of new users, as well as the
26 | authentication of existing users. By default, this controller uses
27 | a simple trait to add these behaviors. Why don't you explore it?
31 use AuthenticatesAndRegistersUsers, ThrottlesLogins;
33 protected $loginPath = '/login';
34 protected $redirectPath = '/';
35 protected $redirectAfterLogout = '/login';
37 protected $socialAuthService;
38 protected $emailConfirmationService;
42 * Create a new authentication controller instance.
43 * @param SocialAuthService $socialAuthService
44 * @param EmailConfirmationService $emailConfirmationService
45 * @param UserRepo $userRepo
47 public function __construct(SocialAuthService $socialAuthService, EmailConfirmationService $emailConfirmationService, UserRepo $userRepo)
49 $this->middleware('guest', ['only' => ['getLogin', 'postLogin', 'getRegister']]);
50 $this->socialAuthService = $socialAuthService;
51 $this->emailConfirmationService = $emailConfirmationService;
52 $this->userRepo = $userRepo;
53 parent::__construct();
57 * Get a validator for an incoming registration request.
60 * @return \Illuminate\Contracts\Validation\Validator
62 protected function validator(array $data)
64 return Validator::make($data, [
65 'name' => 'required|max:255',
66 'email' => 'required|email|max:255|unique:users',
67 'password' => 'required|min:6',
72 * Create a new user instance after a valid registration.
77 protected function create(array $data)
80 'name' => $data['name'],
81 'email' => $data['email'],
82 'password' => bcrypt($data['password']),
86 protected function checkRegistrationAllowed()
88 if(!\Setting::get('registration-enabled')) {
89 throw new UserRegistrationException('Registrations are currently disabled.', '/login');
94 * Show the application registration form.
96 * @return \Illuminate\Http\Response
98 public function getRegister()
100 $this->checkRegistrationAllowed();
101 $socialDrivers = $this->socialAuthService->getActiveDrivers();
102 return view('auth.register', ['socialDrivers' => $socialDrivers]);
106 * Handle a registration request for the application.
108 * @param \Illuminate\Http\Request $request
109 * @return \Illuminate\Http\Response
110 * @throws UserRegistrationException
112 public function postRegister(Request $request)
114 $this->checkRegistrationAllowed();
115 $validator = $this->validator($request->all());
117 if ($validator->fails()) {
118 $this->throwValidationException(
123 if(\Setting::get('registration-restrict')) {
124 $restrictedEmailDomains = explode(',', str_replace(' ', '', \Setting::get('registration-restrict')));
125 $userEmailDomain = $domain = substr(strrchr($request->get('email'), "@"), 1);
126 if(!in_array($userEmailDomain, $restrictedEmailDomains)) {
127 throw new UserRegistrationException('That email domain does not have access to this application', '/register');
131 $newUser = $this->create($request->all());
132 $newUser->attachRoleId(\Setting::get('registration-role'), 1);
134 if(\Setting::get('registration-confirmation') || \Setting::get('registration-restrict')) {
135 $newUser->email_confirmed = false;
137 $this->emailConfirmationService->sendConfirmation($newUser);
138 return redirect('/register/confirm');
141 auth()->login($newUser);
142 return redirect($this->redirectPath());
146 * Show the page to tell the user to check thier email
147 * and confirm their address.
149 public function getRegisterConfirmation()
151 return view('auth/register-confirm');
155 * Confirms an email via a token and logs the user into the system.
157 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
158 * @throws UserRegistrationException
160 public function confirmEmail($token)
162 $confirmation = $this->emailConfirmationService->getEmailConfirmationFromToken($token);
163 $user = $confirmation->user;
164 $user->email_confirmed = true;
166 auth()->login($confirmation->user);
167 session()->flash('success', 'Your email has been confirmed!');
168 $this->emailConfirmationService->deleteConfirmationsByUser($user);
169 return redirect($this->redirectPath);
173 * Shows a notice that a user's email address has not been confirmed,
174 * Also has the option to re-send the confirmation email.
175 * @return \Illuminate\View\View
177 public function showAwaitingConfirmation()
179 return view('auth/user-unconfirmed');
183 * Resend the confirmation email
184 * @param Request $request
185 * @return \Illuminate\View\View
187 public function resendConfirmation(Request $request)
189 $this->validate($request, [
190 'email' => 'required|email|exists:users,email'
192 $user = $this->userRepo->getByEmail($request->get('email'));
193 $this->emailConfirmationService->sendConfirmation($user);
194 \Session::flash('success', 'Confirmation email resent, Please check your inbox.');
195 return redirect('/register/confirm');
199 * Show the application login form.
201 * @return \Illuminate\Http\Response
203 public function getLogin()
206 if (view()->exists('auth.authenticate')) {
207 return view('auth.authenticate');
210 $socialDrivers = $this->socialAuthService->getActiveDrivers();
211 return view('auth.login', ['socialDrivers' => $socialDrivers]);
215 * Redirect to the relevant social site.
216 * @param $socialDriver
217 * @return \Symfony\Component\HttpFoundation\RedirectResponse
219 public function getSocialLogin($socialDriver)
221 return $this->socialAuthService->startLogIn($socialDriver);
225 * The callback for social login services.
227 * @param $socialDriver
228 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
229 * @throws SocialSignInException
231 public function socialCallback($socialDriver)
233 return $this->socialAuthService->handleCallback($socialDriver);
237 * Detach a social account from a user.
238 * @param $socialDriver
239 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
241 public function detachSocialAccount($socialDriver)
243 return $this->socialAuthService->detachSocialAccount($socialDriver);