Skip to content

Users are randomly getting logged out #3972

Open
@afriedmanGlacier

Description

@afriedmanGlacier

Describe the bug

We believe this is similar to #3969, several times lately when opening the app, users are coming to a login screen because the fetchAuthSession throws an error or is not signed in. We've seen this several times in the past two weeks (reported by production users), but we have not been able to reproduce it in testing.

Our refresh token is set to 3 years, and each of these users logged in within the last few months. We are currently using Amplify 2.45.3. Unfortunately we do not have logs that give more info, and we cannot reproduce the issue.

We believe the connectivity is good in these cases. Restarting the app does not fix the issue.

Here is simplified code snippets. setupAmplify is called from didFinishLaunchingWithOptions

    @objc func setupAmplify() {
        do {
            try Amplify.add(plugin: AWSAPIPlugin())
            try Amplify.add(plugin: AWSCognitoAuthPlugin())
            try Amplify.add(plugin: AWSS3StoragePlugin())
            try Amplify.configure()
            
            Task {
                let signedIn = await self.fetchCurrentAuthSession()
                
                if signedIn == false {
                    //login or re-login
                }
            }
            
        } catch {
            print("Failed to initialize Amplify: \(error)")
        }

    func fetchCurrentAuthSession() async -> Bool {
        do {
            let session = try await Amplify.Auth.fetchAuthSession()
            
            if session.isSignedIn {
                await self.fetchAttributes()
                return true
            }  else { 
                //handle this case
            }
            
        } catch let error as AuthError {
            print("Fetch session failed with error \(error)")
        } catch {
            print("Unexpected error: \(error)")
        }
        
        return false
    }

Expected behavior

Stays logged in or refreshes login until refreshToken expires

Amplify Framework Version

2.45.3

Amplify Categories

Auth

Dependency manager

Swift PM

Swift version

6.0.2

CLI version

12.13.1

Xcode version

16.1

Platforms

iOS

OS Version

18.3.2

Device

iPhone 12

Metadata

Metadata

Assignees

No one assigned

    Labels

    authIssues related to the Auth categorybugSomething isn't workingpending-maintainer-responseIssue is pending response from an Amplify team member

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions