https://github.com/coderivan/jwt-util
https://github.com/coderivan/jwt-util
jwt nodejs started tutorial
Last synced: 10 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/coderivan/jwt-util
- Owner: CoderIvan
- Created: 2020-10-13T07:10:42.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2023-01-07T21:44:28.000Z (about 3 years ago)
- Last Synced: 2025-02-06T01:47:11.992Z (12 months ago)
- Topics: jwt, nodejs, started, tutorial
- Language: JavaScript
- Homepage:
- Size: 360 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 11
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# jwt-util

## 目的
通过自己撸代码实现,加深对[JWT](https://jwt.io/introduction/)技术原理的理解
API接口参照[node-jsonwebtoken](https://github.com/auth0/node-jsonwebtoken)
目前只实现了`HS256`、`RS256`、`ES256`
## 使用
```javascript
const jwt = require('../../lib')
const secret = 'your-256-bit-secret'
const token = jwt.sign(
{
sub: '1234567890',
name: 'John Doe',
admin: true,
iat: 1516239022,
},
secret,
{
algorithm: 'HS256',
}
)
console.log(token) // 输出: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWUsImlhdCI6MTUxNjIzOTAyMn0.reGQzG3OKdoIMWLDKOZ4TICJit3EW69cQE72E2CfzRE
const isValid = jwt.verify(token, secret)
console.log(isValid) // 输出: true
```
其它见[example](https://github.com/CoderIvan/jwt-util/tree/main/example)