UNPKG

@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

28 lines (27 loc) 1.3 kB
import React, { useEffect, useState } from "react"; import AuthContext from "./AuthContext"; function AuthProvider(_a) { var authManager = _a.authManager, children = _a.children; // Create a wrapper object that changes on each auth state change to trigger context updates var _b = useState(function () { return ({ authManager: authManager, }); }), contextValue = _b[0], setContextValue = _b[1]; useEffect(function () { var handleAuthChange = function () { // Create a new wrapper object to trigger context updates across the app setContextValue({ authManager: authManager }); }; // Subscribe to auth events authManager.addEventListener("login", handleAuthChange); authManager.addEventListener("logout", handleAuthChange); // Validate/refresh access tokens on app load (or log out if the token is invalid) authManager.autoLogin(); // Cleanup subscriptions return function () { authManager.removeEventListener("login", handleAuthChange); authManager.removeEventListener("logout", handleAuthChange); }; }, [authManager]); return React.createElement(AuthContext.Provider, { value: contextValue }, children); } export default AuthProvider;