@t2ee/cookieman
Version:
t2ee cookie middleware
36 lines (32 loc) • 989 B
text/typescript
import CookieStore from './CookieStore';
import Cookie from './Cookie';
import * as jwt from 'jsonwebtoken';
export default class JWTStore implements CookieStore {
constructor(private key: string) {}
async get(key: string): Promise<Cookie> {
const cookie = new Cookie();
try {
const result = jwt.verify(key, this.key);
cookie.expires = new Date(result.exp * 1000);
delete result['iat'];
delete result['exp'];
cookie.data = result;
cookie.key = key;
return cookie;
} catch(e) {
return null;
}
}
async set(cookie: Cookie): Promise<void> {
const result = jwt.sign(
cookie.data || {},
this.key,
{
expiresIn: Math.round((cookie.expires.getTime() - Date.now()) / 1000),
}
);
cookie.key = result;
}
async remove(cookie: Cookie): Promise<void> {
}
}