I am facing problem with integrating social logins into ReactNative with Expo app with Cognito.
I will provide sample code which will help understand the problem, complete code is hard to provide.
I have button on click of which I call Amplify Auth loginas below
const onPress = () => {
Auth.federatedSignIn({ provider: providers[props.provider] });
};
Now, to make in app browser available I am injecting urlOpener using expo's WebBrowser
Here is the code how that urlOpener code looks like
export async function urlOpener(url: string, redirectUrl: string) {
if (url.includes('/oauth2/authorize')) {
const oauthSession = await WebBrowser.openAuthSessionAsync(
url,
redirectUrl
);
console.log('session', oauthSession);
if (oauthSession.type === 'success') {
if (Platform.OS === 'ios') {
console.log('session url', oauthSession.url);
// // WebBrowser.dismissBrowser();
return Linking.openURL(oauthSession.url);
}
}
}
}
I have set up Hub event listner which Amplify provides like below to get the user and set it in local scope.
useEffect(() => {
const listener = Hub.listen('auth', ({ payload: { event, data } }) => {
console.log('event', event);
switch (event) {
case 'signIn':
case 'cognitoHostedUI':
console.log('data', data);
currentAuthenticatedUser().then(user => {
if (user) {
setCognitoUser(user);
}
});
break;
case 'signIn_failure':
case 'cognitoHostedUI_failure':
case 'signOut':
setCognitoUser(null);
console.log('error', data);
break;
default:
console.log('unhandled event', event);
}
});
currentAuthenticatedUser().then(user => {
if (user) {
setCognitoUser(user);
}
});
return () => listener;
}, []);
Now, that useEffect is on root app component, also dyanmic linking is also working fine.
Problem - When user uses first time social login it opens in app browser gets login, I even get the log success from url opener for oauthSession but it just reloads the complete app instead of logging in user and opening the dynamic linked screen route.
After this any time user uses the social login it works correctly and its sets the user and redirects to the correct screen/route.
The issue only first time after fresh install that means when we dont have any active local data.
Not sure what's wrong is going on here. Any help or pointers would be really helpful.
In case of confusion or clearification please free to comment.