@shane32/msoauth
Version:
A React library for Azure AD authentication with PKCE (Proof Key for Code Exchange) flow support. This library provides a secure and easy-to-use solution for implementing Azure AD authentication in React applications, with support for both API and Microso
36 lines (35 loc) • 1.52 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var react_1 = require("react");
var AuthContext_1 = __importDefault(require("./AuthContext"));
var AuthManagersContext_1 = __importDefault(require("./AuthManagersContext"));
/**
* Hook to access the auth manager
* @param {string} [providerId] - Optional provider ID to get a specific auth manager
* @returns {AuthManager} The auth manager
* @throws {Error} If used outside of an AuthProvider or if the specified provider is not found
*/
function useAuth(providerId) {
var authContext = (0, react_1.useContext)(AuthContext_1.default);
var authManagersContext = (0, react_1.useContext)(AuthManagersContext_1.default);
if (!authContext) {
throw new Error("useAuth must be used within an AuthProvider");
}
// If no provider ID is specified, return the active provider from AuthContext
if (!providerId) {
return authContext.authManager;
}
// If a provider ID is specified, get it from the AuthManagersContext
if (!authManagersContext) {
throw new Error("useAuth with providerId must be used within a MultiAuthProvider");
}
var provider = authManagersContext.get(providerId);
if (!provider) {
throw new Error("Provider with ID \"".concat(providerId, "\" not found"));
}
return provider;
}
exports.default = useAuth;