Skip to content

Commit 226fd19

Browse files
committed
chore(nextjs): fix signin issue
1 parent bc360c1 commit 226fd19

File tree

4 files changed

+14
-41
lines changed

4 files changed

+14
-41
lines changed

packages/nextjs/src/client/components/control/SignedIn/SignedIn.tsx

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818

1919
'use client';
2020

21-
import {FC, PropsWithChildren, ReactNode, useEffect, useState} from 'react';
22-
import isSignedIn from '../../../../server/actions/isSignedIn';
21+
import {FC, PropsWithChildren, ReactNode} from 'react';
22+
import useAsgardeo from '../../../contexts/Asgardeo/useAsgardeo';
2323

2424
/**
2525
* Props interface of {@link SignedIn}
@@ -51,23 +51,9 @@ const SignedIn: FC<PropsWithChildren<SignedInProps>> = ({
5151
children,
5252
fallback = null,
5353
}: PropsWithChildren<SignedInProps>) => {
54-
const [isSignedInSync, setIsSignedInSync] = useState<boolean | null>(null);
54+
const {isSignedIn} = useAsgardeo();
5555

56-
useEffect(() => {
57-
(async (): Promise<void> => {
58-
try {
59-
const result: boolean = await isSignedIn();
60-
61-
setIsSignedInSync(result);
62-
} catch (error) {
63-
setIsSignedInSync(false);
64-
}
65-
})();
66-
}, []);
67-
68-
if (isSignedInSync === null) return null;
69-
70-
return <>{isSignedInSync ? children : fallback}</>;
56+
return <>{isSignedIn ? children : fallback}</>;
7157
};
7258

7359
export default SignedIn;

packages/nextjs/src/client/components/control/SignedOut/SignedOut.tsx

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818

1919
'use client';
2020

21-
import {FC, PropsWithChildren, ReactNode, useEffect, useState} from 'react';
22-
import isSignedIn from '../../../../server/actions/isSignedIn';
21+
import {FC, PropsWithChildren, ReactNode} from 'react';
22+
import useAsgardeo from '../../../contexts/Asgardeo/useAsgardeo';
2323

2424
/**
2525
* Props interface of {@link SignedOut}
@@ -51,23 +51,9 @@ const SignedOut: FC<PropsWithChildren<SignedOutProps>> = ({
5151
children,
5252
fallback = null,
5353
}: PropsWithChildren<SignedOutProps>) => {
54-
const [isSignedInSync, setIsSignedInSync] = useState<boolean | null>(null);
54+
const {isSignedIn} = useAsgardeo();
5555

56-
useEffect(() => {
57-
(async (): Promise<void> => {
58-
try {
59-
const result: boolean = await isSignedIn();
60-
61-
setIsSignedInSync(result);
62-
} catch (error) {
63-
setIsSignedInSync(false);
64-
}
65-
})();
66-
}, []);
67-
68-
if (isSignedInSync === null) return null;
69-
70-
return <>{!isSignedInSync ? children : fallback}</>;
56+
return <>{!isSignedIn ? children : fallback}</>;
7157
};
7258

7359
export default SignedOut;

packages/nextjs/src/server/AsgardeoProvider.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@ const AsgardeoServerProvider: FC<PropsWithChildren<AsgardeoServerProviderProps>>
7979
return <></>;
8080
}
8181

82-
const _isSignedIn: boolean = await isSignedIn();
82+
const sessionId: string = await getSessionId() as string;
83+
const _isSignedIn: boolean = await isSignedIn(sessionId);
84+
8385
let user: User = {};
8486
let userProfile: UserProfile = {
8587
schemas: [],
@@ -88,8 +90,8 @@ const AsgardeoServerProvider: FC<PropsWithChildren<AsgardeoServerProviderProps>>
8890
};
8991

9092
if (_isSignedIn) {
91-
const userResponse = await getUserAction((await getSessionId()) as string);
92-
const userProfileResponse = await getUserProfileAction((await getSessionId()) as string);
93+
const userResponse = await getUserAction((sessionId));
94+
const userProfileResponse = await getUserProfileAction((sessionId));
9395

9496
user = userResponse.data?.user || {};
9597
userProfile = userProfileResponse.data?.userProfile;

packages/nextjs/src/server/actions/isSignedIn.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@
2121
import AsgardeoNextClient from '../../AsgardeoNextClient';
2222
import getSessionId from './getSessionId';
2323

24-
const isSignedIn = async (): Promise<boolean> => {
25-
const sessionId: string | undefined = await getSessionId();
24+
const isSignedIn = async (sessionId: string): Promise<boolean> => {
2625
const client = AsgardeoNextClient.getInstance();
2726
const accessToken: string | undefined = await client.getAccessToken(sessionId);
2827

0 commit comments

Comments
 (0)