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\RedirectResponse;
14 use Illuminate\Http\Request;
15 use Illuminate\Routing\Redirector;
16 use Illuminate\View\View;
18 class ConfirmEmailController extends Controller
20 protected $emailConfirmationService;
21 protected $loginService;
25 * Create a new controller instance.
27 public function __construct(
28 EmailConfirmationService $emailConfirmationService,
29 LoginService $loginService,
33 $this->emailConfirmationService = $emailConfirmationService;
34 $this->loginService = $loginService;
35 $this->userRepo = $userRepo;
39 * Show the page to tell the user to check their email
40 * and confirm their address.
42 public function show()
44 return view('auth.register-confirm');
48 * Shows a notice that a user's email address has not been confirmed,
49 * Also has the option to re-send the confirmation email.
53 public function showAwaiting()
55 return view('auth.user-unconfirmed');
59 * Confirms an email via a token and logs the user into the system.
63 * @throws ConfirmationEmailException
66 * @return RedirectResponse|Redirector
68 public function confirm($token)
71 $userId = $this->emailConfirmationService->checkTokenAndGetUserId($token);
72 } catch (Exception $exception) {
73 if ($exception instanceof UserTokenNotFoundException) {
74 $this->showErrorNotification(trans('errors.email_confirmation_invalid'));
76 return redirect('/register');
79 if ($exception instanceof UserTokenExpiredException) {
80 $user = $this->userRepo->getById($exception->userId);
81 $this->emailConfirmationService->sendConfirmation($user);
82 $this->showErrorNotification(trans('errors.email_confirmation_expired'));
84 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'));
96 $this->loginService->login($user, auth()->getDefaultDriver());
102 * Resend the confirmation email.
104 * @param Request $request
108 public function resend(Request $request)
110 $this->validate($request, [
111 'email' => 'required|email|exists:users,email',
113 $user = $this->userRepo->getByEmail($request->get('email'));
116 $this->emailConfirmationService->sendConfirmation($user);
117 } catch (Exception $e) {
118 $this->showErrorNotification(trans('auth.email_confirm_send_error'));
120 return redirect('/register/confirm');
123 $this->showSuccessNotification(trans('auth.email_confirm_resent'));
125 return redirect('/register/confirm');