Validación de “log in”

 

Aquí te dejamos el código utilizado en esta lección:

Module Accounts {
    Bool performLogin (String username, String password) {
        if (username == null || username.trim().length() < 1) {
            broker.ui.showAlert('Oops', 'Please enter your username.')
            return false
        }

        if (password == null || password.length() < 1) {
            broker.ui.showAlert('Oops', 'Please enter your password.')
            return false
        }

        Filter userNameFilter = Filter(modelName: "User")
        userNameFilter.equalTo("name", username.trim())
        Array<User> existingUsers = broker.cloudPersistence.getWithFilter(userNameFilter)

        if (existingUsers.size() == 0) {
            broker.ui.showAlert('Oops', "There's no user with username: " + username)
            return false
        }

        User user = existingUsers.get(0)
        if (user.password != password) {
            broker.ui.showAlert('Oops', "The password you have entered is wrong.")
            return false
        }

        return true
    }
}

RuleContext LoginContext {
    Rule login {
        Login loginExp = broker.ui.getDataSource()
        
        if (Accounts.performLogin(loginExp.username, loginExp.password) == true) {
            Welcome welcomeDataSource = Welcome()
            welcomeDataSource.greetings = "Welcome " + loginExp.username + " to the treasure hunt app!"
            broker.ui.push('Welcome',welcomeDataSource)
        }
    }
}