UNPKG

waterbase

Version:
84 lines (83 loc) 3.47 kB
"use strict"; var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, privateMap, value) { if (!privateMap.has(receiver)) { throw new TypeError("attempted to set private field on non-instance"); } privateMap.set(receiver, value); return value; }; var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, privateMap) { if (!privateMap.has(receiver)) { throw new TypeError("attempted to get private field on non-instance"); } return privateMap.get(receiver); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; var _token, _refreshToken, _client; const jsonwebtoken_1 = __importDefault(require("jsonwebtoken")); const node_localstorage_1 = require("node-localstorage"); if (typeof global.localStorage === 'undefined' || global.localStorage === null) { global.localStorage = new node_localstorage_1.LocalStorage('./localStorage'); } class User { constructor(client, token) { this.email = ''; this.id = ''; this.profile = {}; _token.set(this, ''); _refreshToken.set(this, ''); _client.set(this, void 0); /** * * Refresh the jwt token * * @return Promise<any> */ this.renewToken = () => { return new Promise((res, rej) => { __classPrivateFieldGet(this, _client).call('post', '/auth/token', { 'content-type': 'application/json' }, { token: __classPrivateFieldGet(this, _refreshToken), email: this.email, }) .then((data) => { __classPrivateFieldSet(this, _token, data.token); localStorage.setItem('authToken', __classPrivateFieldGet(this, _token)); const decode = jsonwebtoken_1.default.decode(__classPrivateFieldGet(this, _token)); this.email = decode.email; this.profile = decode.profile; this.id = decode.id; __classPrivateFieldSet(this, _refreshToken, decode.refreshToken); res(data); }) .catch(rej); }); }; /** * * Will delete the account from waterbase * * @return Promise<any> */ this.deleteAccount = () => { return new Promise((res, rej) => { __classPrivateFieldGet(this, _client).call('delete', `/auth/token/${this.id}`, { 'content-type': 'application/json' }) .then((data) => { localStorage.removeItem('authToken'); res(data); }) .catch(rej); }); }; __classPrivateFieldSet(this, _client, client); __classPrivateFieldSet(this, _token, token); const decode = jsonwebtoken_1.default.decode(__classPrivateFieldGet(this, _token)); this.email = decode.email; this.profile = decode.profile; this.id = decode.id; __classPrivateFieldSet(this, _refreshToken, decode.refreshToken); } } _token = new WeakMap(), _refreshToken = new WeakMap(), _client = new WeakMap(); module.exports = User;