@@ -119,10 +119,22 @@ const AsgardeoClientProvider: FC<PropsWithChildren<AsgardeoClientProviderProps>>
119
119
120
120
// Handle OAuth callback automatically
121
121
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
+
122
134
// Don't handle callback if already signed in
123
135
if ( isSignedIn ) return ;
124
136
125
- const processOAuthCallback = async ( ) => {
137
+ ( async ( ) => {
126
138
try {
127
139
const code = searchParams . get ( 'code' ) ;
128
140
const state = searchParams . get ( 'state' ) ;
@@ -132,13 +144,8 @@ const AsgardeoClientProvider: FC<PropsWithChildren<AsgardeoClientProviderProps>>
132
144
133
145
// Check for OAuth errors first
134
146
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
+
142
149
return ;
143
150
}
144
151
@@ -157,21 +164,16 @@ const AsgardeoClientProvider: FC<PropsWithChildren<AsgardeoClientProviderProps>>
157
164
window . location . reload ( ) ;
158
165
}
159
166
} 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' } ` ,
164
169
) ;
165
170
}
166
171
}
167
172
} 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 ) ;
170
174
}
171
- } ;
172
-
173
- processOAuthCallback ( ) ;
174
- } , [ searchParams , router , isSignedIn , handleOAuthCallback ] ) ;
175
+ } ) ( ) ;
176
+ } , [ ] ) ;
175
177
176
178
useEffect ( ( ) => {
177
179
if ( ! preferences ?. theme ?. mode || preferences . theme . mode === 'system' ) {
0 commit comments