react-oidc-context
Advanced tools
Comparing version 2.1.1 to 2.2.0
@@ -1,3 +0,1 @@ | ||
"use strict"; | ||
// src/AuthContext.ts | ||
@@ -106,3 +104,5 @@ import React from "react"; | ||
var unsupportedEnvironment = (fnName) => () => { | ||
throw new Error(`UserManager#${fnName} was called from an unsupported context. If this is a server-rendered page, defer this call with useEffect() or pass a custom UserManager implementation.`); | ||
throw new Error( | ||
`UserManager#${fnName} was called from an unsupported context. If this is a server-rendered page, defer this call with useEffect() or pass a custom UserManager implementation.` | ||
); | ||
}; | ||
@@ -119,29 +119,43 @@ var defaultUserManagerImpl = typeof window === "undefined" ? null : UserManager; | ||
implementation: UserManagerImpl = defaultUserManagerImpl, | ||
userManager: userManagerProp, | ||
...userManagerSettings | ||
} = props; | ||
const [userManager] = useState(() => UserManagerImpl ? new UserManagerImpl(userManagerSettings) : { settings: userManagerSettings }); | ||
const [userManager] = useState(() => { | ||
return userManagerProp != null ? userManagerProp : UserManagerImpl ? new UserManagerImpl(userManagerSettings) : { settings: userManagerSettings }; | ||
}); | ||
const [state, dispatch] = useReducer(reducer, initialAuthState); | ||
const userManagerContext = useMemo(() => Object.assign({ | ||
settings: userManager.settings, | ||
events: userManager.events | ||
}, Object.fromEntries(userManagerContextKeys.map((key) => { | ||
var _a, _b; | ||
return [ | ||
key, | ||
(_b = (_a = userManager[key]) == null ? void 0 : _a.bind(userManager)) != null ? _b : unsupportedEnvironment(key) | ||
]; | ||
})), Object.fromEntries(navigatorKeys.map((key) => [ | ||
key, | ||
userManager[key] ? async (...args) => { | ||
dispatch({ | ||
type: "NAVIGATOR_INIT", | ||
method: key | ||
}); | ||
try { | ||
return await userManager[key](...args); | ||
} finally { | ||
dispatch({ type: "NAVIGATOR_CLOSE" }); | ||
} | ||
} : unsupportedEnvironment(key) | ||
]))), [userManager]); | ||
const userManagerContext = useMemo( | ||
() => Object.assign( | ||
{ | ||
settings: userManager.settings, | ||
events: userManager.events | ||
}, | ||
Object.fromEntries( | ||
userManagerContextKeys.map((key) => { | ||
var _a, _b; | ||
return [ | ||
key, | ||
(_b = (_a = userManager[key]) == null ? void 0 : _a.bind(userManager)) != null ? _b : unsupportedEnvironment(key) | ||
]; | ||
}) | ||
), | ||
Object.fromEntries( | ||
navigatorKeys.map((key) => [ | ||
key, | ||
userManager[key] ? async (...args) => { | ||
dispatch({ | ||
type: "NAVIGATOR_INIT", | ||
method: key | ||
}); | ||
try { | ||
return await userManager[key](...args); | ||
} finally { | ||
dispatch({ type: "NAVIGATOR_CLOSE" }); | ||
} | ||
} : unsupportedEnvironment(key) | ||
]) | ||
) | ||
), | ||
[userManager] | ||
); | ||
const didInitialize = useRef(false); | ||
@@ -187,5 +201,14 @@ useEffect(() => { | ||
}, [userManager]); | ||
const removeUser = useCallback(userManager ? () => userManager.removeUser().then(onRemoveUser) : unsupportedEnvironment("removeUser"), [userManager, onRemoveUser]); | ||
const signoutRedirect = useCallback((args) => userManagerContext.signoutRedirect(args).then(onSignoutRedirect), [userManagerContext.signoutRedirect, onSignoutRedirect]); | ||
const signoutPopup = useCallback((args) => userManagerContext.signoutPopup(args).then(onSignoutPopup), [userManagerContext.signoutPopup, onSignoutPopup]); | ||
const removeUser = useCallback( | ||
userManager ? () => userManager.removeUser().then(onRemoveUser) : unsupportedEnvironment("removeUser"), | ||
[userManager, onRemoveUser] | ||
); | ||
const signoutRedirect = useCallback( | ||
(args) => userManagerContext.signoutRedirect(args).then(onSignoutRedirect), | ||
[userManagerContext.signoutRedirect, onSignoutRedirect] | ||
); | ||
const signoutPopup = useCallback( | ||
(args) => userManagerContext.signoutPopup(args).then(onSignoutPopup), | ||
[userManagerContext.signoutPopup, onSignoutPopup] | ||
); | ||
return /* @__PURE__ */ React2.createElement(AuthContext.Provider, { | ||
@@ -192,0 +215,0 @@ value: { |
@@ -51,3 +51,15 @@ import type { QuerySessionStatusArgs } from 'oidc-client-ts'; | ||
*/ | ||
export declare interface AuthProviderProps extends UserManagerSettings { | ||
export declare interface AuthProviderNoUserManagerProps extends AuthProviderPropsBase { | ||
userManager?: never; | ||
} | ||
/** | ||
* @public | ||
*/ | ||
export declare type AuthProviderProps = AuthProviderNoUserManagerProps | AuthProviderUserManagerProps; | ||
/** | ||
* @public | ||
*/ | ||
export declare interface AuthProviderPropsBase extends UserManagerSettings { | ||
/** | ||
@@ -105,4 +117,8 @@ * The child nodes your Provider has wrapped | ||
/** | ||
* Allow passing a custom UserManager implementation | ||
* Allow passing a custom UserManager. | ||
*/ | ||
userManager?: UserManager; | ||
/** | ||
* @deprecated Allow passing a custom UserManager implementation | ||
*/ | ||
implementation?: typeof UserManager | null; | ||
@@ -112,2 +128,11 @@ } | ||
/** | ||
* @public | ||
*/ | ||
export declare interface AuthProviderUserManagerProps extends Omit<AuthProviderPropsBase, "redirect_uri" | "client_id" | "authority"> { | ||
redirect_uri?: never; | ||
client_id?: never; | ||
authority?: never; | ||
} | ||
/** | ||
* The auth state which, when combined with the auth methods, make up the return object of the `useAuth` hook. | ||
@@ -114,0 +139,0 @@ * |
@@ -20,3 +20,6 @@ "use strict"; | ||
}; | ||
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod)); | ||
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( | ||
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, | ||
mod | ||
)); | ||
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); | ||
@@ -131,3 +134,5 @@ | ||
var unsupportedEnvironment = (fnName) => () => { | ||
throw new Error(`UserManager#${fnName} was called from an unsupported context. If this is a server-rendered page, defer this call with useEffect() or pass a custom UserManager implementation.`); | ||
throw new Error( | ||
`UserManager#${fnName} was called from an unsupported context. If this is a server-rendered page, defer this call with useEffect() or pass a custom UserManager implementation.` | ||
); | ||
}; | ||
@@ -144,29 +149,43 @@ var defaultUserManagerImpl = typeof window === "undefined" ? null : import_oidc_client_ts.UserManager; | ||
implementation: UserManagerImpl = defaultUserManagerImpl, | ||
userManager: userManagerProp, | ||
...userManagerSettings | ||
} = props; | ||
const [userManager] = (0, import_react2.useState)(() => UserManagerImpl ? new UserManagerImpl(userManagerSettings) : { settings: userManagerSettings }); | ||
const [userManager] = (0, import_react2.useState)(() => { | ||
return userManagerProp != null ? userManagerProp : UserManagerImpl ? new UserManagerImpl(userManagerSettings) : { settings: userManagerSettings }; | ||
}); | ||
const [state, dispatch] = (0, import_react2.useReducer)(reducer, initialAuthState); | ||
const userManagerContext = (0, import_react2.useMemo)(() => Object.assign({ | ||
settings: userManager.settings, | ||
events: userManager.events | ||
}, Object.fromEntries(userManagerContextKeys.map((key) => { | ||
var _a, _b; | ||
return [ | ||
key, | ||
(_b = (_a = userManager[key]) == null ? void 0 : _a.bind(userManager)) != null ? _b : unsupportedEnvironment(key) | ||
]; | ||
})), Object.fromEntries(navigatorKeys.map((key) => [ | ||
key, | ||
userManager[key] ? async (...args) => { | ||
dispatch({ | ||
type: "NAVIGATOR_INIT", | ||
method: key | ||
}); | ||
try { | ||
return await userManager[key](...args); | ||
} finally { | ||
dispatch({ type: "NAVIGATOR_CLOSE" }); | ||
} | ||
} : unsupportedEnvironment(key) | ||
]))), [userManager]); | ||
const userManagerContext = (0, import_react2.useMemo)( | ||
() => Object.assign( | ||
{ | ||
settings: userManager.settings, | ||
events: userManager.events | ||
}, | ||
Object.fromEntries( | ||
userManagerContextKeys.map((key) => { | ||
var _a, _b; | ||
return [ | ||
key, | ||
(_b = (_a = userManager[key]) == null ? void 0 : _a.bind(userManager)) != null ? _b : unsupportedEnvironment(key) | ||
]; | ||
}) | ||
), | ||
Object.fromEntries( | ||
navigatorKeys.map((key) => [ | ||
key, | ||
userManager[key] ? async (...args) => { | ||
dispatch({ | ||
type: "NAVIGATOR_INIT", | ||
method: key | ||
}); | ||
try { | ||
return await userManager[key](...args); | ||
} finally { | ||
dispatch({ type: "NAVIGATOR_CLOSE" }); | ||
} | ||
} : unsupportedEnvironment(key) | ||
]) | ||
) | ||
), | ||
[userManager] | ||
); | ||
const didInitialize = (0, import_react2.useRef)(false); | ||
@@ -212,5 +231,14 @@ (0, import_react2.useEffect)(() => { | ||
}, [userManager]); | ||
const removeUser = (0, import_react2.useCallback)(userManager ? () => userManager.removeUser().then(onRemoveUser) : unsupportedEnvironment("removeUser"), [userManager, onRemoveUser]); | ||
const signoutRedirect = (0, import_react2.useCallback)((args) => userManagerContext.signoutRedirect(args).then(onSignoutRedirect), [userManagerContext.signoutRedirect, onSignoutRedirect]); | ||
const signoutPopup = (0, import_react2.useCallback)((args) => userManagerContext.signoutPopup(args).then(onSignoutPopup), [userManagerContext.signoutPopup, onSignoutPopup]); | ||
const removeUser = (0, import_react2.useCallback)( | ||
userManager ? () => userManager.removeUser().then(onRemoveUser) : unsupportedEnvironment("removeUser"), | ||
[userManager, onRemoveUser] | ||
); | ||
const signoutRedirect = (0, import_react2.useCallback)( | ||
(args) => userManagerContext.signoutRedirect(args).then(onSignoutRedirect), | ||
[userManagerContext.signoutRedirect, onSignoutRedirect] | ||
); | ||
const signoutPopup = (0, import_react2.useCallback)( | ||
(args) => userManagerContext.signoutPopup(args).then(onSignoutPopup), | ||
[userManagerContext.signoutPopup, onSignoutPopup] | ||
); | ||
return /* @__PURE__ */ import_react2.default.createElement(AuthContext.Provider, { | ||
@@ -217,0 +245,0 @@ value: { |
{ | ||
"name": "react-oidc-context", | ||
"version": "2.1.1", | ||
"version": "2.2.0", | ||
"description": "OpenID Connect & OAuth2 authentication using react context api as state management", | ||
@@ -49,3 +49,2 @@ "repository": { | ||
"@testing-library/react": "^13.1.1", | ||
"@testing-library/react-hooks": "^7.0.2", | ||
"@types/react": "^18.0.6", | ||
@@ -56,3 +55,3 @@ "@types/react-dom": "^18.0.2", | ||
"@typescript-eslint/parser": "^5.7.0", | ||
"esbuild": "^0.14.1", | ||
"esbuild": "^0.15.6", | ||
"eslint": "^8.4.1", | ||
@@ -62,3 +61,3 @@ "eslint-plugin-testing-library": "^5.0.0", | ||
"jest": "^27.2.0", | ||
"jest-mock": "^27.4.2", | ||
"jest-mock": "^29.0.1", | ||
"lint-staged": "^13.0.0", | ||
@@ -65,0 +64,0 @@ "react": "^18.0.0", |
@@ -119,3 +119,3 @@ # react-oidc-context | ||
You **must** provide an implementation of `onSigninCallback` to `oidcConfig` to remove the payload from the URL upon successful login. Otherwise if you refresh the page and the payload is still there, `signinSilent` - which handles renewing your token - won't work. | ||
You **must** provide an implementation of `onSigninCallback` to `oidcConfig` to remove the payload from the URL upon successful login. Otherwise if you refresh the page and the payload is still there, `signinSilent` - which handles renewing your token - won't work. | ||
@@ -241,3 +241,3 @@ A working implementation is already in the code [here](https://github.com/authts/react-oidc-context/blob/f175dcba6ab09871b027d6a2f2224a17712b67c5/src/AuthProvider.tsx#L20-L30). | ||
}) | ||
}, [auth.events, auth.signinSilent]) | ||
}, [auth.events, auth.signinSilent]); | ||
@@ -250,2 +250,35 @@ return <button onClick={() => void auth.signinRedirect()}>Log in</button>; | ||
### Automatic sign-in | ||
Automatically sign-in and silently reestablish your previous session, if you close the tab and reopen the application. | ||
```jsx | ||
// src/App.jsx | ||
import React from "react"; | ||
import { useAuth, hasAuthParams } from "react-oidc-context"; | ||
function App() { | ||
const auth = useAuth(); | ||
// automatically sign-in | ||
React.useEffect(() => { | ||
if (!hasAuthParams() && | ||
!auth.isAuthenticated && !auth.activeNavigator && !auth.isLoading) { | ||
auth.signinRedirect(); | ||
} | ||
}, [auth.isAuthenticated, auth.activeNavigator, auth.isLoading, auth.signinRedirect]); | ||
if (auth.activeNavigator) { | ||
return <div>Signing you in/out...</div>; | ||
} | ||
if (!auth.isAuthenticated) { | ||
return <div>Unable to log in</div>; | ||
} | ||
return <button onClick={() => void auth.removeUser()}>Log out</button>; | ||
} | ||
export default App; | ||
``` | ||
## Contributing | ||
@@ -252,0 +285,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
73807
22
697
296