How do I call a system function before registering users in Mongo RealmSwift?

Viewed 84

Apparently one of the requirements to call a Realm system function is to user login first. I need the user to confirm their external device ID to make sure it exists and is available. If it is, then proceed with emailPasswordAuth and user registration.

I've been triggering the if/elseif error handlers underneath CheckDeviceAvailability but somehow the return is ignored and the user is registered anyways. What am I doing wrong?

I've tried using app.currentUser?.functions..., and I've also tried nesting the function within the app.login block but something is still wrong. I don't really understand the async functionality so I imagine it has something to do with that.

func signUp(EMAIL: String, PASSWORD: String, CONFIRM: String, DEVICEID: String) {
        if EMAIL == "" || PASSWORD == "" || CONFIRM == "" || DEVICEID == "" {
            self.errorMSG = "Fields must be filled"
            return
        }
        
        if PASSWORD != CONFIRM {
            self.errorMSG = "Passwords must match"
            return
        }
        
        app.currentUser!.functions.CheckDeviceAvailability([AnyBSON(DEVICEID)]) { result, error in
            self.statusMSG = result?.stringValue
            if error != nil {
                self.errorMSG = "Failed to call function: \(error!.localizedDescription)"
                _ = app.currentUser?.logOut()
                return
                
            } else if result!.stringValue != "available" {
                self.errorMSG = "That device is already taken, try again"
                _ = app.currentUser?.logOut()
                return
                
            } else {
                
                app.emailPasswordAuth.registerUser(email: EMAIL, password: PASSWORD) { ERROR in
                    guard ERROR == nil else {
                        self.errorMSG = "Failed to register user: \(ERROR!.localizedDescription)"
                        return
                    }
                    
                    app.login(credentials: .emailPassword(email: EMAIL, password: PASSWORD)) { (RESULT) in
                        DispatchQueue.main.async {
                            switch RESULT {
                            case .failure:
                                return
                            case .success(let USER):
                                        
                                let config = USER.configuration(partitionValue: "U-\(USER.id)")
                                Realm.asyncOpen(configuration: config) { (RES) in
                                    switch RES {
                                    case .failure(let ERR):
                                        self.errorMSG = "Failed to open realm: \(ERR)"
                                        return
                                    case .success(let REALM):
                                        self.statusMSG = "Open user realm success"
                                        let newUser = User()
                                        newUser._id = USER.id
                                        newUser.auraID = "U-\(USER.id)"
                                        newUser.Preferences = User_Preferences()
                                        newUser.Preferences?.email = EMAIL
                                        newUser.Preferences?.displayName = ""
                                        newUser.Preferences?.deviceID = DEVICEID
                                        newUser.Preferences?.signupDate = Date()
                                        
                                        try! REALM.write {
                                            REALM.create(User.self, value: newUser, update: .modified)
                                        }
                                        self.openUserRealm()
                                    }
                                    
                                }
                            
                                
                            }
                        }
                    }
                }
            }
        
        }
0 Answers
Related