https://github.com/webduinoio/hashkey-middleware
驗證使用權限的 Middleware
https://github.com/webduinoio/hashkey-middleware
Last synced: 10 months ago
JSON representation
驗證使用權限的 Middleware
- Host: GitHub
- URL: https://github.com/webduinoio/hashkey-middleware
- Owner: webduinoio
- Created: 2022-06-22T02:44:30.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2022-06-22T02:45:23.000Z (over 3 years ago)
- Last Synced: 2025-02-03T14:41:17.400Z (12 months ago)
- Language: JavaScript
- Size: 3.91 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
hashkey-middleware
==================
驗證使用權限的 Middleware。
CLI Usage
---------
node index.js [number|hashkey]
Hashkey
---------
- 用來使用 API 的鑰匙
- 在生產環境中的有效時間為 15 分鐘
- Package: [Hashids](https://www.npmjs.com/package/hashids)
```js
const Hashids = require('hashids/cjs');
const hashSalt = process.env.SALT || 'my salt';
const idHashids = new Hashids(hashSalt, 32, '0123456789abcdef');
function genHashId(id = 141236) {
const time = Date.now(); // Timestamp
const idHash = idHashids.encode([id, time]); // encode id and time to fake hex
return idHash;
}
// 編碼
const idHash = genHashId();
// 解碼
const decodedId = idHashids.decode(idHash);
console.log('Original ID: %d', id);
console.log('Encoded ID: %s', idHash);
console.log('Decoded ID: %j', decodedId);
```