Laravel 5.7
PHP 7.2.10
目前我可以使用 web 和 api 保护中的任何一个,有没有办法允许两者,以便 web 应用程序和 api 可以一起工作。
就像是
return [
/*
|--------------------------------------------------------------------------
| Authentication Defaults
|--------------------------------------------------------------------------
|
| This option controls the default authentication "guard" and password
| reset options for your application. You may change these defaults
| as required, but they're a perfect start for most applications.
|
*/
'defaults' => [
'guard' => 'api|web',
'passwords' => 'users',
],
在不使用架构的情况下,这是一个需要更改架构的解决方案/解决方法,我不喜欢。此外,我不需要访问令牌进行注册,这个答案在做什么。
api.php
Route::group([
'middleware' => 'api|web',
'prefix' => 'auth'
], function ($router) {
Route::post('register', 'Auth\AuthController@register')->name('api.register');
Route::post('forgot-password', 'Auth\ForgotPasswordController@forgotPassword')->name('api.forgot-password');
Route::post('login', 'Auth\AuthController@login')->name('api.login');
Route::middleware('auth')->post('logout', 'Auth\AuthController@logout')->name('api.logout');
网页.php
Auth::routes(['verify' => true]);
Route::prefix('admin')->group(function () {
Route::middleware('auth', 'permission:super-admin|association-member')->resource('users', 'Auth\UserController');
});
配置/auth.php
return [
/*
|--------------------------------------------------------------------------
| Authentication Defaults
|--------------------------------------------------------------------------
|
| This option controls the default authentication "guard" and password
| reset options for your application. You may change these defaults
| as required, but they're a perfect start for most applications.
|
*/
'defaults' => [
'guard' => 'web', //api
'passwords' => 'users',
],
/*
|--------------------------------------------------------------------------
| Authentication Guards
|--------------------------------------------------------------------------
|
| Next, you may define every authentication guard for your application.
| Of course, a great default configuration has been defined for you
| here which uses session storage and the Eloquent user provider.
|
| All authentication drivers have a user provider. This defines how the
| users are actually retrieved out of your database or other storage
| mechanisms used by this application to persist your user's data.
|
| Supported: "session", "token"
|
*/
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'jwt',
'provider' => 'users',
],
],
更新
正如@apokryfos 所说,If you want both to work for both then yes. However, I think that's bad practice. API routes should only allow API authentication since web authentication usually uses the session which API routes don't use anyway. If I were you I'd take a step back and rethink my entire strategy.
我也不想让两者都工作,我只想让 api 和 web 应用程序同时工作,现在我可以使用它们中的任何一个。
Update2 正如@Lim Kean Phang 建议的 git 问题链接
我变了
protected function respondWithToken($token)
{
return response()->json([
'access_token' => $token,
'token_type' => 'bearer',
'expires_in' => auth('api')->factory()->getTTL() * 60,//auth()->factory()->getTTL() * 60,
'status' => 200,
"response" => "Successfully login",
]);
}
expires_in 值,但现在我没有获得访问令牌。
api响应是
{
"access_token": true,
"token_type": "bearer",
"expires_in": 31536000,
"status": 200,
"response": "Successfully login"
}
更新 3 添加了一个github问题,因为找不到任何可能的解决方案来使其工作。