9s-fe-core
Version:
Core functionalities for authentication, configuration, and repository management.
39 lines (38 loc) • 1.47 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const nookies_1 = __importDefault(require("nookies"));
const ACCESS_TOKEN_KEY = 'accessToken';
const REFRESH_TOKEN_KEY = 'refreshToken';
class BaseStorages {
// Set token & refresh token
static setToken(token, refreshToken, ctx = null) {
nookies_1.default.set(ctx, ACCESS_TOKEN_KEY, token, {
maxAge: 60 * 60 * 24 * 7, // 7 ngày
path: '/',
secure: process.env.NODE_ENV === 'production',
httpOnly: false, // nếu muốn tránh JS access thì để true
});
if (refreshToken) {
nookies_1.default.set(ctx, REFRESH_TOKEN_KEY, refreshToken, {
maxAge: 60 * 60 * 24 * 30,
path: '/',
secure: process.env.NODE_ENV === 'production',
httpOnly: false,
});
}
}
// Lấy token
static getToken(ctx = null) {
const cookies = nookies_1.default.get(ctx);
return cookies[ACCESS_TOKEN_KEY] || null;
}
// Xóa token và refresh token
static removeToken(ctx = null) {
nookies_1.default.destroy(ctx, ACCESS_TOKEN_KEY, { path: '/' });
nookies_1.default.destroy(ctx, REFRESH_TOKEN_KEY, { path: '/' });
}
}
exports.default = BaseStorages;