]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/Auth/ConfirmEmailController.php
Started moving MFA and email confirmation to new login flow
[bookstack] / app / Http / Controllers / Auth / ConfirmEmailController.php
1 <?php
2
3 namespace BookStack\Http\Controllers\Auth;
4
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;
12 use Exception;
13 use Illuminate\Http\RedirectResponse;
14 use Illuminate\Http\Request;
15 use Illuminate\Routing\Redirector;
16 use Illuminate\View\View;
17
18 class ConfirmEmailController extends Controller
19 {
20     protected $emailConfirmationService;
21     protected $loginService;
22     protected $userRepo;
23
24     /**
25      * Create a new controller instance.
26      */
27     public function __construct(
28         EmailConfirmationService $emailConfirmationService,
29         LoginService $loginService,
30         UserRepo $userRepo
31     )
32     {
33         $this->emailConfirmationService = $emailConfirmationService;
34         $this->loginService = $loginService;
35         $this->userRepo = $userRepo;
36     }
37
38     /**
39      * Show the page to tell the user to check their email
40      * and confirm their address.
41      */
42     public function show()
43     {
44         return view('auth.register-confirm');
45     }
46
47     /**
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.
50      *
51      * @return View
52      */
53     public function showAwaiting()
54     {
55         return view('auth.user-unconfirmed');
56     }
57
58     /**
59      * Confirms an email via a token and logs the user into the system.
60      *
61      * @param $token
62      *
63      * @throws ConfirmationEmailException
64      * @throws Exception
65      *
66      * @return RedirectResponse|Redirector
67      */
68     public function confirm($token)
69     {
70         try {
71             $userId = $this->emailConfirmationService->checkTokenAndGetUserId($token);
72         } catch (Exception $exception) {
73             if ($exception instanceof UserTokenNotFoundException) {
74                 $this->showErrorNotification(trans('errors.email_confirmation_invalid'));
75
76                 return redirect('/register');
77             }
78
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'));
83
84                 return redirect('/register/confirm');
85             }
86
87             throw $exception;
88         }
89
90         $user = $this->userRepo->getById($userId);
91         $user->email_confirmed = true;
92         $user->save();
93
94         $this->emailConfirmationService->deleteByUser($user);
95         $this->showSuccessNotification(trans('auth.email_confirm_success'));
96         $this->loginService->login($user, auth()->getDefaultDriver());
97
98         return redirect('/');
99     }
100
101     /**
102      * Resend the confirmation email.
103      *
104      * @param Request $request
105      *
106      * @return View
107      */
108     public function resend(Request $request)
109     {
110         $this->validate($request, [
111             'email' => 'required|email|exists:users,email',
112         ]);
113         $user = $this->userRepo->getByEmail($request->get('email'));
114
115         try {
116             $this->emailConfirmationService->sendConfirmation($user);
117         } catch (Exception $e) {
118             $this->showErrorNotification(trans('auth.email_confirm_send_error'));
119
120             return redirect('/register/confirm');
121         }
122
123         $this->showSuccessNotification(trans('auth.email_confirm_resent'));
124
125         return redirect('/register/confirm');
126     }
127 }