Laravel jwtauth

时间:2016-04-28 11:51:41

标签: php laravel-5.2 jwt

我试图在JWTAuth中设置Laravel 5.2。我已经安装了所有东西,当我尝试获得令牌时它就成功了。

路线档案

$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 @认证

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 @索引

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         |          |            |
+------+--------+---------------+------+-------------------------------------------------------+-----------+------------+----------+------------+

2 个答案:

答案 0 :(得分:0)

使用GET方法:

$api->get('users','App\Http\Controllers\Auth\AuthController@index');

答案 1 :(得分:0)

发现我在AuthController中激活了中间件:

public function __construct()
{
   $this->middleware($this->guestMiddleware(), ['except' => 'logout']);
}