Login() with metamask and Signup()

Hi Team,

I created successfully the signup function based on your docs:

signup = async() => {

    const user = new Moralis.User();
    user.set("username", document.getElementById("user-username-new").value);
    user.set("email", document.getElementById("user-email-new").value);
    user.set("password", document.getElementById("user-password").value);
    try {
        await user.signUp();
        // Hooray! Let them use the app now.
        alert("Successfully Signed Up! Login With MetaMask");
        console.log(user)
    } catch (error) {
        // Show the error message somewhere and let the user try again.
        alert("Error: " + error.code + " " + error.message);
        console.log(user)
    }

}

In my login function I would like to check if the user is signed up based on his “user-username” and “user-useremail”. If he is not signed up, then authenticate() should not work and print a notification. Any idea how to change the below login function()

login = async() => {
    console.log(user)
    await Moralis.Web3.authenticate().then(async function(user) {
        console.log('logged in');
        user.set("name", document.getElementById("user-username").value);
        user.set("email", document.getElementById('user-email').value);
        await user.save();
        window.location.href = "dashboard.html";
    })
}

@cryptokid any idea how to improve?

        user = await Moralis.User.current();
        if (!user) {
            user = await Moralis.authenticate()
        }

or is something that I didn’t understand?

1 Like

You can query the _User class for the username/email to check if the user is presnet and then proceed with login if the username/email is found.

1 Like