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 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 * Also has the option to re-send the confirmation email.
37 public function showAwaiting()
39 $user = $this->loginService->getLastLoginAttemptUser();
41 return view('auth.user-unconfirmed', ['user' => $user]);
45 * Show the form for a user to provide their positive confirmation of their email.
47 public function showAcceptForm(string $token)
49 return view('auth.register-confirm-accept', ['token' => $token]);
53 * Confirms an email via a token and logs the user into the system.
55 * @throws ConfirmationEmailException
58 public function confirm(Request $request)
60 $validated = $this->validate($request, [
61 'token' => ['required', 'string']
64 $token = $validated['token'];
67 $userId = $this->emailConfirmationService->checkTokenAndGetUserId($token);
68 } catch (UserTokenNotFoundException $exception) {
69 $this->showErrorNotification(trans('errors.email_confirmation_invalid'));
71 return redirect('/register');
72 } catch (UserTokenExpiredException $exception) {
73 $user = $this->userRepo->getById($exception->userId);
74 $this->emailConfirmationService->sendConfirmation($user);
75 $this->showErrorNotification(trans('errors.email_confirmation_expired'));
77 return redirect('/register/confirm');
80 $user = $this->userRepo->getById($userId);
81 $user->email_confirmed = true;
84 $this->emailConfirmationService->deleteByUser($user);
85 $this->showSuccessNotification(trans('auth.email_confirm_success'));
87 return redirect('/login');
91 * Resend the confirmation email.
93 public function resend(Request $request)
95 $this->validate($request, [
96 'email' => ['required', 'email', 'exists:users,email'],
98 $user = $this->userRepo->getByEmail($request->get('email'));
101 $this->emailConfirmationService->sendConfirmation($user);
102 } catch (Exception $e) {
103 $this->showErrorNotification(trans('auth.email_confirm_send_error'));
105 return redirect('/register/confirm');
108 $this->showSuccessNotification(trans('auth.email_confirm_resent'));
110 return redirect('/register/confirm');