title: Sanctum form request validation on fail redirect to home
body: <p>I am creating API with sanctum, and works fine I can list users, update, store, etc.</p>
  <p>I added middleware auth:sanctum and passing token, and I add middleware EnsureFrontendRequestsAreStateful like explained in docs.</p>
  <p>With invalid token doesn't work and I am redirect to login.</p>
  <p>However, issue happen when validation failed, I am using form request, but even with in controller validation, when fail then I am redirect to home.</p>
  <p>I am using Postman for testing API and if I disable &quot;Automatically follow redirects&quot; I can see html page for redirect used by setTargetUrl, this:</p>
  <pre><code>&lt;html&gt;
      &lt;head&gt;
          &lt;meta charset=&quot;UTF-8&quot; /&gt;
          &lt;meta http-equiv=&quot;refresh&quot; content=&quot;0;url='%1$s'&quot; /&gt;
  
          &lt;title&gt;Redirecting to %1$s&lt;/title&gt;
      &lt;/head&gt;
      &lt;body&gt;
          Redirecting to &lt;a href=&quot;%1$s&quot;&gt;%1$s&lt;/a&gt;.
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  <p>which point to my home.</p>
  <p>I try to check from where is done this redirect, but can't understand. And is not by RedirectIfAuthenticated.</p>
  <p>I expect to see a JSON response of with errors messages.</p>
  <p>Even the example code of Laravel docs for create a token, if validation fail, there is this redirect, this code:</p>
  <pre><code>        $request-&gt;validate([
              'email' =&gt; 'required|email',
              'password' =&gt; 'required',
              'device_name' =&gt; 'required',
          ]);
  
          $user = User::where('email', $request-&gt;email)-&gt;first();
  
          if (! $user || ! Hash::check($request-&gt;password, $user-&gt;password)) {
              throw ValidationException::withMessages([
                  'email' =&gt; ['The provided credentials are incorrect.'],
              ]);
          }
  
          return $user-&gt;createToken($request-&gt;device_name)-&gt;plainTextToken;
  </code></pre>
  <p>And only when validation fail, but there is not a redirect, so I suppose validation do a redirect back but since there is not, it redirect to home.</p>
  <p>Thanks</p>
  <p>Update</p>
  <p>I found a workaround but not sure if this is the only way.</p>
  <p>Adding a try catch I can display validation errors without redirect.</p>
  <pre><code>
      try {
  
          $data = $request-&gt;validate([
              'name' =&gt; 'required',
              'email' =&gt; 'required|email|unique:users,email',
              'password' =&gt; 'required',
          ]);
  
      } catch(\Illuminate\Validation\ValidationException $e) {
          
          return response()-&gt;json([
              'message' =&gt; $e-&gt;getMessage()
          ]);
      }
  </code></pre>