{"action":"opened","issue":{"url":"https://api.github.com/repos/sizeg/yii2-jwt/issues/12","repository_url":"https://api.github.com/repos/sizeg/yii2-jwt","labels_url":"https://api.github.com/repos/sizeg/yii2-jwt/issues/12/labels{/name}","comments_url":"https://api.github.com/repos/sizeg/yii2-jwt/issues/12/comments","events_url":"https://api.github.com/repos/sizeg/yii2-jwt/issues/12/events","html_url":"https://github.com/sizeg/yii2-jwt/issues/12","id":330854388,"node_id":"MDU6SXNzdWUzMzA4NTQzODg=","number":12,"title":"Authenticate with the generated token","user":{"login":"jonatasfl","id":3798405,"node_id":"MDQ6VXNlcjM3OTg0MDU=","avatar_url":"https://avatars2.githubusercontent.com/u/3798405?v=4","gravatar_id":"","url":"https://api.github.com/users/jonatasfl","html_url":"https://github.com/jonatasfl","followers_url":"https://api.github.com/users/jonatasfl/followers","following_url":"https://api.github.com/users/jonatasfl/following{/other_user}","gists_url":"https://api.github.com/users/jonatasfl/gists{/gist_id}","starred_url":"https://api.github.com/users/jonatasfl/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jonatasfl/subscriptions","organizations_url":"https://api.github.com/users/jonatasfl/orgs","repos_url":"https://api.github.com/users/jonatasfl/repos","events_url":"https://api.github.com/users/jonatasfl/events{/privacy}","received_events_url":"https://api.github.com/users/jonatasfl/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2018-06-09T05:15:12Z","updated_at":"2018-06-09T05:15:12Z","closed_at":null,"author_association":"NONE","body":"I'm trying to authenticate in my API with the generated token (my first time with JWT), but i'm receiving an Unauthorized error:\r\n\r\n```javascript\r\n{\r\n    \"name\": \"Unauthorized\",\r\n    \"message\": \"Your request was made with invalid credentials.\",\r\n    \"code\": 0,\r\n    \"status\": 401,\r\n    \"type\": \"yii\\\\web\\\\UnauthorizedHttpException\"\r\n}\r\n```\r\n\r\nI did as follows:\r\n\r\n**LoginController**\r\n\r\n```php\r\nclass LoginController extends ActiveController\r\n{\r\n    public $modelClass = 'app\\models\\User';\r\n\r\n    public function actionAuth()\r\n    {\r\n        Yii::$app->response->format = \\yii\\web\\Response::FORMAT_JSON;\r\n\r\n        $username = Yii::$app->request->post('username');\r\n        $pass = Yii::$app->request->post('password');\r\n\r\n        $modelUser = User::findByUsername($username);\r\n\r\n        if ($modelUser && $modelUser->validatePassword($pass)) {\r\n            $token = (string) User::generateToken();\r\n\r\n            return [\r\n                'status' => true,\r\n                'message' => 'Success',\r\n                'token' => $token,\r\n            ];\r\n        }\r\n\r\n        return [\r\n            'status' => false,\r\n            'message' => 'Wrong username or password'\r\n        ];\r\n\r\n    }\r\n\r\n}\r\n```\r\n\r\n**User (model)** \r\n\r\n```php\r\npublic static function generateToken()\r\n    {\r\n        $signer = new Sha256();\r\n        $token = \\Yii::$app->jwt->getBuilder()->setIssuer('http://myurl') // Configures the issuer (iss claim)\r\n            ->setAudience('http://myurl') // Configures the audience (aud claim)\r\n            ->setId('myid', true) // Configures the id (jti claim), replicating as a header item\r\n            ->setIssuedAt(time()) // Configures the time that the token was issue (iat claim)\r\n            ->setNotBefore(time() + 60) // Configures the time before which the token cannot be accepted (nbf claim)\r\n            ->setExpiration(time() + 3600) // Configures the expiration time of the token (exp claim)\r\n            ->set('uid', 1) // Configures a new claim, called \"uid\"\r\n            ->sign($signer, 'testing') // creates a signature using \"testing\" as key\r\n            ->getToken(); // Retrieves the generated token\r\n\r\n        return $token;\r\n    }\r\n```\r\n\r\n**Rest Controller**\r\n\r\n```php\r\npublic function behaviors()\r\n    {\r\n        $behaviors = parent::behaviors();\r\n        $behaviors['authenticator'] = [\r\n            'class' => JwtHttpBearerAuth::className(),\r\n        ];\r\n        return $behaviors;\r\n    }\r\n```\r\n\r\nAm I forgetting something to work properly?\r\nThank you!"}}
{"actor":{"display_login":"jonatasfl"}}