Bi-HanNoobSaibot
Bi-HanNoobSaibot

Reputation: 215

This email already exists validation

I am making a React application where i submit the username, password and email to the mongo database.

Now I am trying to figure out how I could check inside of React whether the user or email already exists. Meaning so I could show an error-box telling the user to choose something else as an username.

I do know how to do it when I use Node.js and Handlebars. I know how to check my database with the Find() method of mongoose. But I just don't understand how to think now that I am using React. When I check if the user already exists in the back-end and it shows that it does exist, how could I inform my front-end (React) that it does?

When I use node.js and handlebars I use flash messages, and it works fine. I guess my question could be summarized to, how should I do to get my React front-end to cooperate with my Node/Express back-end to share info about a username inside of the database already existing?

I have no code to show, this is more of asking for advice on what tools or methods I should use. I just can't figure it out.

Thank you in advance!

Upvotes: 2

Views: 13334

Answers (3)

Vennela Pabbu
Vennela Pabbu

Reputation: 1

catch((err) => {
                console.log(err);
                if(err.status=404){
                   alert("email already used");
                }

Upvotes: 0

Henry Woody
Henry Woody

Reputation: 15692

You'll need to have your back-end inform your front-end about whether or not an email has already been used since the front-end has no way of knowing without the back-end.

Basically, when a user tries to register, you should send your registration request from the front-end to the back-end without worrying about duplicate emails. The response from the server should indicate whether or not registration was successful, and if not, why not.

For example the registration component in your React code might look something like this:

class RegistrationComponent extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            email: "",
            password: "",
            error: "",
        }
    }

    handleSubmit = async event => {
        event.preventDefault();
        const { email, password } = this.state;

        const response = await sendRegisterRequest(email, password);
        const responseJson = await response.json();

        if (response.status !== 200) {
            this.setState({error: reponseJson.error});
        } else {
            // handle successful registration
        }
    }

    render() {
        const { error } = this.state;

        return (
            <form onSubmit={ this.handleSubmit }>
                <span>{ error }</span>
                { /* rest of the form */ }
            </form>
        )
    }
}

Where sendRegisterRequest is some module that handles sending registration requests to the server.

Note that this front-end logic expects the server to respond with status 200 on successful registration and with something else (status 400) if there is an error. Also if there is an error, the server should respond with a payload body that looks like: {"error": "That email is already in use"}.

You mention that you know how to check for existing email addresses on the server, so just check in that manner before creating a new account, and if the email address is already in use send the error payload with a status of 400.

Upvotes: 3

Jhm
Jhm

Reputation: 101

You can respond with a 400 status if it occurs then send the error message to the frontend that way. e.g. return res.status(400).json(errors).

Upvotes: 1

Related Questions