Skip to content

Commit

Permalink
fix (beta)
Browse files Browse the repository at this point in the history
  • Loading branch information
guillaume-chervet committed Mar 23, 2024
1 parent 303df2a commit 3fcbabf
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 9 deletions.
1 change: 1 addition & 0 deletions packages/oidc-client/src/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ export const eventNames = {
syncTokensAsync_lock_not_available: 'syncTokensAsync_lock_not_available',
syncTokensAsync_end: 'syncTokensAsync_end',
syncTokensAsync_error: 'syncTokensAsync_error',
tokensInvalidButWaitingActionsToRefresh: 'tokensInvalidButWaitingActionsToRefresh',
};
5 changes: 4 additions & 1 deletion packages/oidc-client/src/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {Fetch} from "./types";
import {OidcClient} from "./oidcClient";
import {getValidTokenAsync} from "./parseTokens";

// @ts-ignore
export const fetchWithTokens = (fetch: Fetch, oidcClient: Oidc | null) : Fetch => async (...params: Parameters<Fetch>) :Promise<Response> => {
Expand All @@ -14,7 +15,9 @@ export const fetchWithTokens = (fetch: Fetch, oidcClient: Oidc | null) : Fetch =
const oidc = oidcClient;

// @ts-ignore
const getValidToken = await oidc.getValidTokenAsync();
console.log('before', getValidTokenAsync);
const getValidToken = await getValidTokenAsync(oidc);
console.log('getValidToken', getValidToken);
const accessToken = getValidToken?.tokens?.accessToken;

if (!headers.has('Accept')) {
Expand Down
2 changes: 1 addition & 1 deletion packages/oidc-client/src/parseTokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,8 @@ export const getValidTokenAsync = async (oidc: OidcToken, waitMs = 200, numberWa
await oidc.renewTokensAsync({});
} else {
await sleepAsync({milliseconds: waitMs});
numberWaitTemp = numberWaitTemp - 1;
}
numberWaitTemp = numberWaitTemp - 1;
}
const isValid = isTokensValid(oidc.tokens);
return {
Expand Down
2 changes: 2 additions & 0 deletions packages/oidc-client/src/renewTokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ const synchroniseTokensAsync = (oidc:Oidc) => async (index = 0, forceRefresh = f
case synchroniseTokensStatus.REQUIRE_SYNC_TOKENS:

if(configuration.token_automatic_renew_mode == TokenAutomaticRenewMode.AutomaticOnlyWhenFetchExecuted && synchroniseTokensStatus.FORCE_REFRESH !== status ){
oidc.publishEvent(eventNames.tokensInvalidButWaitingActionsToRefresh, {});
return { tokens: oidc.tokens, status: 'GIVE_UP' };
}

Expand All @@ -249,6 +250,7 @@ const synchroniseTokensAsync = (oidc:Oidc) => async (index = 0, forceRefresh = f
default: {

if(configuration.token_automatic_renew_mode == TokenAutomaticRenewMode.AutomaticOnlyWhenFetchExecuted && synchroniseTokensStatus.FORCE_REFRESH !== status ){
oidc.publishEvent(eventNames.tokensInvalidButWaitingActionsToRefresh, {});
return { tokens: oidc.tokens, status: 'GIVE_UP' };
}

Expand Down
9 changes: 4 additions & 5 deletions packages/oidc-client/src/user.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { sleepAsync } from './initWorker.js';
import { isTokensValid } from './parseTokens.js';
import Oidc from "./oidc";
import {fetchWithTokens} from "./fetch";

export const userInfoAsync = (oidc:Oidc) => async (noCache = false) => {
console.log("oidc.userInfo", oidc.userInfo);
if (oidc.userInfo != null && !noCache) {
return oidc.userInfo;
}
Expand All @@ -12,11 +11,11 @@ export const userInfoAsync = (oidc:Oidc) => async (noCache = false) => {
const url = oidcServerConfiguration.userInfoEndpoint;
const fetchUserInfo = async () => {
const oidcFetch = fetchWithTokens(fetch, oidc);
const res = await oidcFetch(url);
if (res.status !== 200) {
const response = await oidcFetch(url);
if (response.status !== 200) {
return null;
}
return res.json();
return response.json();
};
const userInfo = await fetchUserInfo();
oidc.userInfo = userInfo;
Expand Down
3 changes: 1 addition & 2 deletions packages/react-oidc/src/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ export type OidcUser<T extends OidcUserInfo = OidcUserInfo> = {
export const useOidcUser = <T extends OidcUserInfo = OidcUserInfo>(configurationName = 'default') => {
const [oidcUser, setOidcUser] = useState<OidcUser<T>>({ user: null, status: OidcUserStatus.Unauthenticated });
const [oidcUserId, setOidcUserId] = useState<string>('');



useEffect(() => {
const oidc = OidcClient.get(configurationName);
let isMounted = true;
Expand Down

0 comments on commit 3fcbabf

Please sign in to comment.