3 namespace BookStack\Http\Controllers\Auth;
5 use BookStack\Actions\ActivityType;
6 use BookStack\Auth\Access\EmailConfirmationService;
7 use BookStack\Auth\UserRepo;
8 use BookStack\Exceptions\ConfirmationEmailException;
9 use BookStack\Exceptions\UserTokenExpiredException;
10 use BookStack\Exceptions\UserTokenNotFoundException;
11 use BookStack\Facades\Theme;
12 use BookStack\Http\Controllers\Controller;
13 use BookStack\Theming\ThemeEvents;
15 use Illuminate\Http\RedirectResponse;
16 use Illuminate\Http\Request;
17 use Illuminate\Routing\Redirector;
18 use Illuminate\View\View;
20 class ConfirmEmailController extends Controller
22 protected $emailConfirmationService;
26 * Create a new controller instance.
28 public function __construct(EmailConfirmationService $emailConfirmationService, UserRepo $userRepo)
30 $this->emailConfirmationService = $emailConfirmationService;
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.
49 public function showAwaiting()
51 return view('auth.user-unconfirmed');
55 * Confirms an email via a token and logs the user into the system.
59 * @throws ConfirmationEmailException
62 * @return RedirectResponse|Redirector
64 public function confirm($token)
67 $userId = $this->emailConfirmationService->checkTokenAndGetUserId($token);
68 } catch (Exception $exception) {
69 if ($exception instanceof UserTokenNotFoundException) {
70 $this->showErrorNotification(trans('errors.email_confirmation_invalid'));
72 return redirect('/register');
75 if ($exception instanceof UserTokenExpiredException) {
76 $user = $this->userRepo->getById($exception->userId);
77 $this->emailConfirmationService->sendConfirmation($user);
78 $this->showErrorNotification(trans('errors.email_confirmation_expired'));
80 return redirect('/register/confirm');
86 $user = $this->userRepo->getById($userId);
87 $user->email_confirmed = true;
91 Theme::dispatch(ThemeEvents::AUTH_LOGIN, auth()->getDefaultDriver(), $user);
92 $this->logActivity(ActivityType::AUTH_LOGIN, $user);
93 $this->showSuccessNotification(trans('auth.email_confirm_success'));
94 $this->emailConfirmationService->deleteByUser($user);
100 * Resend the confirmation email.
102 * @param Request $request
106 public function resend(Request $request)
108 $this->validate($request, [
109 'email' => 'required|email|exists:users,email',
111 $user = $this->userRepo->getByEmail($request->get('email'));
114 $this->emailConfirmationService->sendConfirmation($user);
115 } catch (Exception $e) {
116 $this->showErrorNotification(trans('auth.email_confirm_send_error'));
118 return redirect('/register/confirm');
121 $this->showSuccessNotification(trans('auth.email_confirm_resent'));
123 return redirect('/register/confirm');