wfm-mobile-core
Version:
WFM Mobile Core
401 lines (400 loc) • 20.1 kB
JavaScript
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
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 __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());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AuthApi = void 0;
var mobx_1 = require("mobx");
var constants_1 = require("../constants");
var enums_1 = require("../enums");
var http_1 = require("../http");
var Auth_abstract_1 = require("../interfaces/Auth.abstract");
var Recognition_1 = require("./Recognition");
var AuthApi = /** @class */ (function (_super) {
__extends(AuthApi, _super);
function AuthApi() {
return _super !== null && _super.apply(this, arguments) || this;
}
AuthApi.setFetchingStatus = function (newStatus) {
AuthApi.fetchingStatus = newStatus;
};
AuthApi.setMemorized = function (status) {
this.isMemorized = status;
};
AuthApi.logout = function () {
var _a;
return __awaiter(this, void 0, void 0, function () {
var logoutHeaders, e_1;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
logoutHeaders = __assign({ 'Access-Control-Allow-Origin': (_a = http_1.httpClient.baseURL) !== null && _a !== void 0 ? _a : '*' }, constants_1.headersHAL);
if (this.getToken()) {
logoutHeaders['X-CSRF-Token'] = this.getToken();
}
_b.label = 1;
case 1:
_b.trys.push([1, 3, , 4]);
return [4 /*yield*/, http_1.httpClient.post(http_1.httpClient.entryPoint + "/logout", logoutHeaders)];
case 2:
_b.sent();
http_1.httpClient.setLinks({});
http_1.httpClient.systemProperties = [];
Recognition_1.RecognitionApi.reset();
this.headers = {};
this.isAuthenticated = false;
this.isConfirmed = false;
AuthApi.setFetchingStatus(enums_1.EFetchingStatus.DONE);
return [2 /*return*/, Promise.resolve()];
case 3:
e_1 = _b.sent();
return [2 /*return*/, Promise.reject()];
case 4: return [2 /*return*/];
}
});
});
};
AuthApi.authByCredentials = function (login, password) {
var _a, _b;
if (login === void 0) { login = ''; }
if (password === void 0) { password = ''; }
return __awaiter(this, void 0, void 0, function () {
var encoded, authData, auth, e_2, authHeaders, response, data, _1, e_3;
return __generator(this, function (_c) {
switch (_c.label) {
case 0:
AuthApi.setFetchingStatus(enums_1.EFetchingStatus.IN_PROGRESS);
encoded = '';
if (Boolean(login.length && password.length)) {
encoded = btoa(login + ":" + password);
}
if (!AuthApi.extractCredentials) return [3 /*break*/, 5];
_c.label = 1;
case 1:
_c.trys.push([1, 3, , 4]);
return [4 /*yield*/, AuthApi.extractCredentials()];
case 2:
authData = _c.sent();
if (authData && !login.length && !password.length) {
auth = JSON.parse(authData);
if (Boolean(auth.login.length && auth.password.length)) {
encoded = btoa(auth.login + ":" + auth.password);
}
}
return [3 /*break*/, 4];
case 3:
e_2 = _c.sent();
return [3 /*break*/, 4];
case 4: return [3 /*break*/, 6];
case 5:
console.warn('AuthApi.extractCredentials not defined');
_c.label = 6;
case 6:
authHeaders = __assign(__assign({}, (encoded ? { Authorization: "Basic " + encoded } : {})), constants_1.headersHAL);
_c.label = 7;
case 7:
_c.trys.push([7, 19, , 20]);
return [4 /*yield*/, http_1.httpClient.get(http_1.httpClient.entryPoint, authHeaders, false)];
case 8:
response = (_c.sent());
return [4 /*yield*/, response.json()];
case 9:
data = _c.sent();
if (!response.ok) return [3 /*break*/, 17];
// 2FA
if (Array.isArray(data) && ((_a = data) === null || _a === void 0 ? void 0 : _a.some(function (re) { return (re === null || re === void 0 ? void 0 : re.logref) === 'error'; }))) {
this.isChangePassword = Boolean((_b = response === null || response === void 0 ? void 0 : response.headers) === null || _b === void 0 ? void 0 : _b.get('X-Password-Change-Required'));
this.headers = __assign({}, constants_1.headersHAL);
this.isConfirmed = false;
this.isAuthenticated = true;
AuthApi.setFetchingStatus(enums_1.EFetchingStatus.NONE);
return [2 /*return*/, Promise.resolve(data)];
}
if (!Boolean(login.length && password.length)) return [3 /*break*/, 15];
if (!AuthApi.storeCredentials) return [3 /*break*/, 14];
_c.label = 10;
case 10:
_c.trys.push([10, 12, , 13]);
return [4 /*yield*/, AuthApi.storeCredentials(login, password)];
case 11:
_c.sent();
return [3 /*break*/, 13];
case 12:
_1 = _c.sent();
return [3 /*break*/, 13];
case 13: return [3 /*break*/, 15];
case 14:
console.warn('AuthApi.storeCredentials not defined');
_c.label = 15;
case 15:
this.headers = authHeaders;
this.isAuthenticated = true;
this.isConfirmed = true;
http_1.httpClient.setLinks(data._links);
return [4 /*yield*/, Recognition_1.RecognitionApi.init()];
case 16:
_c.sent();
AuthApi.setFetchingStatus(enums_1.EFetchingStatus.DONE);
this.redirect();
return [2 /*return*/, Promise.resolve(data)];
case 17:
AuthApi.setFetchingStatus(enums_1.EFetchingStatus.ERROR);
throw new Error("Response status: " + response.status);
case 18: return [3 /*break*/, 20];
case 19:
e_3 = _c.sent();
return [2 /*return*/, this.rejected(e_3)];
case 20: return [2 /*return*/];
}
});
});
};
AuthApi.authByToken = function (token) {
return __awaiter(this, void 0, void 0, function () {
var tokenHeaders, response, data, e_4;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
AuthApi.setFetchingStatus(enums_1.EFetchingStatus.IN_PROGRESS);
tokenHeaders = __assign({ Authorization: "Bearer " + token }, constants_1.headersHAL);
_a.label = 1;
case 1:
_a.trys.push([1, 8, , 9]);
return [4 /*yield*/, http_1.httpClient.post(http_1.httpClient.baseURL + "/jwt/login", tokenHeaders)];
case 2:
_a.sent();
return [4 /*yield*/, http_1.httpClient.get(http_1.httpClient.entryPoint, tokenHeaders, false)];
case 3:
response = (_a.sent());
return [4 /*yield*/, response.json()];
case 4:
data = _a.sent();
if (!response.ok) return [3 /*break*/, 6];
this.headers = __assign({}, constants_1.headersHAL);
this.isAuthenticated = true;
http_1.httpClient.setLinks(data === null || data === void 0 ? void 0 : data._links);
return [4 /*yield*/, Recognition_1.RecognitionApi.init()];
case 5:
_a.sent();
AuthApi.setFetchingStatus(enums_1.EFetchingStatus.DONE);
this.redirect();
return [2 /*return*/, Promise.resolve(data)];
case 6: return [2 /*return*/, Promise.reject(data)];
case 7: return [3 /*break*/, 9];
case 8:
e_4 = _a.sent();
return [2 /*return*/, this.rejected(e_4)];
case 9: return [2 /*return*/];
}
});
});
};
AuthApi.requestVerificationCode = function (method) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this.executeProfileRequest('send-verification', { method: method })];
});
});
};
AuthApi.confirmVerificationCode = function (code) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.executeProfileRequest('confirm-verification', { code: code })];
case 1:
_a.sent();
return [2 /*return*/, this.tryToInitializeResourceLinks()];
}
});
});
};
AuthApi.resetPassword = function (email) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this.executeProfileRequest('request-restore-password', { email: email })];
});
});
};
AuthApi.changePassword = function (password, confirm) {
return __awaiter(this, void 0, void 0, function () {
var response, _2;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 2, , 3]);
return [4 /*yield*/, http_1.httpClient.put(http_1.httpClient.entryPoint + "/users/change-current-password", { 'Content-Type': 'application/json' }, { password: password, confirm: confirm })];
case 1:
response = _a.sent();
return [2 /*return*/, Promise.resolve(response)];
case 2:
_2 = _a.sent();
return [2 /*return*/, Promise.reject()];
case 3: return [2 /*return*/];
}
});
});
};
AuthApi.tryToInitializeResourceLinks = function () {
var _a;
return __awaiter(this, void 0, void 0, function () {
var response, _3;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
_b.trys.push([0, 4, , 5]);
return [4 /*yield*/, http_1.httpClient.get(http_1.httpClient.entryPoint, this.isAuthenticated
? __assign(__assign({}, this.headers), constants_1.headersHAL) : constants_1.headersHAL)];
case 1:
response = (_b.sent());
if (!(response === null || response === void 0 ? void 0 : response._links)) return [3 /*break*/, 3];
http_1.httpClient.setLinks((_a = response) === null || _a === void 0 ? void 0 : _a._links);
return [4 /*yield*/, Recognition_1.RecognitionApi.init()];
case 2:
_b.sent();
this.headers = constants_1.headersHAL;
this.isAuthenticated = true;
this.isConfirmed = true;
return [2 /*return*/, Promise.resolve()];
case 3: return [2 /*return*/, Promise.reject()];
case 4:
_3 = _b.sent();
return [2 /*return*/, Promise.reject(_3)];
case 5: return [2 /*return*/];
}
});
});
};
AuthApi.executeProfileRequest = function (path, body) {
if (body === void 0) { body = {}; }
return __awaiter(this, void 0, void 0, function () {
var response, _4;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 2, , 3]);
return [4 /*yield*/, http_1.httpClient.post(http_1.httpClient.entryPoint + "/profile/" + path, { 'Content-Type': 'application/json' }, body)];
case 1:
response = _a.sent();
return [2 /*return*/, Promise.resolve(response)];
case 2:
_4 = _a.sent();
return [2 /*return*/, Promise.reject()];
case 3: return [2 /*return*/];
}
});
});
};
AuthApi.getToken = function () {
var _a, _b;
if (!document.cookie || !document.cookie.match)
return null;
var token = document.cookie.match(/XSRF-TOKEN=([\w-])+/);
if (token) {
return (_b = (_a = token === null || token === void 0 ? void 0 : token[0]) === null || _a === void 0 ? void 0 : _a.split('=')) === null || _b === void 0 ? void 0 : _b[1];
}
return null;
};
// todo: remove
AuthApi.redirect = function () {
window.history.replaceState({}, document.title, "/" + window.location.hash);
};
// todo: refactor, extend error object?
AuthApi.rejected = function (e) {
AuthApi.setFetchingStatus(enums_1.EFetchingStatus.ERROR);
this.error = e;
return Promise.reject(e);
};
AuthApi.isConfirmed = false;
AuthApi.isChangePassword = false;
AuthApi.isAuthenticated = false;
AuthApi.isMemorized = false;
AuthApi.fetchingStatus = enums_1.EFetchingStatus.NONE;
__decorate([
mobx_1.observable
], AuthApi, "headers", void 0);
__decorate([
mobx_1.observable
], AuthApi, "isConfirmed", void 0);
__decorate([
mobx_1.observable
], AuthApi, "isChangePassword", void 0);
__decorate([
mobx_1.observable
], AuthApi, "isAuthenticated", void 0);
__decorate([
mobx_1.observable
], AuthApi, "isMemorized", void 0);
__decorate([
mobx_1.observable
], AuthApi, "error", void 0);
__decorate([
mobx_1.observable
], AuthApi, "fetchingStatus", void 0);
return AuthApi;
}(Auth_abstract_1.default));
exports.AuthApi = AuthApi;