Skip to content

Commit

Permalink
[7.x] Return API Style Responses On XHR Requests To Auth Scaffodling (#…
Browse files Browse the repository at this point in the history
…30855)

This updates the authentication scaffolding to return normal 2xx level (or 4xx on validation) responses on XHR requests to the scaffolding backend instead of returning redirects and manipulating the session, etc.

JSON payload is returned where appropriate.
  • Loading branch information
taylorotwell authored Dec 17, 2019
1 parent cf08093 commit 8ccec17
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 10 deletions.
10 changes: 8 additions & 2 deletions src/Illuminate/Foundation/Auth/AuthenticatesUsers.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Illuminate\Foundation\Auth;

use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Auth;
use Illuminate\Validation\ValidationException;

Expand Down Expand Up @@ -106,8 +107,13 @@ protected function sendLoginResponse(Request $request)

$this->clearLoginAttempts($request);

return $this->authenticated($request, $this->guard()->user())
?: redirect()->intended($this->redirectPath());
if ($response = $this->authenticated($request, $this->guard()->user())) {
return $response;
}

return $request->wantsJson()
? new Response('', 204)
: redirect()->intended($this->redirectPath());
}

/**
Expand Down
5 changes: 4 additions & 1 deletion src/Illuminate/Foundation/Auth/ConfirmsPasswords.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Illuminate\Foundation\Auth;

use Illuminate\Http\Request;
use Illuminate\Http\Response;

trait ConfirmsPasswords
{
Expand Down Expand Up @@ -30,7 +31,9 @@ public function confirm(Request $request)

$this->resetPasswordConfirmationTimeout($request);

return redirect()->intended($this->redirectPath());
return $request->wantsJson()
? new Response('', 204)
: redirect()->intended($this->redirectPath());
}

/**
Expand Down
10 changes: 8 additions & 2 deletions src/Illuminate/Foundation/Auth/RegistersUsers.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Auth\Events\Registered;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Auth;

trait RegistersUsers
Expand Down Expand Up @@ -34,8 +35,13 @@ public function register(Request $request)

$this->guard()->login($user);

return $this->registered($request, $user)
?: redirect($this->redirectPath());
if ($response = $this->registered($request, $user)) {
return $response;
}

return $request->wantsJson()
? new Response('', 201)
: redirect($this->redirectPath());
}

/**
Expand Down
12 changes: 11 additions & 1 deletion src/Illuminate/Foundation/Auth/SendsPasswordResetEmails.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace Illuminate\Foundation\Auth;

use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Password;
use Illuminate\Validation\ValidationException;

trait SendsPasswordResetEmails
{
Expand Down Expand Up @@ -70,7 +72,9 @@ protected function credentials(Request $request)
*/
protected function sendResetLinkResponse(Request $request, $response)
{
return back()->with('status', trans($response));
return $request->wantsJson()
? new JsonResponse(['message' => trans($response)], 200)
: back()->with('status', trans($response));
}

/**
Expand All @@ -82,6 +86,12 @@ protected function sendResetLinkResponse(Request $request, $response)
*/
protected function sendResetLinkFailedResponse(Request $request, $response)
{
if ($request->wantsJson()) {
throw ValidationException::withMessages([
'email' => [trans($response)],
]);
}

return back()
->withInput($request->only('email'))
->withErrors(['email' => trans($response)]);
Expand Down
17 changes: 13 additions & 4 deletions src/Illuminate/Foundation/Auth/VerifiesEmails.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Auth\Events\Verified;
use Illuminate\Http\Request;
use Illuminate\Http\Response;

trait VerifiesEmails
{
Expand Down Expand Up @@ -42,14 +43,18 @@ public function verify(Request $request)
}

if ($request->user()->hasVerifiedEmail()) {
return redirect($this->redirectPath());
return $request->wantsJson()
? new Response('', 204)
: redirect($this->redirectPath());
}

if ($request->user()->markEmailAsVerified()) {
event(new Verified($request->user()));
}

return redirect($this->redirectPath())->with('verified', true);
return $request->wantsJson()
? new Response('', 204)
: redirect($this->redirectPath())->with('verified', true);
}

/**
Expand All @@ -61,11 +66,15 @@ public function verify(Request $request)
public function resend(Request $request)
{
if ($request->user()->hasVerifiedEmail()) {
return redirect($this->redirectPath());
return $request->wantsJson()
? new Response('', 204)
: redirect($this->redirectPath());
}

$request->user()->sendEmailVerificationNotification();

return back()->with('resent', true);
return $request->wantsJson()
? new Response('', 202)
: back()->with('resent', true);
}
}

0 comments on commit 8ccec17

Please sign in to comment.