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 * @param EmailConfirmationService $emailConfirmationService
26 * @param UserRepo $userRepo
28 public function __construct(EmailConfirmationService $emailConfirmationService, UserRepo $userRepo)
30 $this->emailConfirmationService = $emailConfirmationService;
31 $this->userRepo = $userRepo;
32 parent::__construct();
37 * Show the page to tell the user to check their email
38 * and confirm their address.
40 public function show()
42 return view('auth.register-confirm');
46 * Shows a notice that a user's email address has not been confirmed,
47 * Also has the option to re-send the confirmation email.
50 public function showAwaiting()
52 return view('auth.user-unconfirmed');
56 * Confirms an email via a token and logs the user into the system.
58 * @return RedirectResponse|Redirector
59 * @throws ConfirmationEmailException
62 public function confirm($token)
65 $userId = $this->emailConfirmationService->checkTokenAndGetUserId($token);
66 } catch (Exception $exception) {
67 if ($exception instanceof UserTokenNotFoundException) {
68 $this->showErrorNotification(trans('errors.email_confirmation_invalid'));
69 return redirect('/register');
72 if ($exception instanceof UserTokenExpiredException) {
73 $user = $this->userRepo->getById($exception->userId);
74 $this->emailConfirmationService->sendConfirmation($user);
75 $this->showErrorNotification(trans('errors.email_confirmation_expired'));
76 return redirect('/register/confirm');
82 $user = $this->userRepo->getById($userId);
83 $user->email_confirmed = true;
87 $this->showSuccessNotification(trans('auth.email_confirm_success'));
88 $this->emailConfirmationService->deleteByUser($user);
95 * Resend the confirmation email
96 * @param Request $request
99 public function resend(Request $request)
101 $this->validate($request, [
102 'email' => 'required|email|exists:users,email'
104 $user = $this->userRepo->getByEmail($request->get('email'));
107 $this->emailConfirmationService->sendConfirmation($user);
108 } catch (Exception $e) {
109 $this->showErrorNotification(trans('auth.email_confirm_send_error'));
110 return redirect('/register/confirm');
113 $this->showSuccessNotification(trans('auth.email_confirm_resent'));
114 return redirect('/register/confirm');