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;
18 protected $loginService;
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 * Confirms an email via a token and logs the user into the system.
57 * @throws ConfirmationEmailException
60 public function confirm(string $token)
63 $userId = $this->emailConfirmationService->checkTokenAndGetUserId($token);
64 } catch (UserTokenNotFoundException $exception) {
65 $this->showErrorNotification(trans('errors.email_confirmation_invalid'));
67 return redirect('/register');
68 } catch (UserTokenExpiredException $exception) {
69 $user = $this->userRepo->getById($exception->userId);
70 $this->emailConfirmationService->sendConfirmation($user);
71 $this->showErrorNotification(trans('errors.email_confirmation_expired'));
73 return redirect('/register/confirm');
76 $user = $this->userRepo->getById($userId);
77 $user->email_confirmed = true;
80 $this->emailConfirmationService->deleteByUser($user);
81 $this->showSuccessNotification(trans('auth.email_confirm_success'));
83 return redirect('/login');
87 * Resend the confirmation email.
89 public function resend(Request $request)
91 $this->validate($request, [
92 'email' => ['required', 'email', 'exists:users,email'],
94 $user = $this->userRepo->getByEmail($request->get('email'));
97 $this->emailConfirmationService->sendConfirmation($user);
98 } catch (Exception $e) {
99 $this->showErrorNotification(trans('auth.email_confirm_send_error'));
101 return redirect('/register/confirm');
104 $this->showSuccessNotification(trans('auth.email_confirm_resent'));
106 return redirect('/register/confirm');