3 namespace BookStack\Access\Controllers;
5 use BookStack\Access\EmailConfirmationService;
6 use BookStack\Access\LoginService;
7 use BookStack\Exceptions\ConfirmationEmailException;
8 use BookStack\Exceptions\UserTokenExpiredException;
9 use BookStack\Exceptions\UserTokenNotFoundException;
10 use BookStack\Http\Controller;
11 use BookStack\Users\UserRepo;
13 use Illuminate\Http\Request;
15 class ConfirmEmailController extends Controller
17 public function __construct(
18 protected EmailConfirmationService $emailConfirmationService,
19 protected LoginService $loginService,
20 protected UserRepo $userRepo
25 * Show the page to tell the user to check their email
26 * and confirm their address.
28 public function show()
30 return view('auth.register-confirm');
34 * Shows a notice that a user's email address has not been confirmed,
35 * along with the option to re-send the confirmation email.
37 public function showAwaiting()
39 $user = $this->loginService->getLastLoginAttemptUser();
41 $this->showErrorNotification(trans('errors.login_user_not_found'));
42 return redirect('/login');
45 return view('auth.register-confirm-awaiting');
49 * Show the form for a user to provide their positive confirmation of their email.
51 public function showAcceptForm(string $token)
53 return view('auth.register-confirm-accept', ['token' => $token]);
57 * Confirms an email via a token and logs the user into the system.
59 * @throws ConfirmationEmailException
62 public function confirm(Request $request)
64 $validated = $this->validate($request, [
65 'token' => ['required', 'string']
68 $token = $validated['token'];
71 $userId = $this->emailConfirmationService->checkTokenAndGetUserId($token);
72 } catch (UserTokenNotFoundException $exception) {
73 $this->showErrorNotification(trans('errors.email_confirmation_invalid'));
75 return redirect('/register');
76 } catch (UserTokenExpiredException $exception) {
77 $user = $this->userRepo->getById($exception->userId);
78 $this->emailConfirmationService->sendConfirmation($user);
79 $this->showErrorNotification(trans('errors.email_confirmation_expired'));
81 return redirect('/register/confirm');
84 $user = $this->userRepo->getById($userId);
85 $user->email_confirmed = true;
88 $this->emailConfirmationService->deleteByUser($user);
89 $this->showSuccessNotification(trans('auth.email_confirm_success'));
91 return redirect('/login');
95 * Resend the confirmation email.
97 public function resend()
99 $user = $this->loginService->getLastLoginAttemptUser();
100 if ($user === null) {
101 $this->showErrorNotification(trans('errors.login_user_not_found'));
102 return redirect('/login');
106 $this->emailConfirmationService->sendConfirmation($user);
107 } catch (ConfirmationEmailException $e) {
108 $this->showErrorNotification($e->getMessage());
110 return redirect('/login');
111 } catch (Exception $e) {
112 $this->showErrorNotification(trans('auth.email_confirm_send_error'));
114 return redirect('/register/awaiting');
117 $this->showSuccessNotification(trans('auth.email_confirm_resent'));
119 return redirect('/register/confirm');