mirror of
https://github.com/snobu/destreamer.git
synced 2026-01-20 23:12:16 +00:00
Early work for token cache implementation
This commit is contained in:
42
TokenCache.ts
Normal file
42
TokenCache.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import * as fs from 'fs';
|
||||
const jwtDecode = require('jwt-decode');
|
||||
|
||||
export class TokenCache {
|
||||
|
||||
public Read(): string | null {
|
||||
let token = null;
|
||||
try {
|
||||
token = fs.readFileSync(".token_cache", "utf8");
|
||||
}
|
||||
catch (e) {
|
||||
console.error(e);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
interface Jwt {
|
||||
[key: string]: any
|
||||
}
|
||||
|
||||
const decodedJwt: Jwt = jwtDecode(token);
|
||||
|
||||
let now = Math.floor(Date.now() / 1000);
|
||||
let exp = decodedJwt["exp"];
|
||||
let timeLeft = exp - now;
|
||||
|
||||
if (timeLeft < 120) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return token;
|
||||
}
|
||||
|
||||
public Write(token: string): void {
|
||||
fs.writeFile(".token_cache", token, (err: any) => {
|
||||
if (err) {
|
||||
return console.error(err);
|
||||
}
|
||||
console.log("Fresh access token dropped into .token_cache");
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user