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;
36 * Show the page to tell the user to check their email
37 * and confirm their address.
39 public function show()
41 return view('auth.register-confirm');
45 * Shows a notice that a user's email address has not been confirmed,
46 * 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.
57 * @return RedirectResponse|Redirector
58 * @throws ConfirmationEmailException
61 public function confirm($token)
64 $userId = $this->emailConfirmationService->checkTokenAndGetUserId($token);
65 } catch (Exception $exception) {
66 if ($exception instanceof UserTokenNotFoundException) {
67 $this->showErrorNotification(trans('errors.email_confirmation_invalid'));
68 return redirect('/register');
71 if ($exception instanceof UserTokenExpiredException) {
72 $user = $this->userRepo->getById($exception->userId);
73 $this->emailConfirmationService->sendConfirmation($user);
74 $this->showErrorNotification(trans('errors.email_confirmation_expired'));
75 return redirect('/register/confirm');
81 $user = $this->userRepo->getById($userId);
82 $user->email_confirmed = true;
86 Theme::dispatch(ThemeEvents::AUTH_LOGIN, auth()->getDefaultDriver(), $user);
87 $this->logActivity(ActivityType::AUTH_LOGIN, $user);
88 $this->showSuccessNotification(trans('auth.email_confirm_success'));
89 $this->emailConfirmationService->deleteByUser($user);
96 * Resend the confirmation email
97 * @param Request $request
100 public function resend(Request $request)
102 $this->validate($request, [
103 'email' => 'required|email|exists:users,email'
105 $user = $this->userRepo->getByEmail($request->get('email'));
108 $this->emailConfirmationService->sendConfirmation($user);
109 } catch (Exception $e) {
110 $this->showErrorNotification(trans('auth.email_confirm_send_error'));
111 return redirect('/register/confirm');
114 $this->showSuccessNotification(trans('auth.email_confirm_resent'));
115 return redirect('/register/confirm');