amplify-js: Social Authentication not working on Ionic
The docs tell us we can use federated sign in like so:
federatedSignIn('facebook')
To create a cognito user from facebook OAuth. However on a device with Ionic, the redirection process is broken.
In the browser, the process works great, however on IOS or Android it does not.
once the authentication is done, we cannot redirect back to localhost as it won’t go back to the app.
If you set the redirect URI to the apps url schema, the redirection does work, however the authentication system does not pick this up when the app is opened from the login redirect.
I have in place a function that can detect the URL being used to open the app and it is correctly opening the app with the appended token information in the redirect URL.
However Amplify does not pick this up. I then tried implementing the HUB to detect auth changes and again this had no effect. Once the redirect away to the Amplify federated login page occurs, it effectively breaks the process.
It is possible to fix this by using the Facebook Ionic wrapper or the Google auth wrapper, however this creates a federated identity which then means you cannot read and write using the API.
Any advice on this would be great as this is a real blocker. Is there a way to take the redirect tokens and pass them to amplify manually? This would solve the problem.
The goal is to be able to create a cognito user in the Amplify pool, from the social providers.
About this issue
- Original URL
- State: open
- Created 5 years ago
- Reactions: 10
- Comments: 48 (4 by maintainers)
Hi @marcangelx
I currently use the latest version of amplify 5.3.10 and we are working without issues in production with web, android and ios. I had to do a few more tweaks to make it work, but it’s very similar to my last comment. We use Angular + Ionic + Capacitor.
Here’s how we make it work:
amplify oauth config
I hope it helps you
I’m a bit late, but it might help others. I was being redirected back to my app after trying to authenticate and when checking the logs it stopped at “set credentials from session”. I’m using capacitor in the project and in my capacitor config file I had the CapacitorHTTP plugin enabled. After disabling the plugin it resolved the issue.
Correct me if I’m wrong, but I think it has to do with CapacitorHTTP sending CORS-free requests and this isn’t accepted by AWS.
In ionic with android until a while ago I used the following approach and it worked very well:
But after update aws-amplify version from 3.0.13 to 3.0.24 this no longer works. I was checking and the problem is in Auth.ts the next code is the problem https://github.com/aws-amplify/amplify-js/blob/85cb5ca42dc2bc7171f4d535ba2fa2ee270a955f/packages/auth/src/Auth.ts#L1940
Because in the new updates, this piece of code was moved to before
So I had to go back to the previous version 😕
I think this pull request broke this functionality https://github.com/aws-amplify/amplify-js/pull/5937
Any update on this?
With AWS Amplify v6 and capacitor, Amplify Auth works for me. I’ll share my implementation in case it is helpful to some.
@capacitor/appand add a listener forappUrlOpen.window.location.replace(data.url)window.location.replace(data.url.replace('https', 'capacitor'))npx cap sync iosanytime dependencies inpackage.jsonchange, otherwise imports won’t work. Also, the domain for applinks on iOS seems to get cached, so set?mode=developerif you are making updates to the site association file so it reads the latest.I got this working with ionic angular on iOS. 4 basic tenets of this implementation:
Links I referred with slight changes to implementation as needed: https://medium.com/@tobinc/create-a-multi-platform-app-using-ionic-4-aws-amplify-in-app-oauth-with-social-idp-deep-linking-6b8de9bc6878 https://medium.com/@zippicoder/setup-aws-cognito-user-pool-with-an-azure-ad-identity-provider-to-perform-single-sign-on-sso-7ff5aa36fc2a https://arjunsk.medium.com/cognito-hosted-ui-with-amplify-in-angular-7-26c9285675c4 And bunch of open issues on this topic on github. Thanks for all contributors.
Key code snippets:
config.xml
aws-exports.js
@@giacomocarrozzo I finally got it working!!
So the reason the authentication is not registered is because the Ionic webview is not loaded with the state and code from the redirect. These need to be passed to Ionic in order for Amplify to run its magic.
In order to do so you need to add redirect URL’s top the app and then intercept these deep links. In my case im using capacitor, which handles the deep links so I can grab the data:
From this point I now have the redirect query params passed back by Facebook and google. Now I need to load the application into the webview again but this time with the query params appended to it:
document.location.href =capacitor://localhost/authevent/?code=${queryParams.code}&state=${queryParams.state};The next step is to ensure you have the amplify hub set up, as for some reason the auth state observable doesn’t notice the event even after this.
At this point I have the facebook auth up and running.
@giacomocarrozzo so I also had that. If you run it incognito mode, you wont get that issue.
It seems cognito saves session data about the federated user, so that if you are removing and adding users in development it will throw this error.
Try running in incognito the error should disappear.
Please post your solution if you figure it out @oliverandersencox . This is brick-walling my deployment, as well as everyone else’ I’m sure.