Skip to content

Commit

Permalink
feat: add refresh-token api && update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
forecho committed Jul 20, 2020
1 parent 3ca3846 commit 9e5b5bf
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 7 deletions.
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ You can then access the application through the following URL:
Check out the packages
------------

- [yiithings/yii2-doten](https://github.com/forecho/yiithings/yii2-doten)
- [sizeg/yii2-jwt](https://github.com/forecho/sizeg/yii2-jwt)
- [yiier/yii2-helpers](https://github.com/forecho/yiier/yii2-helpers)
- [yiithings/yii2-doten](https://github.com/yiithings/yii2-doten)
- [sizeg/yii2-jwt](https://github.com/sizeg/yii2-jwt)
- [yiier/yii2-helpers](https://github.com/yiier/yii2-helpers)

Use
------------
Expand All @@ -99,6 +99,7 @@ At this time, you have a RESTful API server running at `http://127.0.0.1:8000`.
* `GET /health-check`: a health check service provided for health checking purpose (needed when implementing a server cluster)
* `POST /v1/join`: create a user
* `POST /v1/login`: authenticates a user and generates a JWT
* `POST /v1/refresh-token`: refresh a JWT

Try the URL `http://localhost:8000/health-check` in a browser, and you should see something like `{"code":0,"data":"OK","message":"成功"}` displayed.

Expand All @@ -113,4 +114,8 @@ curl -X POST -H "Content-Type: application/json" -d '{"username":"demo","email":
# authenticate the user via: POST /v1/login
curl -X POST -H "Content-Type: application/json" -d '{"username": "demo", "password": "pass123"}' http://localhost:8000/v1/login
# should return like: {"code":0,"data":{"user":{"id":4,"username":"dem211o1","avatar":"","email":"[email protected]","status":1,"created_at":"2020-07-17T23:49:39+08:00","updated_at":"2020-07-17T23:49:39+08:00"},"token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiIsImp0aSI6IllpaS1SRVNULUFQSSJ9.eyJpc3MiOiJodHRwOlwvXC9sb2NhbGhvc3QiLCJqdGkiOiJZaWktUkVTVC1BUEkiLCJpYXQiOjE1OTUwNjQ5NzIsImV4cCI6MTU5NTMyNDE3MiwidXNlcm5hbWUiOiJkZW0yMTFvMSIsImlkIjo0fQ.y2NSVQe-TQ08RnXnF-o55h905G9WHo6GYHNaUWlKjDE"},"message":"成功"}

# refresh a JWT
curl -X POST -H 'Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiIsImp0aSI6IllpaS1SRVNULUFQSSJ9.eyJpc3MiOiJodHRwOlwvXC9sb2NhbGhvc3QiLCJqdGkiOiJZaWktUkVTVC1BUEkiLCJpYXQiOjE1OTUwNjQ5NzIsImV4cCI6MTU5NTMyNDE3MiwidXNlcm5hbWUiOiJkZW0yMTFvMSIsImlkIjo0fQ.y2NSVQe-TQ08RnXnF-o55h905G9WHo6GYHNaUWlKjDE' http://localhost:8000/v1/refresh-token
# should return like: {"code":0,"data":{"user":{"id":4,"username":"dem211o1","avatar":"","email":"[email protected]","status":1,"created_at":"2020-07-17T23:49:39+08:00","updated_at":"2020-07-17T23:49:39+08:00"},"token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiIsImp0aSI6IllpaS1SRVNULUFQSSJ9.eyJpc3MiOiJodHRwOlwvXC9sb2NhbGhvc3QiLCJqdGkiOiJZaWktUkVTVC1BUEkiLCJpYXQiOjE1OTUwNjQ5NzIsImV4cCI6MTU5NTMyNDE3MiwidXNlcm5hbWUiOiJkZW0yMTFvMSIsImlkIjo0fQ.y2NSVQe-TQ08RnXnF-o55h905G9WHo6GYHNaUWlKjDE"},"message":"成功"}
```
3 changes: 1 addition & 2 deletions config/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
"POST <module>/join" => '<module>/user/join',
"POST <module>/login" => '<module>/user/login',
"POST <module>/<alias:login|join|refresh-token>" => '<module>/user/<alias>',
"GET health-check" => 'site/health-check',
'<module>/<controller:\w+>/<action:\w+>/<id:\d+>' => '<module>/<controller>/<action>',
],
Expand Down
1 change: 1 addition & 0 deletions core/messages/zh-CN/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
'Username' => '用户名',
'Email' => '邮箱',
'Password' => '密码',
'The JWT secret must be configured first.' => '必须先配置 JWT_SECRET',
];
2 changes: 1 addition & 1 deletion core/models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public static function findIdentity($id)
*/
public static function findIdentityByAccessToken($token, $type = null)
{
$userId = (string)$token->getClaim('uid');
$userId = (string)$token->getClaim('id');
return self::findIdentity($userId);
}

Expand Down
4 changes: 3 additions & 1 deletion core/services/UserService.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,12 @@ public function getToken(): string
{
/** @var Jwt $jwt */
$jwt = Yii::$app->jwt;
if (!$jwt->key) {
throw new InternalException(t('app', 'The JWT secret must be configured first.'));
}
$signer = $jwt->getSigner('HS256');
$key = $jwt->getKey();
$time = time();

return (string)$jwt->getBuilder()
->issuedBy(params('appUrl'))
->identifiedBy(Yii::$app->name, true)
Expand Down
10 changes: 10 additions & 0 deletions modules/v1/controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,14 @@ public function actionLogin()
'token' => (string)$token,
];
}

public function actionRefreshToken()
{
$user = Yii::$app->user->identity;
$token = $this->userService->getToken();
return [
'user' => $user,
'token' => (string)$token,
];
}
}

0 comments on commit 9e5b5bf

Please sign in to comment.