ios – How to retrieve userId from amplify authentication response using swift

0
31

[ad_1]

I am using amplify to register and sign user in my app written in swift. User registration works but I need a way to retrieve the userId from the response. Sample code below I always get the userId value to be nil even though in the cognito session of IAM I see the user just created

func signUp(username: String, password: String, email: String, firstName: String, lastName: String, phoneNumber: String) {
        //get token ID
        let guestToken = DashboardView.getGuesToken()
        let guestTokenID = DashboardView.getGuesTokenID()
        let userAttributes = [
            AuthUserAttribute(.email, value: email),
            AuthUserAttribute(.givenName, value: firstName),
            AuthUserAttribute(.familyName, value: lastName),
            AuthUserAttribute(.phoneNumber, value: phoneNumber),
            AuthUserAttribute(.preferredUsername, value: username)
        ]
        let options = AuthSignUpRequest.Options(userAttributes: userAttributes)
        Amplify.Auth.signUp(username: email, password: password, options: options) { result in
            switch result {
            case .success(let signUpResult):
                if case let .confirmUser(deliveryDetails, userId) = signUpResult.nextStep {
                    print("Delivery details \(String(describing: deliveryDetails))")
                    print("Delivery details \(String(describing: userId))")
                    DispatchQueue.main.async {
                        self.activityLoaderView.alpha = 0
                        self.activityLoader.alpha = 0
                        self.txtPassword.alpha = 0
                        self.txtPassword.isEnabled = false
                        self.txtVerifCode.alpha = 1
                        self.txtVerifCode.isEnabled = true
                        self.btnVerify.alpha = 1
                        self.btnVerify.isEnabled = true
                    }
                } else {
                    print("SignUp Complete")
                    
                }
            case .failure(let error):
                DispatchQueue.main.async {
                    self.activityLoaderView.alpha = 0
                    self.activityLoader.alpha = 0
                    self.toastView.alpha = 1
                    self.toastViewLabel.text = "Operation couldn't be completed. Please try again."
                    
                    print("An error occurred while registering a user \(error)")
                }
                
            }
        }
    }

I wrote a signup function which when successful I will receive a success message and details pertaining to the user that was just created. I print out a unique userId but it is always empty

[ad_2]