UNPKG

will-lib

Version:
83 lines (82 loc) 3.3 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.WowAuthentication = void 0; const axios_1 = __importDefault(require("axios")); const AuthenError_1 = require("../AuthenError"); const HTTP_1 = require("../HTTP"); const EnvironmentVariable_1 = require("../EnvironmentVariable"); const BaseAuthentication_1 = require("./BaseAuthentication"); class WowAuthentication extends BaseAuthentication_1.BaseAuthentication { constructor(url) { super(); this.url = EnvironmentVariable_1.WOW_AUTHEN_URL; this.url = url; } static getInstance() { return new WowAuthentication(EnvironmentVariable_1.WOW_AUTHEN_URL); } getRequestUserInfo() { return { platform: "mobile", platformInfo: { os: "iOS", osVersion: "10", appVersion: "0.0.1" }, languageId: "en", countryId: "UK", timezoneId: "Asia/Bangkok", companyCode: "FWG", username: "", password: "" }; } getHeaderConfig() { return { headers: { 'Content-Type': 'application/json', 'api-key': EnvironmentVariable_1.WOW_AUTHEN_API_KEY } }; } async login(user, pwd, site) { let data = this.getRequestUserInfo(); data.username = user; data.password = pwd; if (site && site.trim().length > 0) data.companyCode = site; let config = this.getHeaderConfig(); let resinfo = this.getResponseInfo(); let result = await axios_1.default.post(this.url, data, config).then((res) => { console.log("response data", JSON.stringify(res.data)); if (res.data.head) { if (res.data.head.status != "success") { let errmsg = res.data.head.statusmessage ? res.data.head.statusmessage : "Authen fail"; return Promise.reject(new AuthenError_1.AuthenError(errmsg, HTTP_1.HTTP.UNAUTHORIZED)); } if (res.data.body) { let auth_token = { accessToken: res.data.body.accessToken, appServerUrl: res.data.body.appServerUrl, }; let auth_token_text = encodeURIComponent(JSON.stringify(auth_token)); return { ...resinfo, accessToken: res.data.body.accessToken, appServerUrl: res.data.body.appServerUrl, auth_token: auth_token_text, userProfile: res.data.body.userProfile }; } } return Promise.reject(new AuthenError_1.AuthenError("Authen fail", HTTP_1.HTTP.UNAUTHORIZED)); }).catch(function (error) { return Promise.reject(error); }); return Promise.resolve(result); } } exports.WowAuthentication = WowAuthentication;