Calling the APIs
Now within scripts/Auth.js
, add the following members of the Auth
object and test the results
login: async (event) => {
if (event) event.preventDefault();
const user = {
email: document.getElementById("login_email").value,
password: document.getElementById("login_password").value
};
const response = await API.login(user);
Auth.postLogin(response, {
...user,
name: response.name
});
},
register: async (event) => {
event.preventDefault();
const user = {
name: document.getElementById("register_name").value,
email: document.getElementById("register_email").value,
password: document.getElementById("register_password").value
}
const response = await API.register(user);
Auth.postLogin(response, user);
},
Also we will create a postLogin
and logout
methods:
postLogin: (response, user) => {
if (response.ok) {
Auth.isLoggedIn = true;
Auth.account = user;
Auth.updateStatus();
Router.go("/account");
} else {
alert(response.message)
}
},
logout: () => {
Auth.isLoggedIn = false;
Auth.account = null;
Auth.updateStatus();
Router.go("/");
},