File tree 1 file changed +37
-0
lines changed
1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change
1
+ hashkey-middleware
2
+ ==================
3
+
4
+ 驗證使用權限的 Middleware。
5
+
6
+ CLI Usage
7
+ ---------
8
+
9
+ node index.js [number]
10
+
11
+ Hashkey
12
+ ---------
13
+
14
+ - 用來使用 API 的鑰匙
15
+ - 在生產環境中的有效時間為 15 分鐘
16
+ - Package: [ Hashids] ( https://www.npmjs.com/package/hashids )
17
+
18
+ ``` js
19
+ const Hashids = require (' hashids/cjs' );
20
+ const hashSalt = process .env .SALT || ' my salt' ;
21
+ const idHashids = new Hashids (hashSalt, 32 , ' 0123456789abcdef' );
22
+
23
+ function genHashId (id = 141236 ) {
24
+ const time = Date .now (); // Timestamp
25
+ const idHash = idHashids .encode ([id, time]); // encode id and time to fake hex
26
+ return idHash;
27
+ }
28
+
29
+ // 編碼
30
+ const idHash = genHashId ();
31
+ // 解碼
32
+ const decodedId = idHashids .decode (idHash);
33
+
34
+ console .log (' Original ID: %d' , id);
35
+ console .log (' Encoded ID: %s' , idHash);
36
+ console .log (' Decoded ID: %j' , decodedId);
37
+ ```
You can’t perform that action at this time.
0 commit comments