New endpoints and logic for the new flow
In server.js
we need to add a new endpoint
app.post("/auth/auth-options", (req, res) => {
const user = findUser(req.body.email);
if (user) {
res.send({
password: true,
google: user.federated && user.federated.google,
webauthn: user.webauthn
})
} else {
res.send({
password: true
})
}
});
Then on API.js
checkAuthOptions: async (user) => {
return await API.makePostRequest(API.endpoint + "auth-options", user);
},
And finally, on Auth.js we change login
while adding a new checkAuthOptions
method:
login: async (event) => {
if (event) event.preventDefault();
if (Auth.loginStep==1) {
Auth.checkAuthOptions();
} else {
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
});
}
},
checkAuthOptions: async (event) => {
const response = await API.checkAuthOptions({
email: document.getElementById("login_email").value
});
if (response.password) {
document.getElementById("login_section_password").hidden = false;
}
if (response.webauthn) {
document.getElementById("login_section_webauthn").hidden = false;
}
Auth.challenge = response.challenge;
Auth.loginStep = 2;
},