3 namespace BookStack\Http\Controllers\Auth;
5 use BookStack\Auth\Access\EmailConfirmationService;
6 use BookStack\Auth\Access\LoginService;
7 use BookStack\Auth\UserRepo;
8 use BookStack\Exceptions\ConfirmationEmailException;
9 use BookStack\Exceptions\UserTokenExpiredException;
10 use BookStack\Exceptions\UserTokenNotFoundException;
11 use BookStack\Http\Controllers\Controller;
13 use Illuminate\Http\RedirectResponse;
14 use Illuminate\Http\Request;
15 use Illuminate\Routing\Redirector;
16 use Illuminate\View\View;
18 class ConfirmEmailController extends Controller
20 protected $emailConfirmationService;
21 protected $loginService;
25 * Create a new controller instance.
27 public function __construct(
28 EmailConfirmationService $emailConfirmationService,
29 LoginService $loginService,
32 $this->emailConfirmationService = $emailConfirmationService;
33 $this->loginService = $loginService;
34 $this->userRepo = $userRepo;
38 * Show the page to tell the user to check their email
39 * and confirm their address.
41 public function show()
43 return view('auth.register-confirm');
47 * Shows a notice that a user's email address has not been confirmed,
48 * Also has the option to re-send the confirmation email.
50 public function showAwaiting()
52 $user = $this->loginService->getLastLoginAttemptUser();
54 return view('auth.user-unconfirmed', ['user' => $user]);
58 * Confirms an email via a token and logs the user into the system.
62 * @throws ConfirmationEmailException
65 * @return RedirectResponse|Redirector
67 public function confirm($token)
70 $userId = $this->emailConfirmationService->checkTokenAndGetUserId($token);
71 } catch (Exception $exception) {
72 if ($exception instanceof UserTokenNotFoundException) {
73 $this->showErrorNotification(trans('errors.email_confirmation_invalid'));
75 return redirect('/register');
78 if ($exception instanceof UserTokenExpiredException) {
79 $user = $this->userRepo->getById($exception->userId);
80 $this->emailConfirmationService->sendConfirmation($user);
81 $this->showErrorNotification(trans('errors.email_confirmation_expired'));
83 return redirect('/register/confirm');
89 $user = $this->userRepo->getById($userId);
90 $user->email_confirmed = true;
93 $this->emailConfirmationService->deleteByUser($user);
94 $this->showSuccessNotification(trans('auth.email_confirm_success'));
95 $this->loginService->login($user, auth()->getDefaultDriver());
101 * Resend the confirmation email.
103 * @param Request $request
107 public function resend(Request $request)
109 $this->validate($request, [
110 'email' => 'required|email|exists:users,email',
112 $user = $this->userRepo->getByEmail($request->get('email'));
115 $this->emailConfirmationService->sendConfirmation($user);
116 } catch (Exception $e) {
117 $this->showErrorNotification(trans('auth.email_confirm_send_error'));
119 return redirect('/register/confirm');
122 $this->showSuccessNotification(trans('auth.email_confirm_resent'));
124 return redirect('/register/confirm');