Expo SDK35 AWS Amplify not remembering logged in user - expo

I've just updated my app from Expo 34 to 35. Since the upgrade whenever the app reloads it logs me out of my authenticated session with Amplify.
Below is my code but it was working flawlessly prior to the upgrade. The upgrade was completed in isolation so there have been no other code changes.
await Auth.currentAuthenticatedUser()
.then(user => {
this.setState({
userToken: user.signInUserSession.accessToken.jwtToken,
})
})
.catch(err => console.log('Response ', err))

I also got this issue. Updating to sdk version 36 fixed it for me.

Related

React Native - AWS amplify Auth.currentCredentials() returns null in IOS

Aws amplify Auth.currentCredentials() returns null in IOS but working when we turn debugging on. it works completely fine in android but in IOS only works if debugging is on
Auth.currentCredentials().then(info => {
cognitoIdentityId = info.identityId
console.log(cognitoIdentityId)
});

How to use Cognito as OAuth Flow without using Hosted UI?

I saw some similar questions here before but I couldn`t understand none of them. How can I get an code from OAuth 2 authentication flow without using hosted UI in Cognito ?
I saw that hosted calls a javascript function but I don`t understand how this works and return the code.
Is there any way to call an API directly passing user credentials and has this code generated ?
The documentation is pretty bad for this - but I got it to work using a customProvider as a property
Auth.federatedSignIn(
{customProvider: "YOUR_CUSTOM_PROVIDER"})
According to:
https://github.com/aws-amplify/amplify-js/blob/a047ce73/packages/auth/src/types/Auth.ts#L74
The whole function based on the AWS tutorial would then be:
Auth.federatedSignIn(
{customProvider: "YOUR_CUSTOM_PROVIDER"}
).then(cred => {
// If success, you will get the AWS credentials
console.log(cred);
this.setState({
isAuthorized:true
})
this.login()
}).then(user => {
// If success, the user object you passed in Auth.federatedSignIn
console.log(user);
}).catch(e => {
console.log(e)
});
}

AWS Cognito hosted UI and Amplify Auth with authorization code OAuth flow

I have a Vue.js webapp that I am trying to add simple authentication to using AWS Cognito and Amplify Auth. I have my user pool set up with "Authorization code grant" enabled for the OAuth flow. I also have the redirect URL set as https://example.auth.us-east-2.amazoncognito.com/login?response_type=code&client_id=XXXXXXXX&redirect_uri=https://example.com/auth/verify for the hosted UI.
This is what's within the page the hosted UI redirects to:
import { Auth } from "aws-amplify";
export default {
async created() {
try {
await Auth.currentSession();
} catch {
console.error("Not authorized");
}
}
}
When I sign in the first time through the hosted UI and am redirected, I get an error and am not recognized by Amplify as being authenticated. However if I sign in a second time, there is no error in the console and I have an authenticated session.
I do know that authorization code grant doesn't put the tokens in the URL, but I do see them in localstorage even on the first sign in. I have tried switching to using the "token" OAuth flow but the Amplify docs say the refresh token isn't provided that way and I'd like to not have sessions limited to 1 hour. Any guidance here?
For anyone facing the same problem, this seems to be a known issue.
The workaround is to subscribe to Hub actions and handle it there like
Hub.listen("auth", ({ payload: { event, data } }) => {
switch (event) {
case "signIn":
// signin actions
Auth.currentSession()
.then(user => console.log(user)) // redirect to default page
.error(err => console.log(err))
case "signOut":
// signout actions, redirect to '/' etc
case "customOAuthState":
// other changes
}
}
refer to https://github.com/aws-amplify/amplify-js/issues/5133#issuecomment-600759135

AWS Amplify Auth sign-in issues with React Native

I am experiencing an issue with the AWS Amplify Auth sign in button in React Native only (both in iOS and Android). I've tried both the withAuthenticator() implementation as well as a custom flow using Auth.signIn(). Clicking the sign in button does absolutely nothing with no error reported in the React Native debugger. The exact same configuration / user pool is used on a parallel web app and that works absolutely fine (tried using a different userPoolWebClientId just in case that was the issue). I have installed amazon-cognito-identity-js and react-native link and have reset all caches. The sign in ui renders fine, but the click action just does nothing.
import/config used:
import Amplify from 'aws-amplify';
import awsmobile from './aws-exports';
import { withAuthenticator } from 'aws-amplify-react-native';
Amplify.configure(awsmobile);
and tried manual config as well:
Amplify.configure({
Auth: {
identityPoolId: 'xx-xxxx-x:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
region: 'us-east-1',
userPoolId: 'us-east-1_xxxxxxxxx',
userPoolWebClientId: 'xxxxxxxxxxxxxxxxxxxxxxxxxx',
}
});
Using withAuthenticator:
export default withAuthenticator(App, true);
relevant packages used:
"amazon-cognito-identity-js": "^3.0.12",
"aws-amplify": "^1.1.28",
"aws-amplify-react": "^2.3.8",
"aws-amplify-react-native": "^2.1.12",
"react": "16.8.6",
"react-apollo": "^2.5.6",
"react-dom": "^16.8.6",
"react-native": "0.59.9",
Oddly enough, the Forget Password action will send out a verification email, but the ui does not respond to the button click as well.
I'm guessing this is something very minor with configuration, but unable to determine what this issue is. It is also very odd that there is not any errors reported. Any insight is appreciated.
Below is complete output from the debugger - with the last line added after the click of the sign in button:

Ember simple auth tests failing intermittently

I am struggling to debug failing tests on an Ember 3.0 app. I have a login form using ember-simple-auth. The authenticate action looks like this:
let { identification, password } = this.getProperties('identification', 'password');
this.get('session').authenticate('authenticator:devise', identification, password)
.catch((reason) => {
this.set('loginError', reason.errors || reason);
});
My test simply fills in the form and clicks a button to trigger this action, then checks a user is logged in:
invalidateSession();
await visit('/');
fillIn('#identification', 'test#email.com');
fillIn('#password', 'secret');
await click('.login-btn');
let sesh = currentSession();
let isAuthed = get(sesh, 'isAuthenticated');
assert.equal(
isAuthed,
true,
'after a user submits good creds to login form, they are logged in'
);
Using a Mirage backend, this works every time. However, using a Rails API which returns the same response, this test fails about half the time. It seems to be random whether it will pass or fail.
I'm not sure where the problem is here. The Rails app is running locally on port 3000 so it is not a network issue. I wonder if the test is timing out because the API takes longer to respond than Mirage - although this does seem unlikely as the tests run in under a second. Has anyone encountered a problem like this?
Thanks
My hunch is that you may be evaluating isAuth before ember-simple-auth has completed authenticating with the backend. For my tests that are similar to yours, I have await settled(); after await click();.
If that doesn't do the trick then you could try using waitFor or waitUntil from the ember-test-helpers to make sure that authentication has finished.