fuana database for static hosted sites

50 views Asked by At

so i wrote this code and I am getting no errors in the console the only error i get from the fauna server is An error occurred while logging in. also i will change the secret key after this is awnsered for security reasons.

const client = new faunadb.Client({
    secret: "fnAE7vAmHdAA1I7LvovMRWnGVVM2_sit_IrKDgnN"
});

async function signUp() {
    const email = document.getElementById("email").value;
    const password = document.getElementById("password").value;

    try {
        const result = await client.query(
            q.Create(
                q.Collection("users"),
                {
                    data: {
                        email,
                        password
                    }
                }
            )
        );

        alert("Sign up successful! You can now log in.");
    } catch (error) {
        console.error(error);
        alert("An error occurred while signing up.");
    }
}

async function login() {
    const email = document.getElementById("email").value;
    const password = document.getElementById("password").value;

    try {
        const result = await client.query(
            q.Get(
                q.Match(
                    q.Index("users_by_email"),
                    email
                )
            )
        );

        if (result.password === password) {
            alert("Login successful!");
        } else {
            alert("Incorrect email or password.");
        }
    } catch (error) {
        console.error(error);
        alert("An error occurred while logging in.");
    }
}
<!DOCTYPE html>
<html>

<head>
    <script src="https://unpkg.com/faunadb"></script>
    <script></script>
</head>

<body>
    <h1>Sign Up</h1>
    <form>
        <label for="email">Email:</label>
        <input type="email" id="email" required>
        <br><br>
        <label for="password">Password:</label>
        <input type="password" id="password" required>
        <br><br>
        <button type="button" onclick="signUp()">Sign Up</button>
    </form>

    <h1>Login</h1>
    <form>
        <label for="email">Email:</label>
        <input type="email" id="email" required>
        <br><br>
        <label for="password">Password:</label>
        <input type="password" id="password" required>
        <br><br>
        <button type="button" onclick="login()">Login</button>
    </form>
</body>

</html>

I've tried looking for errors in thr java script and tried playing around with the fauna database itself

1

There are 1 answers

0
ptpaterson On

IMPORTANT

note about credentials in Fauna: using Fauna's built in Credentials support means you don't store the actual password, only the hashed password. This is also what enables Fauna's built in Login function, which returns a new user Token. Your current application provides all users with permissions to read every user, including every user's password, and that is very, very bad.

Your errors

I see that you are trying to access the password field by doing result.password. Notice that when you created the document, password is under the data field.

        if (result.data.password === password) {
            alert("Login successful!");
        } else {
            alert("Incorrect email or password.");
        }

A better way

You should follow along with the User Authentication Tutorial.

It is my recommendation that you turn signup and login into UDFs and use a public key that has two permissions (and only two permissions):

  1. call Function("signup")
  2. call Function("login")

Then your application can use the public key to access these functions. The login function then gives you a separate Token, which you use to make a new Fauna Client. That Token should have whatever privileges a user should have (but no more).

Fauna Community Resources

Forums: https://forums.fauna.com/

Discord: https://discord.gg/2qXynEjn