Solución:
Cambié el AuthController
a algo como
<?php
namespace AppHttpControllers;
use Auth;
use IlluminateHttpRequest;
class AuthController extends Controller
{
/**
* Create a new AuthController instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth:api', ['except' => ['login']]);
}
/**
* Get a JWT via given credentials.
*
* @return IlluminateHttpJsonResponse
*/
public function login()
{
$credentials = request(['username', 'password']);
$token = auth()->guard('api')->attempt($credentials);
if (!$token) {
return response()->json(['error' => 'Unauthorized'], 401);
}
return $this->respondWithToken($token);
}
/**
* Log the user out (Invalidate the token).
*
* @return IlluminateHttpJsonResponse
*/
public function logout()
{
auth()->guard('api')->logout();
return response()->json(['message' => 'Successfully logged out']);
}
/**
* Refresh a token.
*
* @return IlluminateHttpJsonResponse
*/
public function refresh()
{
return $this->respondWithToken(auth()->refresh());
}
/**
* Get the token array structure.
*
* @param string $token
*
* @return IlluminateHttpJsonResponse
*/
protected function respondWithToken($token)
{
return response()->json([
'access_token' => $token,
'token_type' => 'bearer',
'expires_in' => auth('api')->factory()->getTTL() * 60,
]);
}
}
Y en api.php, cambiar auth a jwt.auth resuelve el problema.
Route::group([
'middleware' => 'api',
'prefix' => 'auth'
], function ($router) {
Route::post('register', 'Auth[email protected]')->name('api.register');
Route::post('forgot-password', 'Auth[email protected]')->name('api.forgot-password');
Route::post('login', 'Auth[email protected]')->name('api.login');
Route::middleware('jwt.auth')->post('logout', 'Auth[email protected]')->name('api.logout');
Route::middleware('auth')->post('refresh', 'Auth[email protected]')->name('api.refresh');
Route::middleware('jwt.auth')->post('me', 'Auth[email protected]')->name('api.me');
});
Ruta API, debe usar cartero de Chrome / aplicación para probar la API
Route::group(['prefix' => 'auth',namespace =>'AppHttpController'], function () {
Route::post('login', 'Auth[email protected]')->name('api.login');
Route::group(['middleware' => 'auth:api'], function () {
Route::post('register', 'Auth[email protected]')->name('api.register');
Route::post('forgot-password', 'Auth[email protected]')->name('api.forgot-password');
Route::post('logout', 'Auth[email protected]')->name('api.logout');
});
});
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',
'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',
],
],
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)