aws-amplify / amplify-swift

A declarative library for application development using cloud services.
Apache License 2.0
447 stars 193 forks source link

Amplify Auth login/logOut session #1805

Closed RoustamManookian closed 2 years ago

RoustamManookian commented 2 years ago

Describe the bug

I use Amplify Auth in my IOS app.

When the user logs in with social signIn flow and leaves the app without signOut and comes back the Amplify.Auth.fetchAuthSession result is AWSAuthCognitoSession(isSignedIn: true,...) but the session is in Auth.fetchSessionAPI state. So when I try to reassign the user it gives me an error that the user is already signed in try to sign out, but when I try to sign out pops up a dialog asking "the app wants to use amazoncognito.com to sign in" if I choose continue it says Safari cannot open the page because the address is invalid and if I choose cancel it gives me an error "Sign out failed with error AuthError: User cancelled the signOut flow and could not be completed. Recovery suggestion: Present the signOut UI again for the user to sign out. Caused by: userCancelled"

Even when I remove the app from the device and install a new fresh installation the situation is not changing.

Steps To Reproduce

Steps to reproduce the behavior:
1. Open the app
2. Click on 'Continue with Google'
3. Close the app without signing out
4. Open the app after a while
5. see the behavior

Expected behavior

If the user wants to sign out anytime it has to work No matter when

Amplify Framework Version

7.6.12

Amplify Categories

Auth

Dependency manager

Swift PM

Swift version

5

CLI version

7.6.12

Xcode version

13.3.1

Relevant log output

No response

Is this a regression?

No

Regression additional context

No response

Device

iPhoneX, simulator-10,11,12,13

iOS Version

15.0, 15.4

Specific to simulators

No response

Additional context

No response

atierian commented 2 years ago

Thanks for opening this issue @RoustamManookian. You wrote that you're using Amplify Framework Version 7.6.12. This is most likely the CLI version. Can you let us know which version of Amplify iOS you're using? Since you're using SPM, you can find it in the bottom of the Project Navigator in the Package Dependencies section. Thanks!

RoustamManookian commented 2 years ago

is this helpful?

dependencies: [ .package(name: "AWSiOSSDKV2", url: "https://github.com/aws-amplify/aws-sdk-ios-spm.git", .upToNextMinor(from: "2.27.0")), .package(name: "AppSyncRealTimeClient", url: "https://github.com/aws-amplify/aws-appsync-realtime-client-ios.git", from: "1.8.0"), .package(url: "https://github.com/stephencelis/SQLite.swift.git", .exact("0.13.2")) ]

Greenshot 2022-05-19 03 13 30

,

RoustamManookian commented 2 years ago

And one very weird thing !!!!! Even when I go to Cognito Console and delete the user the user still signs in 😳🤯

atierian commented 2 years ago

Yes, that helps thanks. The version rules you have in your project point at the main branch of Amplify. We recommend using an actual version because the main branch can change at anytime. e.g. up to next major from 1.24.1 I don't believe that is responsible for what you're seeing, but you should update it anyway.

To confirm, you are using Social Sign in with Web UI, correct?

RoustamManookian commented 2 years ago

Hi This is my sign-in function

func webSignIn() {
        Amplify.Auth.signInWithWebUI(for: .google, presentationAnchor: window, options: .preferPrivateSession()){result in
            switch result {
            case .success:
                print("Signed in")

                Amplify.Auth.fetchAuthSession {[weak self] result in
                    do {
                        let session = try result.get()

                        // Get user sub or identity id
                        if let identityProvider = session as? AuthCognitoIdentityProvider {
                            let usersub = try identityProvider.getUserSub().get()
                            let identityId = try identityProvider.getIdentityId().get()
                            print("User sub -> \(usersub)")
                            print("identity id -> \(identityId)")

                            guard let currentUser  = Amplify.Auth.getCurrentUser()
                            else {
                                return
                            }

                            AWS_API.apiManager.update_identity_id(userName: currentUser.username, identityId: identityId)

                             //   self?.attachPolicy(identityId: identityId)
                            //self?.attachPrincipalPolicy(identityId: identityId)
                        }
                    } catch {
                        print("Fetch auth session failed with error - \(error)")
                    }
                }

            case .failure(let error):
                print("Error signInWithWebUI -> \(error)")
            }
        }
    }

And this is the logOut function I tried all three versions, the same result.

func signOut() {
        _ = Amplify.Auth.signOut() { result in
            switch result {
            case .success:
                print("Signed out")

            case .failure(let error):
                print("ERROR to sign out: \(error)")
            }
        }
    }

    func signOutLocally(){
        Amplify.Auth.signOut()
            .resultPublisher
            .sink {
                if case let .failure(authError) = $0 {
                    print("Sign out failed with error \(authError)")
                }
            }
            receiveValue: {
                print("Successfully signed out")
            }
            .store(in: &cancellables)
    }

    func signOutGlobally(){
        Amplify.Auth.signOut(options: .init(globalSignOut: true))
            .resultPublisher
            .sink {
                if case let .failure(authError) = $0 {
                    print("Sign out failed with error \(authError)")
                }
            }
            receiveValue: {
                print("Successfully signed out")
            }
            .store(in: &cancellables)
    }

}
royjit commented 2 years ago

Can you make sure that you have proper redirectURIs in your amplifyconfiguration.json:

     "Auth": {
                    "Default": {
                        "OAuth": {
                            "WebDomain": "<your hostedUI webdomain>",
                            "AppClientId": "xxx",
                            "SignInRedirectURI": "myapp://",
                            "SignOutRedirectURI": "myapp://",
                            "Scopes": [
                                "phone",
                                "email",
                                "openid",
                                "profile",
                                "aws.cognito.signin.user.admin"
                            ]
                        },
RoustamManookian commented 2 years ago

Hi

After a long videochat with AWS support center, we found what was the problem !!!

Because I pulled the amplify configurations from the amplify pull command I didn't pass all the steps. I didn't make the changes that had to do in plist file

I added this in plist file and everything is working now !!! `

CFBundleURLTypes
     <array>
         <dict>
             <key>CFBundleURLSchemes</key>
             <array>
                 <string>YourAppName</string>
             </array>
         </dict>
     </array>

`