Answer the question
In order to leave comments, you need to log in
How to do authorization by token for API in Laravel?
I'm trying to use the existing authorization for the API by token out of the box. The process seems to have been studied and partially implemented. I set up authorization by token, hung on a group of routes:
Route::group(['middleware' => ['auth:api']], function() {
// тут прописал все свои маршруты
});
Answer the question
In order to leave comments, you need to log in
In order not to reinvent the wheel, you can use the ready-made solution Artem0071 https://github.com/tymondesigns/jwt-auth
In my latest project, I made it simple. When accessing the API, you must pass the token in the header, in the X-Auth-Token field. The implementation is simple. We write middleware:
<?php
namespace App\Http\Middleware;
use App\AuthToken;
use Closure;
class TokenAuth
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$_auth_token = $request->header('X-Auth-Token', null);
if ($_auth_token)
{
$_token = AuthToken::find($_auth_token);
if (!$_token)
abort('401', 'No such token. Request a new one.');
}
else
abort('401', 'No auth token provided');
return $next($request);
}
}
I also recently started studying the framework and ran into this problem
. In general, I don’t know how smart this solution is, but you can go to app/Exceptions/Handler.php
There, as I understand it, all errors are intercepted
. Unauthenticated is used for authorization, so you can do this :
/**
* Convert an authentication exception into an unauthenticated response.
*
* @param \Illuminate\Http\Request $request
* @param \Illuminate\Auth\AuthenticationException $exception
* @return \Illuminate\Http\Response
*/
protected function unauthenticated($request, AuthenticationException $exception)
{
if ($request->is('api/*')) {
// тут пишите что хотите вывести, например:
return response()->json(['error' => 'auth']);
}
if ($request->expectsJson()) {
return response()->json(['error' => 'Unauthenticated.'], 401);
}
return redirect()->guest(route('login'));
}
if ($request->is('api/*')) {
if ($exception instanceof \Symfony\Component\HttpKernel\Exception\NotFoundHttpException)
return $this->setStatusCode(400)->renderOutput();
if ($exception instanceof AuthorizationException)
return $this->setStatusCode(403)->renderOutput();
if ($exception instanceof ModelNotFoundException)
return $this->setStatusCode(404)->renderOutput();
if ($exception instanceof \Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException)
return $this->setStatusCode(405)->renderOutput();
if ($exception instanceof \Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException)
return $this->setStatusCode(405)->renderOutput();
// return $this->setData(['extension of' => get_class($exception)])->renderOutput();
}
The logic for this behavior is in \App\Http\Middleware\Authenticate:
protected function redirectTo($request)
{
if (! $request->expectsJson()) {
return route('login');
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question