@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
31 lines (30 loc) • 1.21 kB
JavaScript
import { useContext } from "react";
import AuthContext from "./AuthContext";
import AuthManagersContext from "./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 = useContext(AuthContext);
var authManagersContext = useContext(AuthManagersContext);
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;
}
export default useAuth;