oidc-jwt-client
Advanced tools
Comparing version 4.0.16 to 4.0.17
@@ -6,6 +6,6 @@ 'use strict'; | ||
var reactUse = require('react-use'); | ||
var store = require('./store.js'); | ||
var OidcJwtProvider = require('./OidcJwtProvider.js'); | ||
function useAuthControls() { | ||
var authService = store.useStore(function (state) { return state.service; }); | ||
var authService = OidcJwtProvider.useOidcJwtStore(function (state) { return state.service; }); | ||
return { | ||
@@ -17,8 +17,8 @@ authorize: function (params) { return authService === null || authService === void 0 ? void 0 : authService.authorize(params); }, | ||
function useAuthInitialized() { | ||
return store.useStore(function (state) { return state.authState.isInitialized; }); | ||
return OidcJwtProvider.useOidcJwtStore(function (state) { return state.authState.isInitialized; }); | ||
} | ||
function useAuthUserInfo() { | ||
var _this = this; | ||
var authService = store.useStore(function (state) { return state.service; }); | ||
var setState = store.useStore(function (state) { return state.setState; }); | ||
var authService = OidcJwtProvider.useOidcJwtStore(function (state) { return state.service; }); | ||
var setState = OidcJwtProvider.useOidcJwtStore(function (state) { return state.setState; }); | ||
var isLoggedIn = useAuthIsLoggedIn(); | ||
@@ -39,4 +39,4 @@ return reactUse.useAsync(function () { return tslib_es6.__awaiter(_this, void 0, void 0, function () { | ||
var _this = this; | ||
var authService = store.useStore(function (state) { return state.service; }); | ||
var setState = store.useStore(function (state) { return state.setState; }); | ||
var authService = OidcJwtProvider.useOidcJwtStore(function (state) { return state.service; }); | ||
var setState = OidcJwtProvider.useOidcJwtStore(function (state) { return state.setState; }); | ||
var isLoggedIn = useAuthIsLoggedIn(); | ||
@@ -57,8 +57,8 @@ return reactUse.useAsync(function () { return tslib_es6.__awaiter(_this, void 0, void 0, function () { | ||
function useAuthIsLoggedIn() { | ||
return store.useStore(function (state) { return state.authState.isLoggedIn; }); | ||
return OidcJwtProvider.useOidcJwtStore(function (state) { return state.authState.isLoggedIn; }); | ||
} | ||
function useAuthSessionExpired() { | ||
var authService = store.useStore(function (state) { return state.service; }); | ||
var isLoggedIn = store.useStore(function (state) { return state.authState.isLoggedIn; }); | ||
var didRetryLogin = store.useStore(function (state) { return state.authState.didRetryLogin; }); | ||
var authService = OidcJwtProvider.useOidcJwtStore(function (state) { return state.service; }); | ||
var isLoggedIn = OidcJwtProvider.useOidcJwtStore(function (state) { return state.authState.isLoggedIn; }); | ||
var didRetryLogin = OidcJwtProvider.useOidcJwtStore(function (state) { return state.authState.didRetryLogin; }); | ||
var isPrevLoggedIn = reactUse.usePrevious(isLoggedIn); | ||
@@ -107,4 +107,4 @@ var _a = React.useState(false), isSessionExpired = _a[0], setSessionExpired = _a[1]; | ||
function useAuthAccessToken() { | ||
var authService = store.useStore(function (state) { return state.service; }); | ||
var setState = store.useStore(function (state) { return state.setState; }); | ||
var authService = OidcJwtProvider.useOidcJwtStore(function (state) { return state.service; }); | ||
var setState = OidcJwtProvider.useOidcJwtStore(function (state) { return state.setState; }); | ||
return React.useCallback(function () { | ||
@@ -111,0 +111,0 @@ if (!authService) { |
@@ -23,3 +23,4 @@ 'use strict'; | ||
exports.OidcJwtProvider = OidcJwtProvider.OidcJwtProvider; | ||
exports.useOidcJwtStore = OidcJwtProvider.useOidcJwtStore; | ||
exports.stripTokenFromUrl = stripTokenFromUrl.stripTokenFromUrl; | ||
//# sourceMappingURL=index.js.map |
@@ -5,2 +5,3 @@ 'use strict'; | ||
var React = require('react'); | ||
var zustand = require('zustand'); | ||
var store = require('./store.js'); | ||
@@ -14,7 +15,15 @@ require('./utils/errors.js'); | ||
var OidcJwtContext = React.createContext(undefined); | ||
function useOidcJwtStore(selector, equals) { | ||
var context = React.useContext(OidcJwtContext); | ||
if (context === undefined) { | ||
throw new Error('useOidcJwtStore must be used within a OidcJwtProvider'); | ||
} | ||
return zustand.useStore(context, selector, equals); | ||
} | ||
var OidcJwtInitializer = function (_a) { | ||
var _b = _a.shouldAttemptLogin, shouldAttemptLogin = _b === void 0 ? false : _b, _c = _a.shouldMonitorAccessTokens, shouldMonitorAccessTokens = _c === void 0 ? true : _c, children = _a.children; | ||
var authService = store.useStore(function (state) { return state.service; }); | ||
var isLoggedIn = store.useStore(function (state) { return state.authState.isLoggedIn; }); | ||
var setState = store.useStore(function (state) { return state.setState; }); | ||
var authService = useOidcJwtStore(function (state) { return state.service; }); | ||
var isLoggedIn = useOidcJwtStore(function (state) { return state.authState.isLoggedIn; }); | ||
var setState = useOidcJwtStore(function (state) { return state.setState; }); | ||
React.useEffect(function () { | ||
@@ -48,6 +57,4 @@ authService === null || authService === void 0 ? void 0 : authService.loadInitialData().then(function () { return setState(authService.state); }); | ||
var client = props.client, _a = props.removeTokenFromUrlFunction, removeTokenFromUrlFunction = _a === void 0 ? stripTokenFromUrl.removeTokenFromUrl : _a, children = props.children; | ||
var createStore = React.useCallback(function () { | ||
return store.createOidcJwtClientStore(client, removeTokenFromUrlFunction); | ||
}, [client, removeTokenFromUrlFunction]); | ||
return (React__default.default.createElement(store.Provider, { createStore: createStore }, | ||
var store$1 = React.useRef(store.createOidcJwtClientStore(client, removeTokenFromUrlFunction)).current; | ||
return (React__default.default.createElement(OidcJwtContext.Provider, { value: store$1 }, | ||
React__default.default.createElement(OidcJwtInitializer, tslib_es6.__assign({}, props), children))); | ||
@@ -57,2 +64,3 @@ }; | ||
exports.OidcJwtProvider = OidcJwtProvider; | ||
exports.useOidcJwtStore = useOidcJwtStore; | ||
//# sourceMappingURL=OidcJwtProvider.js.map |
'use strict'; | ||
var zustand = require('zustand'); | ||
var createContext = require('zustand/context'); | ||
var constants = require('./constants.js'); | ||
@@ -9,6 +8,2 @@ var storage = require('./storage.js'); | ||
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; } | ||
var createContext__default = /*#__PURE__*/_interopDefault(createContext); | ||
function createOidcJwtClientStore(client, removeTokenFromUrlFunction) { | ||
@@ -37,7 +32,4 @@ return zustand.create(function (set) { | ||
} | ||
var _a = createContext__default.default(), Provider = _a.Provider, useStore = _a.useStore; | ||
exports.Provider = Provider; | ||
exports.createOidcJwtClientStore = createOidcJwtClientStore; | ||
exports.useStore = useStore; | ||
//# sourceMappingURL=store.js.map |
import { __awaiter, __generator } from './node_modules/tslib/tslib.es6.js'; | ||
import { useState, useCallback, useEffect } from 'react'; | ||
import { useAsync, usePrevious } from 'react-use'; | ||
import { useStore } from './store.js'; | ||
import { useOidcJwtStore } from './OidcJwtProvider.js'; | ||
function useAuthControls() { | ||
var authService = useStore(function (state) { return state.service; }); | ||
var authService = useOidcJwtStore(function (state) { return state.service; }); | ||
return { | ||
@@ -14,8 +14,8 @@ authorize: function (params) { return authService === null || authService === void 0 ? void 0 : authService.authorize(params); }, | ||
function useAuthInitialized() { | ||
return useStore(function (state) { return state.authState.isInitialized; }); | ||
return useOidcJwtStore(function (state) { return state.authState.isInitialized; }); | ||
} | ||
function useAuthUserInfo() { | ||
var _this = this; | ||
var authService = useStore(function (state) { return state.service; }); | ||
var setState = useStore(function (state) { return state.setState; }); | ||
var authService = useOidcJwtStore(function (state) { return state.service; }); | ||
var setState = useOidcJwtStore(function (state) { return state.setState; }); | ||
var isLoggedIn = useAuthIsLoggedIn(); | ||
@@ -36,4 +36,4 @@ return useAsync(function () { return __awaiter(_this, void 0, void 0, function () { | ||
var _this = this; | ||
var authService = useStore(function (state) { return state.service; }); | ||
var setState = useStore(function (state) { return state.setState; }); | ||
var authService = useOidcJwtStore(function (state) { return state.service; }); | ||
var setState = useOidcJwtStore(function (state) { return state.setState; }); | ||
var isLoggedIn = useAuthIsLoggedIn(); | ||
@@ -54,8 +54,8 @@ return useAsync(function () { return __awaiter(_this, void 0, void 0, function () { | ||
function useAuthIsLoggedIn() { | ||
return useStore(function (state) { return state.authState.isLoggedIn; }); | ||
return useOidcJwtStore(function (state) { return state.authState.isLoggedIn; }); | ||
} | ||
function useAuthSessionExpired() { | ||
var authService = useStore(function (state) { return state.service; }); | ||
var isLoggedIn = useStore(function (state) { return state.authState.isLoggedIn; }); | ||
var didRetryLogin = useStore(function (state) { return state.authState.didRetryLogin; }); | ||
var authService = useOidcJwtStore(function (state) { return state.service; }); | ||
var isLoggedIn = useOidcJwtStore(function (state) { return state.authState.isLoggedIn; }); | ||
var didRetryLogin = useOidcJwtStore(function (state) { return state.authState.didRetryLogin; }); | ||
var isPrevLoggedIn = usePrevious(isLoggedIn); | ||
@@ -104,4 +104,4 @@ var _a = useState(false), isSessionExpired = _a[0], setSessionExpired = _a[1]; | ||
function useAuthAccessToken() { | ||
var authService = useStore(function (state) { return state.service; }); | ||
var setState = useStore(function (state) { return state.setState; }); | ||
var authService = useOidcJwtStore(function (state) { return state.service; }); | ||
var setState = useOidcJwtStore(function (state) { return state.setState; }); | ||
return useCallback(function () { | ||
@@ -108,0 +108,0 @@ if (!authService) { |
export { CsrfTokenMethod } from './constants.js'; | ||
export { useAuthAccessClaims, useAuthAccessToken, useAuthControls, useAuthInitialized, useAuthIsLoggedIn, useAuthSessionExpired, useAuthUserInfo } from './hooks.js'; | ||
export { OidcJwtProvider } from './OidcJwtProvider.js'; | ||
export { OidcJwtProvider, useOidcJwtStore } from './OidcJwtProvider.js'; | ||
import './utils/errors.js'; | ||
export { stripTokenFromUrl } from './utils/stripTokenFromUrl.js'; | ||
//# sourceMappingURL=index.js.map |
import { __assign } from './node_modules/tslib/tslib.es6.js'; | ||
import React, { useCallback, useEffect } from 'react'; | ||
import { createOidcJwtClientStore, Provider, useStore } from './store.js'; | ||
import React, { createContext, useContext, useRef, useEffect } from 'react'; | ||
import { useStore } from 'zustand'; | ||
import { createOidcJwtClientStore } from './store.js'; | ||
import './utils/errors.js'; | ||
import { removeTokenFromUrl } from './utils/stripTokenFromUrl.js'; | ||
var OidcJwtContext = createContext(undefined); | ||
function useOidcJwtStore(selector, equals) { | ||
var context = useContext(OidcJwtContext); | ||
if (context === undefined) { | ||
throw new Error('useOidcJwtStore must be used within a OidcJwtProvider'); | ||
} | ||
return useStore(context, selector, equals); | ||
} | ||
var OidcJwtInitializer = function (_a) { | ||
var _b = _a.shouldAttemptLogin, shouldAttemptLogin = _b === void 0 ? false : _b, _c = _a.shouldMonitorAccessTokens, shouldMonitorAccessTokens = _c === void 0 ? true : _c, children = _a.children; | ||
var authService = useStore(function (state) { return state.service; }); | ||
var isLoggedIn = useStore(function (state) { return state.authState.isLoggedIn; }); | ||
var setState = useStore(function (state) { return state.setState; }); | ||
var authService = useOidcJwtStore(function (state) { return state.service; }); | ||
var isLoggedIn = useOidcJwtStore(function (state) { return state.authState.isLoggedIn; }); | ||
var setState = useOidcJwtStore(function (state) { return state.setState; }); | ||
useEffect(function () { | ||
@@ -40,10 +49,8 @@ authService === null || authService === void 0 ? void 0 : authService.loadInitialData().then(function () { return setState(authService.state); }); | ||
var client = props.client, _a = props.removeTokenFromUrlFunction, removeTokenFromUrlFunction = _a === void 0 ? removeTokenFromUrl : _a, children = props.children; | ||
var createStore = useCallback(function () { | ||
return createOidcJwtClientStore(client, removeTokenFromUrlFunction); | ||
}, [client, removeTokenFromUrlFunction]); | ||
return (React.createElement(Provider, { createStore: createStore }, | ||
var store = useRef(createOidcJwtClientStore(client, removeTokenFromUrlFunction)).current; | ||
return (React.createElement(OidcJwtContext.Provider, { value: store }, | ||
React.createElement(OidcJwtInitializer, __assign({}, props), children))); | ||
}; | ||
export { OidcJwtProvider }; | ||
export { OidcJwtProvider, useOidcJwtStore }; | ||
//# sourceMappingURL=OidcJwtProvider.js.map |
import { create } from 'zustand'; | ||
import createContext from 'zustand/context'; | ||
import { USER_INFO_TOKEN_STORAGE_KEY, CSRF_TOKEN_STORAGE_KEY, LOGGED_IN_TOKEN_STORAGE_KEY, RETRY_LOGIN_STORAGE_KEY } from './constants.js'; | ||
@@ -30,5 +29,4 @@ import { Storage } from './storage.js'; | ||
} | ||
var _a = createContext(), Provider = _a.Provider, useStore = _a.useStore; | ||
export { Provider, createOidcJwtClientStore, useStore }; | ||
export { createOidcJwtClientStore }; | ||
//# sourceMappingURL=store.js.map |
import React from 'react'; | ||
import { OidcJwtProviderProps } from './types'; | ||
import { OidcJwtClientStore, OidcJwtProviderProps } from './types'; | ||
/** | ||
* @see https://github.com/pmndrs/zustand/blob/main/docs/guides/typescript.md#bounded-usestore-hook-for-vanilla-stores | ||
*/ | ||
declare function useOidcJwtStore(): OidcJwtClientStore; | ||
declare function useOidcJwtStore<T>(selector: (state: OidcJwtClientStore) => T, equals?: (a: T, b: T) => boolean): T; | ||
declare const OidcJwtProvider: React.FC<React.PropsWithChildren<OidcJwtProviderProps>>; | ||
export { OidcJwtProvider }; | ||
export { OidcJwtProvider, useOidcJwtStore }; |
@@ -1,12 +0,3 @@ | ||
/// <reference types="react" /> | ||
import { StoreApi } from 'zustand'; | ||
import { OidcJwtClientOptions, UseOidcJwtClientStore } from './types'; | ||
declare function createOidcJwtClientStore(client: OidcJwtClientOptions | false, removeTokenFromUrlFunction?: (url: string) => void): import("zustand").UseBoundStore<StoreApi<UseOidcJwtClientStore>>; | ||
declare const Provider: ({ createStore, children, }: { | ||
createStore: () => StoreApi<UseOidcJwtClientStore>; | ||
children: import("react").ReactNode; | ||
}) => import("react").FunctionComponentElement<import("react").ProviderProps<StoreApi<UseOidcJwtClientStore> | undefined>>, useStore: { | ||
(): UseOidcJwtClientStore; | ||
<U>(selector: (state: UseOidcJwtClientStore) => U, equalityFn?: ((a: U, b: U) => boolean) | undefined): U; | ||
}; | ||
export { createOidcJwtClientStore, Provider, useStore }; | ||
import { OidcJwtClientOptions, OidcJwtClientStore } from './types'; | ||
declare function createOidcJwtClientStore(client: OidcJwtClientOptions | false, removeTokenFromUrlFunction?: (url: string) => void): import("zustand").UseBoundStore<import("zustand").StoreApi<OidcJwtClientStore>>; | ||
export { createOidcJwtClientStore }; |
@@ -47,3 +47,3 @@ import { CsrfTokenMethod } from './constants'; | ||
} | ||
export type UseOidcJwtClientStore = { | ||
export type OidcJwtClientStore = { | ||
service: AuthService | null; | ||
@@ -50,0 +50,0 @@ authState: AuthState; |
{ | ||
"name": "oidc-jwt-client", | ||
"version": "4.0.16", | ||
"version": "4.0.17", | ||
"description": "Fetch JWTs for API access from oidc-jwt-provider", | ||
@@ -5,0 +5,0 @@ "sideEffects": false, |
# oidc-jwt-client | ||
Fetch JWTs for API access from oidc-jwt-provider | ||
## Installation | ||
`npm install oidc-jwt-client --save` | ||
## How to use | ||
```javascript | ||
@@ -23,2 +26,3 @@ <OidcJwtProvider | ||
In NextJS you could create a helper function like this: | ||
```tsx | ||
@@ -38,6 +42,7 @@ // removeTokenFromUrlFunction.ts | ||
And then use it like this: | ||
```tsx | ||
// App.tsx | ||
<OidcJwtProvider | ||
client={{ url: 'https://api-auth.acc.titan.awssdu.nl' }} | ||
client={{ url: 'https://api-auth.ota.titan2.awssdu.nl' }} | ||
shouldAttemptLogin={false} | ||
@@ -52,2 +57,3 @@ shouldMonitorAccessTokens={false} | ||
### Fetch an accessToken | ||
Within the provider we make use of several hooks to use the functionality exposed within the context. | ||
@@ -61,3 +67,3 @@ | ||
```javascript | ||
const [token, setToken] = useState<null | string>(null); | ||
const [token, setToken] = (useState < null) | (string > null); | ||
const fetchAccessToken = useAuthAccessToken(); | ||
@@ -73,2 +79,3 @@ | ||
### Login and Logout functions | ||
To login or logout a user manually you can make use of these two function exposed by the useAuthControls hook: | ||
@@ -89,2 +96,3 @@ | ||
### Check if authentication client has initialized | ||
Checks when the loadInitialData function is done executing and will return true when finished. NB! This doesn't mean you're logged in, jsut that the authentication is done initializing. | ||
@@ -94,15 +102,16 @@ | ||
const isInitialized = useAuthInitialized(); | ||
console.log('Auth is initialized: ', isInitialized) | ||
console.log('Auth is initialized: ', isInitialized); | ||
``` | ||
### Get User Info | ||
### Get User Info | ||
To get the user info you can do this within the context of the provider: | ||
```javascript | ||
const { value, loading } = useAuthUserInfo(); | ||
console.log('This is the userInfo: ', value) | ||
const { value, loading } = useAuthUserInfo(); | ||
console.log('This is the userInfo: ', value); | ||
``` | ||
### Get the Claims | ||
To get the claims you can do this within the context of the provider: | ||
@@ -112,7 +121,7 @@ | ||
const { value, loading } = useAuthAccessClaims(); | ||
console.log('These are the claims: ', value) | ||
console.log('These are the claims: ', value); | ||
``` | ||
### Check if a user is logged in. | ||
### Check if a user is logged in. | ||
Checking if the user is logged in so that you can act on it. | ||
@@ -122,6 +131,7 @@ | ||
const isLoggedIn = useAuthIsLoggedIn(); | ||
console.log('Is the user loggedin? ', isLoggedIn) | ||
console.log('Is the user loggedin? ', isLoggedIn); | ||
``` | ||
### Check if the users session has expired | ||
Checking if the users session has expired | ||
@@ -131,3 +141,3 @@ | ||
const isSessionExpired = useAuthSessionExpired(); | ||
console.log('Is the users session expired? ', isSessionExpired) | ||
console.log('Is the users session expired? ', isSessionExpired); | ||
``` |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
169359
1625
134