whodis-react
Version:
React hooks and components for secure, best practices authentication in seconds
79 lines • 4.02 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 });
exports.useConfirmationCodeChallenge = exports.ContactMethodType = exports.ChallengeGoal = void 0;
const react_1 = require("react");
const whodis_client_1 = require("whodis-client");
const useAuthenticationConfig_1 = require("./useAuthenticationConfig");
// and re-export these two types, since they're used as inputs to a function we expose
var whodis_client_2 = require("whodis-client");
Object.defineProperty(exports, "ChallengeGoal", { enumerable: true, get: function () { return whodis_client_2.ChallengeGoal; } });
Object.defineProperty(exports, "ContactMethodType", { enumerable: true, get: function () { return whodis_client_2.ContactMethodType; } });
/**
* hook which exposes using confirmation code challenge to authenticate
* - supports login and signup
* - supports email and email
*/
const useConfirmationCodeChallenge = ({ storage, }) => {
// expose the client and directory uuid
const { directoryUuid, clientUuid, override } = (0, useAuthenticationConfig_1.useAuthenticationConfig)();
// store the challenge uuid
const [challengeUuid, setChallengeUuid] = (0, react_1.useState)(null);
// store whether the challenge was answered already
const [challengeAnswered, setChallengeAnswered] = (0, react_1.useState)(false);
// define how to ask the confirmation code challenge
const askConfirmationCodeChallenge = (0, react_1.useCallback)(({ goal, contactMethod, }) => __awaiter(void 0, void 0, void 0, function* () {
// make the request
const { challengeUuid: newChallengeUuid } = yield (0, whodis_client_1.askAuthChallenge)({
directoryUuid,
clientUuid,
goal,
type: whodis_client_1.ChallengeType.CONFIRMATION_CODE,
details: { contactMethod },
override,
});
// now that we have the challenge, save it
setChallengeUuid(newChallengeUuid);
setChallengeAnswered(false);
}), [directoryUuid, clientUuid]);
/**
* answer the confirmation code challenge
*
* throws error if:
* - answer was incorrect
* - challengeUuid not defined first
*/
const answerConfirmationCodeChallenge = (0, react_1.useCallback)(({ answer }) => __awaiter(void 0, void 0, void 0, function* () {
// check that challenge is defined
if (!challengeUuid)
throw new Error('challenge must be asked for before it can be answered');
// send the answer
const { token } = yield (0, whodis_client_1.answerAuthChallenge)({
challengeUuid,
challengeAnswer: answer,
override,
});
// save the token if one was given for the challenge
if (token)
yield storage.set(token);
// track that the challenge was answered
setChallengeAnswered(true);
}), [challengeUuid]);
// return how to ask, answer, and whether it has been asked
return {
challengeHasBeenAsked: typeof challengeUuid === 'string',
challengeHasBeenAnswered: challengeAnswered,
askConfirmationCodeChallenge,
answerConfirmationCodeChallenge,
};
};
exports.useConfirmationCodeChallenge = useConfirmationCodeChallenge;
//# sourceMappingURL=useConfirmationCodeChallenge.js.map