3 namespace BookStack\Http\Controllers\Auth;
5 use BookStack\Auth\Access\EmailConfirmationService;
6 use BookStack\Auth\UserRepo;
7 use BookStack\Exceptions\ConfirmationEmailException;
8 use BookStack\Exceptions\UserTokenExpiredException;
9 use BookStack\Exceptions\UserTokenNotFoundException;
10 use BookStack\Http\Controllers\Controller;
12 use Illuminate\Http\RedirectResponse;
13 use Illuminate\Http\Request;
14 use Illuminate\Routing\Redirector;
15 use Illuminate\View\View;
17 class ConfirmEmailController extends Controller
19 protected $emailConfirmationService;
23 * Create a new controller instance.
25 public function __construct(EmailConfirmationService $emailConfirmationService, UserRepo $userRepo)
27 $this->emailConfirmationService = $emailConfirmationService;
28 $this->userRepo = $userRepo;
33 * Show the page to tell the user to check their email
34 * and confirm their address.
36 public function show()
38 return view('auth.register-confirm');
42 * Shows a notice that a user's email address has not been confirmed,
43 * Also has the option to re-send the confirmation email.
46 public function showAwaiting()
48 return view('auth.user-unconfirmed');
52 * Confirms an email via a token and logs the user into the system.
54 * @return RedirectResponse|Redirector
55 * @throws ConfirmationEmailException
58 public function confirm($token)
61 $userId = $this->emailConfirmationService->checkTokenAndGetUserId($token);
62 } catch (Exception $exception) {
63 if ($exception instanceof UserTokenNotFoundException) {
64 $this->showErrorNotification(trans('errors.email_confirmation_invalid'));
65 return redirect('/register');
68 if ($exception instanceof UserTokenExpiredException) {
69 $user = $this->userRepo->getById($exception->userId);
70 $this->emailConfirmationService->sendConfirmation($user);
71 $this->showErrorNotification(trans('errors.email_confirmation_expired'));
72 return redirect('/register/confirm');
78 $user = $this->userRepo->getById($userId);
79 $user->email_confirmed = true;
83 $this->showSuccessNotification(trans('auth.email_confirm_success'));
84 $this->emailConfirmationService->deleteByUser($user);
91 * Resend the confirmation email
92 * @param Request $request
95 public function resend(Request $request)
97 $this->validate($request, [
98 'email' => 'required|email|exists:users,email'
100 $user = $this->userRepo->getByEmail($request->get('email'));
103 $this->emailConfirmationService->sendConfirmation($user);
104 } catch (Exception $e) {
105 $this->showErrorNotification(trans('auth.email_confirm_send_error'));
106 return redirect('/register/confirm');
109 $this->showSuccessNotification(trans('auth.email_confirm_resent'));
110 return redirect('/register/confirm');