UNPKG

@nxtoai/jwtette

Version:

JWT authentication package for NxtoAI microservices

54 lines 2.63 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.JwtHelper = void 0; const common_1 = require("@nestjs/common"); const gati_1 = require("@nxtoai/gati"); let JwtHelper = class JwtHelper { constructor(gati) { this.gati = gati; this.TOKEN_PREFIX = 'token:'; this.USER_TOKEN_PREFIX = 'user_token:'; this.TOKEN_EXPIRY = 24 * 60 * 60; } async cacheToken(token, userid) { await this.gati.put(`${this.TOKEN_PREFIX}${token}`, { value: userid }, { ttl: this.TOKEN_EXPIRY }); await this.gati.put(`${this.USER_TOKEN_PREFIX}${userid}`, { value: token }, { ttl: this.TOKEN_EXPIRY }); } async getTokenUser(token) { const cachedUser = await this.gati.get(`${this.TOKEN_PREFIX}${token}`); return cachedUser?.value && typeof cachedUser.value === 'string' ? cachedUser.value : null; } async getUserToken(userid) { const cachedToken = await this.gati.get(`${this.USER_TOKEN_PREFIX}${userid}`); return cachedToken?.value && typeof cachedToken.value === 'string' ? cachedToken.value : null; } async invalidateToken(token) { const userid = await this.getTokenUser(token); if (userid) { await this.gati.remove(`${this.USER_TOKEN_PREFIX}${userid}`); } await this.gati.remove(`${this.TOKEN_PREFIX}${token}`); } async invalidateUserToken(userid) { const token = await this.getUserToken(userid); if (token) { await this.gati.remove(`${this.TOKEN_PREFIX}${token}`); } await this.gati.remove(`${this.USER_TOKEN_PREFIX}${userid}`); } }; exports.JwtHelper = JwtHelper; exports.JwtHelper = JwtHelper = __decorate([ (0, common_1.Injectable)(), __metadata("design:paramtypes", [gati_1.GatiService]) ], JwtHelper); //# sourceMappingURL=jwt.helper%20copy.js.map