Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistent behavior when session expires (refresh token dead) #3943

Open
dcristolovean opened this issue Dec 26, 2024 · 2 comments
Open

Inconsistent behavior when session expires (refresh token dead) #3943

dcristolovean opened this issue Dec 26, 2024 · 2 comments
Assignees
Labels
auth Issues related to the Auth category question General question

Comments

@dcristolovean
Copy link

dcristolovean commented Dec 26, 2024

Describe the bug

I'm transitioning from AWSMobileClient to Amplify and it's a huge endeavor. I'm a bit stuck with handling session expiration cases and it might be a bug (or intended behavior, but then see the questions).
My app is configured to use auth user AND non-auth users.

So, let's assume in my Amplify.Hub.listen (...) I receive the sessionExpired event.
This leaves me in a totally inconsistent state, where, I would like to revert to a non-auth user and continue using the app. Or display something, doesn't matter.

But when I get the sessionExpired event, nothing works anymore: fetchAuthSession is dead, getIdentityId is dead, API calls are dead.
And the reason for that is that the fetchAuthSession returns isSignedIn = true and still has a user stalled somewhere.

The only way I could transition back to a non auth user is to call Amplify.Auth.signOut() and actually get rid of my bad user. After that, everything works again, I am a non-auth user.

This approach has one big problem: the user plays in the app and suddenly he gets the system dialog to sign in. We all know the issue with that text in the system dialog. It's really bad to just show this dialog out of the blue. And they might press CANCEL.

So... how can I get rid of the logged in user with the expired session ? Silently, not showing anything to the user.

PS: (Rant) The documentation is, again, completely useless, doesn't explain anything, doesn't give any actual real world examples. I'm using AWS for 10 years now and this was always the weak link. I expected Amplify to finally fix this issue, but unfortunately the documentation is just as useless as before.

PS2: Interestingly enough, on Android, according to my colleague, when he gets a sessionExpired in the Hub, calling fetchAuthSession magically fixes everything, session doesn't show signed in anymore and no user left to sign out and he's a non-auth user with a new identityId.

Steps To Reproduce

Kinda hard to provide some steps, it's a more generic theoretical question

Expected behavior

  1. Either clean up the user when sessionExpires automatically and make sure we have an non-auth user working.
  2. Provide another signOut() way without web. I use SSO and I need the cookies. This user is actually local, the session is dead anyway, so not really sure what a webpage is supposed to do with it and why it's needed to log out in this case.

Amplify Framework Version

2.45.2

Amplify Categories

Auth

Dependency manager

Swift PM

Platforms

iOS

@github-actions github-actions bot added pending-triage Issue is pending triage pending-maintainer-response Issue is pending response from an Amplify team member labels Dec 26, 2024
@edisooon edisooon added question General question auth Issues related to the Auth category and removed pending-triage Issue is pending triage labels Dec 26, 2024
@edisooon
Copy link
Member

Hi @dcristolovean , thank you for submitting this issue. One of our team members will take a look into this matter and provide with an answer here.

@github-actions github-actions bot removed the pending-maintainer-response Issue is pending response from an Amplify team member label Dec 26, 2024
@dcristolovean
Copy link
Author

dcristolovean commented Dec 26, 2024

I discovered also an interesting behavior. So the session is dead and I call signOut(). I press CANCEL on the system dialog but the user is logged out and session can be fixed now, with fetch.

Which leads me to believe that signOut() has 2 parts: one on API side, web stuff, it works if there's a session valid, otherwise .... and one local part that cleans up the local user. The thing is that if the 1st part fails (or i press cancel), you still clean up local stuff and everything will work from now on.

At least there's that, but if I'm right, then you really should have a signOut() method that actually detects the session is dead so there's no point for any web stuff or any system dialog to be displayed and only do the local part.

And another thing. If the session expires when the app is opened, you have to handle the error from the API, which is nowhere to be found in the doc. Putting some prints when you detect an API error is not a documentation.
After digging around, I hope this is it:

if let apiError = error as? APIError {
switch apiError {
.......
case .operationError(let errorMessage, let recoverySuggestion, let underlyingError):

}
}

@github-actions github-actions bot added the pending-maintainer-response Issue is pending response from an Amplify team member label Dec 26, 2024
@ruisebas ruisebas removed the pending-maintainer-response Issue is pending response from an Amplify team member label Dec 27, 2024
@harsh62 harsh62 self-assigned this Dec 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auth Issues related to the Auth category question General question
Projects
None yet
Development

No branches or pull requests

4 participants