]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/Auth/ConfirmEmailController.php
fdde8e70c1b9e851cf79fac5261b72b7fa25a723
[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\Request;
14
15 class ConfirmEmailController extends Controller
16 {
17     public function __construct(
18         protected EmailConfirmationService $emailConfirmationService,
19         protected LoginService $loginService,
20         protected UserRepo $userRepo
21     ) {
22     }
23
24     /**
25      * Show the page to tell the user to check their email
26      * and confirm their address.
27      */
28     public function show()
29     {
30         return view('auth.register-confirm');
31     }
32
33     /**
34      * Shows a notice that a user's email address has not been confirmed,
35      * Also has the option to re-send the confirmation email.
36      */
37     public function showAwaiting()
38     {
39         $user = $this->loginService->getLastLoginAttemptUser();
40
41         return view('auth.user-unconfirmed', ['user' => $user]);
42     }
43
44     /**
45      * Show the form for a user to provide their positive confirmation of their email.
46      */
47     public function showAcceptForm(string $token)
48     {
49         return view('auth.register-confirm-accept', ['token' => $token]);
50     }
51
52     /**
53      * Confirms an email via a token and logs the user into the system.
54      *
55      * @throws ConfirmationEmailException
56      * @throws Exception
57      */
58     public function confirm(Request $request)
59     {
60         $validated = $this->validate($request, [
61             'token' => ['required', 'string']
62         ]);
63
64         $token = $validated['token'];
65
66         try {
67             $userId = $this->emailConfirmationService->checkTokenAndGetUserId($token);
68         } catch (UserTokenNotFoundException $exception) {
69             $this->showErrorNotification(trans('errors.email_confirmation_invalid'));
70
71             return redirect('/register');
72         } catch (UserTokenExpiredException $exception) {
73             $user = $this->userRepo->getById($exception->userId);
74             $this->emailConfirmationService->sendConfirmation($user);
75             $this->showErrorNotification(trans('errors.email_confirmation_expired'));
76
77             return redirect('/register/confirm');
78         }
79
80         $user = $this->userRepo->getById($userId);
81         $user->email_confirmed = true;
82         $user->save();
83
84         $this->emailConfirmationService->deleteByUser($user);
85         $this->showSuccessNotification(trans('auth.email_confirm_success'));
86
87         return redirect('/login');
88     }
89
90     /**
91      * Resend the confirmation email.
92      */
93     public function resend(Request $request)
94     {
95         $this->validate($request, [
96             'email' => ['required', 'email', 'exists:users,email'],
97         ]);
98         $user = $this->userRepo->getByEmail($request->get('email'));
99
100         try {
101             $this->emailConfirmationService->sendConfirmation($user);
102         } catch (Exception $e) {
103             $this->showErrorNotification(trans('auth.email_confirm_send_error'));
104
105             return redirect('/register/confirm');
106         }
107
108         $this->showSuccessNotification(trans('auth.email_confirm_resent'));
109
110         return redirect('/register/confirm');
111     }
112 }