+ $userData = $request->all();
+ return $this->registerUser($userData);
+ }
+
+
+ /**
+ * Overrides the action when a user is authenticated.
+ * If the user authenticated but does not exist in the user table we create them.
+ * @param Request $request
+ * @param Authenticatable $user
+ * @return \Illuminate\Http\RedirectResponse
+ * @throws AuthException
+ */
+ protected function authenticated(Request $request, Authenticatable $user)
+ {
+ // Explicitly log them out for now if they do no exist.
+ if (!$user->exists) auth()->logout($user);
+
+ if (!$user->exists && $user->email === null && !$request->has('email')) {
+ $request->flash();
+ session()->flash('request-email', true);
+ return redirect('/login');
+ }
+
+ if (!$user->exists && $user->email === null && $request->has('email')) {
+ $user->email = $request->get('email');
+ }
+
+ if (!$user->exists) {
+
+ // Check for users with same email already
+ $alreadyUser = $user->newQuery()->where('email', '=', $user->email)->count() > 0;
+ if ($alreadyUser) {
+ throw new AuthException('A user with the email ' . $user->email . ' already exists but with different credentials.');
+ }
+
+ $user->save();
+ $this->userRepo->attachDefaultRole($user);
+ auth()->login($user);
+ }
+
+ return redirect()->intended($this->redirectPath());
+ }
+
+ /**
+ * Register a new user after a registration callback.
+ * @param $socialDriver
+ * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
+ * @throws UserRegistrationException
+ */
+ protected function socialRegisterCallback($socialDriver)
+ {
+ $socialUser = $this->socialAuthService->handleRegistrationCallback($socialDriver);
+ $socialAccount = $this->socialAuthService->fillSocialAccount($socialDriver, $socialUser);
+
+ // Create an array of the user data to create a new user instance
+ $userData = [
+ 'name' => $socialUser->getName(),
+ 'email' => $socialUser->getEmail(),
+ 'password' => str_random(30)
+ ];
+ return $this->registerUser($userData, $socialAccount);
+ }
+
+ /**
+ * The registrations flow for all users.
+ * @param array $userData
+ * @param bool|false|SocialAccount $socialAccount
+ * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
+ * @throws UserRegistrationException
+ * @throws \BookStack\Exceptions\ConfirmationEmailException
+ */
+ protected function registerUser(array $userData, $socialAccount = false)
+ {
+ if (setting('registration-restrict')) {
+ $restrictedEmailDomains = explode(',', str_replace(' ', '', setting('registration-restrict')));
+ $userEmailDomain = $domain = substr(strrchr($userData['email'], "@"), 1);
+ if (!in_array($userEmailDomain, $restrictedEmailDomains)) {
+ throw new UserRegistrationException('That email domain does not have access to this application', '/register');
+ }
+ }
+
+ $newUser = $this->userRepo->registerNew($userData);
+ if ($socialAccount) {
+ $newUser->socialAccounts()->save($socialAccount);
+ }
+
+ if (setting('registration-confirmation') || setting('registration-restrict')) {
+ $newUser->save();
+ $this->emailConfirmationService->sendConfirmation($newUser);
+ return redirect('/register/confirm');
+ }
+
+ auth()->login($newUser);
+ session()->flash('success', 'Thanks for signing up! You are now registered and signed in.');
+ return redirect($this->redirectPath());
+ }
+
+ /**
+ * Show the page to tell the user to check their email
+ * and confirm their address.
+ */
+ public function getRegisterConfirmation()
+ {
+ return view('auth/register-confirm');
+ }