]> BookStack Code Mirror - bookstack/blobdiff - app/Exceptions/StoppedAuthenticationException.php
Fixed failing webhook test cases
[bookstack] / app / Exceptions / StoppedAuthenticationException.php
index de8898df72bec7640810baa4e58c03cc1fa57af1..b9aadb03f33e3aaa4a3a7c03d46916ce511aaade 100644 (file)
@@ -5,10 +5,10 @@ namespace BookStack\Exceptions;
 use BookStack\Auth\Access\LoginService;
 use BookStack\Auth\User;
 use Illuminate\Contracts\Support\Responsable;
+use Illuminate\Http\Request;
 
 class StoppedAuthenticationException extends \Exception implements Responsable
 {
-
     protected $user;
     protected $loginService;
 
@@ -23,18 +23,42 @@ class StoppedAuthenticationException extends \Exception implements Responsable
     }
 
     /**
-     * @inheritdoc
+     * {@inheritdoc}
      */
     public function toResponse($request)
     {
         $redirect = '/login';
 
         if ($this->loginService->awaitingEmailConfirmation($this->user)) {
-            $redirect = '/register/confirm/awaiting';
-        } else if  ($this->loginService->needsMfaVerification($this->user)) {
+            return $this->awaitingEmailConfirmationResponse($request);
+        }
+
+        if ($this->loginService->needsMfaVerification($this->user)) {
             $redirect = '/mfa/verify';
         }
 
         return redirect($redirect);
     }
-}
\ No newline at end of file
+
+    /**
+     * Provide an error response for when the current user's email is not confirmed
+     * in a system which requires it.
+     */
+    protected function awaitingEmailConfirmationResponse(Request $request)
+    {
+        if ($request->wantsJson()) {
+            return response()->json([
+                'error' => [
+                    'code'    => 401,
+                    'message' => trans('errors.email_confirmation_awaiting'),
+                ],
+            ], 401);
+        }
+
+        if (session()->pull('sent-email-confirmation') === true) {
+            return redirect('/register/confirm');
+        }
+
+        return redirect('/register/confirm/awaiting');
+    }
+}