@@ -119,10 +119,22 @@ const AsgardeoClientProvider: FC<PropsWithChildren<AsgardeoClientProviderProps>>
119119
120120 // Handle OAuth callback automatically
121121 useEffect ( ( ) => {
122+ // React 18.x Strict.Mode has a new check for `Ensuring reusable state` to facilitate an upcoming react feature.
123+ // https://reactjs.org/docs/strict-mode.html#ensuring-reusable-state
124+ // This will remount all the useEffects to ensure that there are no unexpected side effects.
125+ // When react remounts the signIn hook of the AuthProvider, it will cause a race condition. Hence, we have to
126+ // prevent the re-render of this hook as suggested in the following discussion.
127+ // https://github.com/reactwg/react-18/discussions/18#discussioncomment-795623
128+ if ( reRenderCheckRef . current ) {
129+ return ;
130+ }
131+
132+ reRenderCheckRef . current = true ;
133+
122134 // Don't handle callback if already signed in
123135 if ( isSignedIn ) return ;
124136
125- const processOAuthCallback = async ( ) => {
137+ ( async ( ) => {
126138 try {
127139 const code = searchParams . get ( 'code' ) ;
128140 const state = searchParams . get ( 'state' ) ;
@@ -132,13 +144,8 @@ const AsgardeoClientProvider: FC<PropsWithChildren<AsgardeoClientProviderProps>>
132144
133145 // Check for OAuth errors first
134146 if ( error ) {
135- console . error ( '[AsgardeoClientProvider] OAuth error:' , error , errorDescription ) ;
136- // Redirect to sign-in page with error
137- router . push (
138- `/signin?error=${ encodeURIComponent ( error ) } &error_description=${ encodeURIComponent (
139- errorDescription || '' ,
140- ) } `,
141- ) ;
147+ logger . error ( '[AsgardeoClientProvider] An error was received for the initiated sign-in request.' ) ;
148+
142149 return ;
143150 }
144151
@@ -157,21 +164,16 @@ const AsgardeoClientProvider: FC<PropsWithChildren<AsgardeoClientProviderProps>>
157164 window . location . reload ( ) ;
158165 }
159166 } else {
160- router . push (
161- `/signin?error=authentication_failed&error_description=${ encodeURIComponent (
162- result . error || 'Authentication failed' ,
163- ) } `,
167+ logger . error (
168+ `[AsgardeoClientProvider] An error occurred while signing in: ${ result . error || 'Authentication failed' } ` ,
164169 ) ;
165170 }
166171 }
167172 } catch ( error ) {
168- console . error ( '[AsgardeoClientProvider] Failed to handle OAuth callback:' , error ) ;
169- router . push ( '/signin?error=authentication_failed' ) ;
173+ logger . error ( '[AsgardeoClientProvider] Failed to handle OAuth callback:' , error ) ;
170174 }
171- } ;
172-
173- processOAuthCallback ( ) ;
174- } , [ searchParams , router , isSignedIn , handleOAuthCallback ] ) ;
175+ } ) ( ) ;
176+ } , [ ] ) ;
175177
176178 useEffect ( ( ) => {
177179 if ( ! preferences ?. theme ?. mode || preferences . theme . mode === 'system' ) {
0 commit comments