UNPKG

@spasea/uz-booking-client

Version:
120 lines (119 loc) 5.25 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const jwt_decode_1 = require("jwt-decode"); const requestable_1 = require("../lib/requestable"); const models_1 = require("../models"); class Auth extends requestable_1.default { /** * Construct auth class. * @constructor * @param {string} fcmToken * @param {string} deviceName * @param {auth} accessToken - the credentials to authenticate to UzBoojking. If auth is * not provided requests will be made unauthenticated * @param userId - user id of UZ account * @param {Language} [lang] - language * @param {string} [apiBase] - the base UzBooking API URL */ constructor(fcmToken, deviceName, accessToken, userId, lang = models_1.Language.EN, apiBase) { super(lang, undefined, apiBase, false, userId); this._accessToken = accessToken.replace('Bearer ', ''); this.fcmToken = fcmToken; this.deviceName = deviceName; } /** * Send sms * @param {string} phoneNumber * @param {Function} [callback] - callback function * @returns {Promise<void>} */ sendSms(phoneNumber, callback) { return __awaiter(this, void 0, void 0, function* () { this.phoneNumber = phoneNumber; yield this.request('POST', '/api/auth/send-sms', { phone: this.phoneNumber, }, 'json', false, callback); }); } /** * Login * @param {string} code * @param {Function} [callback] - callback function * @returns {Promise<{access_token: string, expires_in: number}>} */ login(code, callback) { var _a, _b, _c, _d, _e, _f; return __awaiter(this, void 0, void 0, function* () { const authResponse = yield this.request('POST', '/api/v2/auth/login', { code, device: { fcm_token: this.fcmToken, name: this.deviceName, }, phone: this.phoneNumber, }, 'json', false); if (authResponse) { this._accessToken = (_b = (_a = authResponse.data) === null || _a === void 0 ? void 0 : _a.token) === null || _b === void 0 ? void 0 : _b.access_token; this.userId = (_d = (_c = authResponse.data) === null || _c === void 0 ? void 0 : _c.profile) === null || _d === void 0 ? void 0 : _d.id; if (callback) { return callback(undefined, (_e = authResponse.data) === null || _e === void 0 ? void 0 : _e.token, authResponse); } return (_f = authResponse.data) === null || _f === void 0 ? void 0 : _f.token; } return callback(new Error('Failed to login')); }); } /** * Refresh token * @param {Function} [callback] - callback function * @returns {Promise<{access_token: string, expires_in: number}>} */ refresh(callback) { var _a, _b, _c, _d; return __awaiter(this, void 0, void 0, function* () { const refreshResponse = yield this.request('POST', '/api/v2/auth/refresh', { device: { fcm_token: this.fcmToken, name: this.deviceName, }, }, 'json', false, callback); if (refreshResponse) { this._accessToken = (_b = (_a = refreshResponse.data) === null || _a === void 0 ? void 0 : _a.token) === null || _b === void 0 ? void 0 : _b.access_token; if (callback) { return callback(undefined, (_c = refreshResponse === null || refreshResponse === void 0 ? void 0 : refreshResponse.data) === null || _c === void 0 ? void 0 : _c.token, refreshResponse); } return (_d = refreshResponse.data) === null || _d === void 0 ? void 0 : _d.token; } return callback(new Error('Failed to refresh token')); }); } /** * Check if access token expired * @returns {Boolean} - the promise for the http request */ isExpired() { const dateNow = new Date(); if (!this._accessToken) { return true; } const decodedToken = (0, jwt_decode_1.default)(this._accessToken); return decodedToken.exp < dateNow.getTime() / 1000; } /** * Get access token * @returns {string} */ get accessToken() { return `Bearer ${this._accessToken}`; } } exports.default = Auth;