Answer the question
In order to leave comments, you need to log in
How to disable redirect to /login for guests for route with ->middleware('auth:api')?
In routes:
Route::any('/test, [TestController::class, 'main'])->middleware('auth:api');
protected function redirectTo($request)
{
if (! $request->expectsJson()) {
return route('login');
}
}
Answer the question
In order to leave comments, you need to log in
1) in the app/Exceptions/Handler.php file, override the unauthenticated method from vendor/laravel/framework/src/Illuminate/Foundation/Exceptions/Handler.php
, that is, do something like this
protected function unauthenticated($request, AuthenticationException $exception)
{
return response()->json(['message' => $exception->getMessage()], 401)
}
public function render($request, Throwable $e)
{
if ($e instanceof CustomAuthenticationException) {
return response()->json(['message' => $e->getMessage()], 401);
}
return parent::render($request, $e);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question