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\Request;
15 class ConfirmEmailController extends Controller
17 protected EmailConfirmationService $emailConfirmationService;
18 protected LoginService $loginService;
19 protected UserRepo $userRepo;
22 * Create a new controller instance.
24 public function __construct(
25 EmailConfirmationService $emailConfirmationService,
26 LoginService $loginService,
29 $this->emailConfirmationService = $emailConfirmationService;
30 $this->loginService = $loginService;
31 $this->userRepo = $userRepo;
35 * Show the page to tell the user to check their email
36 * and confirm their address.
38 public function show()
40 return view('auth.register-confirm');
44 * Shows a notice that a user's email address has not been confirmed,
45 * Also has the option to re-send the confirmation email.
47 public function showAwaiting()
49 $user = $this->loginService->getLastLoginAttemptUser();
51 return view('auth.user-unconfirmed', ['user' => $user]);
55 * Show the form for a user to provide their positive confirmation of their email.
57 public function showAcceptForm(string $token)
59 return view('auth.register-confirm-accept', ['token' => $token]);
63 * Confirms an email via a token and logs the user into the system.
65 * @throws ConfirmationEmailException
68 public function confirm(Request $request)
70 $validated = $this->validate($request, [
71 'token' => ['required', 'string']
74 $token = $validated['token'];
77 $userId = $this->emailConfirmationService->checkTokenAndGetUserId($token);
78 } catch (UserTokenNotFoundException $exception) {
79 $this->showErrorNotification(trans('errors.email_confirmation_invalid'));
81 return redirect('/register');
82 } catch (UserTokenExpiredException $exception) {
83 $user = $this->userRepo->getById($exception->userId);
84 $this->emailConfirmationService->sendConfirmation($user);
85 $this->showErrorNotification(trans('errors.email_confirmation_expired'));
87 return redirect('/register/confirm');
90 $user = $this->userRepo->getById($userId);
91 $user->email_confirmed = true;
94 $this->emailConfirmationService->deleteByUser($user);
95 $this->showSuccessNotification(trans('auth.email_confirm_success'));
97 return redirect('/login');
101 * Resend the confirmation email.
103 public function resend(Request $request)
105 $this->validate($request, [
106 'email' => ['required', 'email', 'exists:users,email'],
108 $user = $this->userRepo->getByEmail($request->get('email'));
111 $this->emailConfirmationService->sendConfirmation($user);
112 } catch (Exception $e) {
113 $this->showErrorNotification(trans('auth.email_confirm_send_error'));
115 return redirect('/register/confirm');
118 $this->showSuccessNotification(trans('auth.email_confirm_resent'));
120 return redirect('/register/confirm');