Skip to content

Commit

Permalink
Merge pull request #1338 from authts/refactor-1
Browse files Browse the repository at this point in the history
refactor
  • Loading branch information
pamapa authored Aug 21, 2024
2 parents 6e5bf64 + af6ef49 commit 01ea83d
Showing 1 changed file with 10 additions and 16 deletions.
26 changes: 10 additions & 16 deletions src/AuthProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
import React, {
useCallback,
useEffect,
useMemo,
useReducer,
useRef,
useState,
} from "react";
import React from "react";
import { UserManager, type UserManagerSettings, User } from "oidc-client-ts";
import type {
ProcessResourceOwnerPasswordCredentialsArgs,
Expand Down Expand Up @@ -174,15 +167,15 @@ export const AuthProvider = (props: AuthProviderProps): JSX.Element => {
...userManagerSettings
} = props;

const [userManager] = useState(() => {
const [userManager] = React.useState(() => {
return userManagerProp ??
(UserManagerImpl
? new UserManagerImpl(userManagerSettings as UserManagerSettings)
: ({ settings: userManagerSettings } as UserManager));
});

const [state, dispatch] = useReducer(reducer, initialAuthState);
const userManagerContext = useMemo(
const [state, dispatch] = React.useReducer(reducer, initialAuthState);
const userManagerContext = React.useMemo(
() =>
Object.assign(
{
Expand Down Expand Up @@ -220,18 +213,19 @@ export const AuthProvider = (props: AuthProviderProps): JSX.Element => {
),
[userManager],
);
const didInitialize = useRef(false);
const didInitialize = React.useRef(false);

useEffect(() => {
React.useEffect(() => {
if (!userManager || didInitialize.current) {
return;
}
didInitialize.current = true;

void (async (): Promise<void> => {
// sign-in
let user: User | void | null = null;
try {
let user: User | void | null = null;

// check if returning back from authority server
if (hasAuthParams() && !skipSigninCallback) {
user = await userManager.signinCallback();
Expand All @@ -256,7 +250,7 @@ export const AuthProvider = (props: AuthProviderProps): JSX.Element => {
}, [userManager, skipSigninCallback, onSigninCallback, onSignoutCallback, matchSignoutCallback]);

// register to userManager events
useEffect(() => {
React.useEffect(() => {
if (!userManager) return undefined;
// event UserLoaded (e.g. initial load, silent renew success)
const handleUserLoaded = (user: User) => {
Expand Down Expand Up @@ -290,7 +284,7 @@ export const AuthProvider = (props: AuthProviderProps): JSX.Element => {
};
}, [userManager]);

const removeUser = useCallback(async () => {
const removeUser = React.useCallback(async () => {
if (!userManager) unsupportedEnvironment("removeUser");
await userManager.removeUser();
onRemoveUser && await onRemoveUser();
Expand Down

0 comments on commit 01ea83d

Please sign in to comment.