I tried using the Auth Scaffolding of Laravel 5.3 including the api routes. I wanted to use the session driver for the api guard, but apparently this has no impact whatsoever. After I log into the application with a valid user (so I get from /login
to /home
) I tried entering the path /api/user
, but it always redirects me to /home
. The RedirectIfAuthenticated
middleware redirects the user.
Here is what I tried and a quick overview of the test application:
// In "app\Http\Middleware\RedirectIfAuthenticated.php"
if (Auth::guard($guard)->check()) {
return redirect('/home');
}
$guard
is null, and the if is true when browsing to /api/user
.
// In "config\auth.php"
'api' => [
'driver' => 'session', // changed from token to session
'provider' => 'users',
],
I changed the driver of the api guard to session
.
// In "app\Http\Kernel.php"
'api' => [
'throttle:60,1',
'bindings',
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Session\Middleware\StartSession::class,
],
I added the middlewares to support cookies in the api middleware
// In "routes\api.php"
Route::get('/user', function (Request $request) {
return $request->user();
})->middleware('auth:api');
This is an example that comes with a new Laravel installation.
// In "app\Providers\RouteServiceProvider.php"
Route::group([
'middleware' => 'api',
'namespace' => $this->namespace,
'prefix' => 'api',
], function ($router) {
require base_path('routes/api.php');
});
The api
middleware is applied to all the routes defined in the api.php
file.
I want to be able to query my API after a user has logged in without using tokens, etc. The app I wrote with Laravel 5.2 had basically the same route but only the web
middleware group and auth
middleware applied to it. In Laravel 5.3, adding the auth
middleware leads to the described problem.
edit : With my configuration I tried the following:
// In "routes\web.php"
Route::get('/test', function (Request $request) {
return "test";
})->middleware(['auth']);
This works perfectly fine, but this doesn't, although the web
and the api
guard are exactly the same inside the auth.php
.
Route::get('/test', function (Request $request) {
return "test";
})->middleware(['auth:api']);
即使我已经登录,我也遇到了在访问我的 API 路由时被重定向到/home
的相同问题。尝试更改App\\Http\\Kernel.php
api 中间件的顺序并将bindings
放在最后一个位置,以便您的自定义首先执行中间件。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.