3 namespace BookStack\Http\Controllers\Auth;
5 use BookStack\Actions\ActivityType;
6 use BookStack\Auth\Access\EmailConfirmationService;
7 use BookStack\Auth\Access\LoginService;
8 use BookStack\Auth\UserRepo;
9 use BookStack\Exceptions\ConfirmationEmailException;
10 use BookStack\Exceptions\UserTokenExpiredException;
11 use BookStack\Exceptions\UserTokenNotFoundException;
12 use BookStack\Facades\Theme;
13 use BookStack\Http\Controllers\Controller;
14 use BookStack\Theming\ThemeEvents;
16 use Illuminate\Http\RedirectResponse;
17 use Illuminate\Http\Request;
18 use Illuminate\Routing\Redirector;
19 use Illuminate\View\View;
21 class ConfirmEmailController extends Controller
23 protected $emailConfirmationService;
24 protected $loginService;
28 * Create a new controller instance.
30 public function __construct(
31 EmailConfirmationService $emailConfirmationService,
32 LoginService $loginService,
36 $this->emailConfirmationService = $emailConfirmationService;
37 $this->loginService = $loginService;
38 $this->userRepo = $userRepo;
42 * Show the page to tell the user to check their email
43 * and confirm their address.
45 public function show()
47 return view('auth.register-confirm');
51 * Shows a notice that a user's email address has not been confirmed,
52 * Also has the option to re-send the confirmation email.
56 public function showAwaiting()
58 return view('auth.user-unconfirmed');
62 * Confirms an email via a token and logs the user into the system.
66 * @throws ConfirmationEmailException
69 * @return RedirectResponse|Redirector
71 public function confirm($token)
74 $userId = $this->emailConfirmationService->checkTokenAndGetUserId($token);
75 } catch (Exception $exception) {
76 if ($exception instanceof UserTokenNotFoundException) {
77 $this->showErrorNotification(trans('errors.email_confirmation_invalid'));
79 return redirect('/register');
82 if ($exception instanceof UserTokenExpiredException) {
83 $user = $this->userRepo->getById($exception->userId);
84 $this->emailConfirmationService->sendConfirmation($user);
85 $this->showErrorNotification(trans('errors.email_confirmation_expired'));
87 return redirect('/register/confirm');
93 $user = $this->userRepo->getById($userId);
94 $user->email_confirmed = true;
97 $this->loginService->login($user, auth()->getDefaultDriver());
98 $this->showSuccessNotification(trans('auth.email_confirm_success'));
99 $this->emailConfirmationService->deleteByUser($user);
101 return redirect('/');
105 * Resend the confirmation email.
107 * @param Request $request
111 public function resend(Request $request)
113 $this->validate($request, [
114 'email' => 'required|email|exists:users,email',
116 $user = $this->userRepo->getByEmail($request->get('email'));
119 $this->emailConfirmationService->sendConfirmation($user);
120 } catch (Exception $e) {
121 $this->showErrorNotification(trans('auth.email_confirm_send_error'));
123 return redirect('/register/confirm');
126 $this->showSuccessNotification(trans('auth.email_confirm_resent'));
128 return redirect('/register/confirm');