@locii/biopass-cognito-sdk
Version:
Biopass Cognito Web SDK
90 lines • 4.38 kB
JavaScript
;
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 auth_1 = require("@aws-amplify/auth");
const aws_amplify_1 = require("aws-amplify");
const amazon_cognito_identity_js_1 = require("amazon-cognito-identity-js");
const biopass_session_1 = require("./biopass-session");
const _storage = new biopass_session_1.SessionStorage();
class BioAuthClass {
static signin(username, password, clientMetadata) {
var _a;
return __awaiter(this, void 0, void 0, function* () {
try {
const user = yield aws_amplify_1.Auth.signIn(username, password, clientMetadata);
if ((user === null || user === void 0 ? void 0 : user.challengeName) === 'CUSTOM_CHALLENGE') {
_storage.setSession({
username: username,
userPoolId: user.pool.userPoolId,
clientId: user.pool.clientId,
authenticationFlowType: user.getAuthenticationFlowType(),
challengeName: user.challengeName,
challengeParam: user.challengeParam,
cognitoSession: user.Session,
signinUrl: window.location.href
});
const authorizeEndpoint = (_a = user === null || user === void 0 ? void 0 : user.challengeParam) === null || _a === void 0 ? void 0 : _a.authorizeEndpoint;
if (authorizeEndpoint) {
window.location.replace(authorizeEndpoint);
}
}
return user;
}
catch (err) {
_storage.clearSession();
throw err;
}
});
}
static resumeSignIn() {
return __awaiter(this, void 0, void 0, function* () {
const session = _storage.getSession();
const url = new URL(window.location.href);
const state = url.searchParams.get('state');
const code = url.searchParams.get('code');
let error = url.searchParams.get('error');
let errorDescription = url.searchParams.get('error_description');
try {
const res = yield aws_amplify_1.Auth.currentAuthenticatedUser();
}
catch (err) {
error = '';
errorDescription = '';
}
if (code || state) {
if (!session)
throw new Error('Biopass session not found!');
let user = new auth_1.CognitoUser({
Username: session.username,
Pool: new amazon_cognito_identity_js_1.CognitoUserPool({
UserPoolId: session.userPoolId,
ClientId: session.clientId
})
});
user.setAuthenticationFlowType(session.authenticationFlowType);
user.challengeName = session.challengeName;
user.challengeParam = session.challengeParam;
user.Session = session.cognitoSession;
if (code && state) {
const challengeAnswer = btoa(JSON.stringify({ code, state, error, errorDescription }));
user = yield aws_amplify_1.Auth.sendCustomChallengeAnswer(user, challengeAnswer);
}
_storage.clearSession();
if (error && errorDescription) {
throw errorDescription;
}
return user;
}
});
}
}
exports.default = BioAuthClass;
//# sourceMappingURL=biopass-auth.js.map