Skip to content

Commit

Permalink
Allow email verification middleware to work with API routes
Browse files Browse the repository at this point in the history
Currently if you apply the email verification middleware to an API route (XHR) you get strange/broken behaviour because of the redirect.
So, when the request expects JSON, just return a 403. This solves my issues.
  • Loading branch information
ThomHurks committed Sep 4, 2018
1 parent 6a8ab13 commit 0e23b6a
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/Illuminate/Auth/Middleware/EnsureEmailIsVerified.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,18 @@ class EnsureEmailIsVerified
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return \Illuminate\Http\Response
* @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
*/
public function handle($request, Closure $next)
{
if (! $request->user() ||
($request->user() instanceof MustVerifyEmail &&
! $request->user()->hasVerifiedEmail())) {
return Redirect::route('verification.notice');
if ($request->expectsJson()) {
abort(403, 'Your email address is not verified.');
} else {
return Redirect::route('verification.notice');
}
}

return $next($request);
Expand Down

0 comments on commit 0e23b6a

Please sign in to comment.