【发布时间】:2016-08-23 04:03:02
【问题描述】:
我正在尝试在Laravel 5.2 中设置JWTAuth。我已经安装了所有东西,当我尝试获取令牌时它成功了。
路线文件:
$api = app('Dingo\Api\Routing\Router');
$api->version('v1',function($api)
{
$api->group(['prefix' => 'v1'], function($api)
{
$api->post('login','App\Http\Controllers\Auth\AuthController@authenticate');
});
});
AuthController@authenticate:
public function authenticate(Request $request)
{
// grab credentials from the request
$credentials = $request->only('email', 'password');
try {
// attempt to verify the credentials and create a token for the user
if (! $token = JWTAuth::attempt($credentials)) {
return response()->json(['error' => 'invalid_credentials'], 401);
}
} catch (JWTException $e) {
// something went wrong whilst attempting to encode the token
return response()->json(['error' => 'could_not_create_token'], 500);
}
// all good so return the token
return response()->json(compact('token'));
}
结果:
{"token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjEsImlzcyI6Imh0dHA6XC9cL2xuanN1cHBvcnQuZGV2XC9hcGlcL3YxXC9sb2dpbiIsImlhdCI6MTQ2MTg0MzM1OCwiZXhwIjoxNDYxODQ2OTU4LCJuYmYiOjE0NjE4NDMzNTgsImp0aSI6IjEyMThiNWUxNTNmNTBhMTA1ZTBhYTE1ZTlhMjRiYjNlIn0.0MpOWMvd2swqI-3r9hNjkjmrpVgNIDds0srlgjXKFVg"}
然后当我尝试获得这样的用户时:
Route file(网址:/users)(带中间件):
$api = app('Dingo\Api\Routing\Router');
$api->version('v1',function($api)
{
$api->group(['prefix' => 'v1', 'middleware' => 'api.auth'], function($api)
{
$api->post('login','App\Http\Controllers\Auth\AuthController@authenticate');
$api->post('users','App\Http\Controllers\Auth\AuthController@index');
});
});
AuthController@index
public function index()
{
return User::all();
}
标题: 授权持有者 {token})
我收到错误:
NotFoundHttpException in RouteCollection.php line 161:
这里有什么问题?
--EDIT1--
当我给出错误的令牌时,我收到错误:
{"message":"Could not decode token: The token
--EDIT2--
+------+--------+---------------+------+-------------------------------------------------------+-----------+------------+----------+------------+
| Host | Method | URI | Name | Action | Protected | Version(s) | Scope(s) | Rate Limit |
+------+--------+---------------+------+-------------------------------------------------------+-----------+------------+----------+------------+
| | POST | /api/v1/login | | App\Http\Controllers\Auth\AuthController@authenticate | No | v1 | | |
| | POST | /api/v1/users | | App\Http\Controllers\Auth\AuthController@index | No | v1 | | |
+------+--------+---------------+------+-------------------------------------------------------+-----------+------------+----------+------------+
【问题讨论】:
-
您是访问
/v1/users还是只是访问/users? -
我访问
/api/v1/users我使用相同的 url 来接收令牌,所以它应该可以工作。(在我的dingoapi 文件中,我设置了api的前缀) -
您能展示一下
php artisan route:list生成的内容吗? -
它显示您的应用程序没有任何路由。我猜是因为我用澳洲野狗?
-
没错!它的
api:routes这个命令
标签: php laravel-5.2 jwt