Problem statementSend feedbackYou are working on a web application that involves managing user authentication.Objective:Implement the login method on the User prototype.This method should take two parameters:username and password.The method should check if the provided username and password match the systemCredentials. If they match, it should log "Login successful!" to the console. Otherwise, it should log "Login failed!".Lastly, modify the login method by using the bind method to create a reusable function named loginFunction that is bound to the systemCredentials object.
Question
Problem statementSend feedbackYou are working on a web application that involves managing user authentication.Objective:Implement the login method on the User prototype.This method should take two parameters:username and password.The method should check if the provided username and password match the systemCredentials. If they match, it should log "Login successful!" to the console. Otherwise, it should log "Login failed!".Lastly, modify the login method by using the bind method to create a reusable function named loginFunction that is bound to the systemCredentials object.
Solution
Here is a step-by-step solution in JavaScript:
- First, define the
systemCredentialsobject. This object will hold the username and password.
let systemCredentials = {
username: 'admin',
password: 'password123'
};
- Next, define the
Userfunction. This function will serve as a constructor for creating new users.
function User(username, password) {
this.username = username;
this.password = password;
}
- Then, add the
loginmethod to theUserprototype. This method will check if the provided username and password match thesystemCredentials.
User.prototype.login = function(username, password) {
if (this.username === systemCredentials.username && this.password === systemCredentials.password) {
console.log('Login successful!');
} else {
console.log('Login failed!');
}
};
- Finally, use the
bindmethod to create a reusable function namedloginFunctionthat is bound to thesystemCredentialsobject.
let loginFunction = User.prototype.login.bind(systemCredentials);
Now, you can use loginFunction to check if a given username and password match the systemCredentials.
loginFunction('admin', 'password123'); // Logs: "Login successful!"
loginFunction('user', 'password'); // Logs: "Login failed!"
Similar Questions
Develop a Python script to simulate a basic user authentication system using if-else statementsand logical operators. The script should prompt the user for a username and password and validatethem against predefined credentials
Develop a Python script to simulate a basic user authentication system using if-else statementsand logical operators. The script should prompt the user for a username and password and validatethem against predefined credentials.
Design a login form using HTML & JavaScript with following validations usingRegular Expression on username and password fields.1. Username and Password should not be both blank2. Username should not start with digit,_, @ or #3. Password length must be 8 to 16 characters
Which of the following methods can be added to the UserAccount class in the tutorial to access the password?a.)public void setPassword(int password) { this.password = password;}b.)public int getPassword() { return password;}c.)public void setPassword(String password) { this.password = password;}d.)public String getPassword() { return password;}
Write a python program to accept the username "Admin" asthe default argument and password 123 entered by the user toallow login into the system using function
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.