How to remember devices using AWS Amplify SDK?

Viewed 574

I have implemented a sign up/ sign in feature using AWS Amplify and swift using my own View controllers instead of the Drop-in auth. The problem starts once I quit the app and restart it. After I do that the user is no longer signed in. I have set remember devices to always in the User Pool Settings. image of settings Has anyone ever encountered this problem? Here is my function where the user gets confirmed and everything works properly except for remembering the user

    @objc func confirm(){
        print("confirm pressed")
        guard let verificationCode = verificationTextField.text else{
            return
        }
        AWSMobileClient.default().confirmSignUp(username: username, confirmationCode: verificationCode) { (signUpResult, error) in

            if let signUpResult = signUpResult{
                switch(signUpResult.signUpConfirmationState){
                case .confirmed:
                    AWSMobileClient.default().deviceOperations.updateStatus(remembered: true) { (result, error) in //This is where I try to save the users device
                        print("User is signed up and confirmed")
                        DispatchQueue.main.async {
                            let signedInTabBar = SignedInTabBarController()
                            self.view.window!.rootViewController = signedInTabBar
                        }
                    }
                case .unconfirmed:
                    print("User is not confirmed and needs verification via \(signUpResult.codeDeliveryDetails!.deliveryMedium) sent at \(signUpResult.codeDeliveryDetails!.destination!)")
                case.unknown:
                    print("Unexpected case")
                }
            }else if let error = error {
                print("\(error.localizedDescription)")
            }
        }

    }
1 Answers

As I right understand you need to check if a user signed in or not. To do this you need to add this code on the start of the app or wherever you check a user status:

AWSMobileClient.default().initialize { userState, error in
    OperationQueue.main.addOperation {
        if let error = error {
             AWSMobileClient.default().signOut()
            assertionFailure("Logic after init error: \(error.localizedDescription)")
        }

        guard let userState = userState else {
             AWSMobileClient.default().signOut()
             return
        }

        guard userState == .signedIn else {
            return
        }
    }
}
Related