UNPKG

react-native-msal-plugin

Version:

React Native Plugin that wraps Microsoft MSAL library allowing you to authenticate with azure B2C

60 lines (59 loc) 3.51 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { 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) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const react_native_1 = require("react-native"); const MsalUIBehavior_1 = require("./MsalUIBehavior"); const { RNMsalPlugin } = react_native_1.NativeModules; const RESET_PASSWORD_CODE = "AADB2C90118"; const delay = (milliseconds) => new Promise((resolve) => setTimeout(resolve, milliseconds)); class MsalPlugin { constructor(authority, clientId, policies) { this.defaultPolicy = { signUpSignInPolicy: "", }; this.acquireTokenAsync = (scopes, extraQueryParameters = {}, loginHint = "", msalUIBehavior = MsalUIBehavior_1.MsalUIBehavior.SELECT_ACCOUNT, extraScopesToConsent = []) => { return RNMsalPlugin.acquireTokenAsync(this.getAuthority(this.policies.signUpSignInPolicy), this.clientId, scopes, extraQueryParameters, loginHint, msalUIBehavior, extraScopesToConsent).catch((error) => { if (error.message.includes(RESET_PASSWORD_CODE) && this.policies.passwordResetPolicy) { return this.resetPasswordAsync(scopes, extraQueryParameters, loginHint, msalUIBehavior, extraScopesToConsent); } else { throw error; } }); }; this.acquireTokenSilentAsync = (scopes, userIdentifier, forceRefresh = false) => { return RNMsalPlugin.acquireTokenSilentAsync(this.getAuthority(this.policies.signUpSignInPolicy), this.clientId, scopes, userIdentifier, forceRefresh); }; this.tokenCacheDelete = () => { return RNMsalPlugin.tokenCacheDelete(this.clientId); }; this.resetPasswordAsync = (scopes, extraQueryParameters = {}, loginHint = "", msalUIBehavior = MsalUIBehavior_1.MsalUIBehavior.SELECT_ACCOUNT, extraScopesToConsent = []) => __awaiter(this, void 0, void 0, function* () { // had to use a delay otherwise exception is thrown, only one interactive session allowed // if anyone knows a better way feel free to fix return delay(1000).then(() => __awaiter(this, void 0, void 0, function* () { try { yield RNMsalPlugin.acquireTokenAsync(this.getAuthority(this.policies.passwordResetPolicy), this.clientId, scopes, extraQueryParameters, loginHint, msalUIBehavior, extraScopesToConsent); return yield this.acquireTokenAsync(scopes, extraQueryParameters, loginHint, msalUIBehavior, extraScopesToConsent); } catch (error) { throw error; } })); }); this.authority = authority; this.clientId = clientId; this.policies = policies || this.defaultPolicy; } getAuthority(policy) { return policy ? this.authority + "/" + policy : this.authority; } } exports.default = MsalPlugin;