Laravel 5.7
PHP 7.2.10
В настоящее время я могу использовать любой из веб- и API-интерфейсов, есть ли способ разрешить и то, и другое, и веб-приложение, и 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');
web.php
Auth::routes(['verify' => true]);
Route::prefix('admin')->group(function () {
Route::middleware('auth', 'permission:super-admin|association-member')->resource('users', 'Auth\UserController');
});
config / 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, и веб-приложение параллельно, теперь я могу использовать любой из них.
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 , так как не удалось найти какое-либо возможное решение для ее работы.