Z
Z
Zholaman Zhumanov2020-04-13 15:06:29
JavaScript
Zholaman Zhumanov, 2020-04-13 15:06:29

How to display user data in Node js + Express, MongoDB?

Hey!!! Need help!!!
The user registers on the site and fills in the data for the personal account, and the next time when authorizing, his data is displayed on the personal account! I don’t understand how to do this
Strictly don’t judge, I’m just a beginner

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Kiljko, 2020-04-13
@zholla

Well, look, I think the advice, you are not interested in watching YouTube, but in short here is the algorithm:

Registration:
1) The front sends a POST request to the backend with the data entered by the user.
2) The bull takes out the user's data in a separate variable (for example, user), because. it will be necessary to work with this data in the future and it is desirable to do this in a separate variable.
3) The back checks the data for validity, for example, that there are no unnecessary characters in the password, or that the mail is valid
4) The back uses bcryptjs to encrypt the user's password and replaces what the user entered with what the bcryptjs module produces (remember that we change only the data in the created variable (for example, user), and not the data that arrived with a POST request)
5) The back generates an accessToken and saves it to our variable with the user data (for example, user) (you can use the same bcryptjs for this, of course, but in the future I advise you to look for something else)
6) The back sends a request to mongo to save the user in the database
7) The back sends the user's accessToken to the front, which needs to be saved in cookies

Now, as for getting data:
1) The front sends a GET request to the back to get user data. At the same time, sending the cookies that we saved earlier
2) The back sends a request to the monga to search for a user by accessToken in the database
3) The back returns the user data received from the monga to the front
4) The front shows this data to the user

As for authorization:
1) The front sends a POST request to the back with the email and password entered by the user.
2) The back sends a request to the mongo to search for a user by email
3) The back bcryptjs.compareSync()checks with the help whether the password received from the database matches the one entered by the user
4) The back returns the user's accessToken to the front, which must be saved in cookies

A few notes:
1) email must be unique, there can't be two users with the same email, so if you are using mongoose - specify "unique: true" in the user schema
Example:
const userSchema = new Schema({
  email: {
    type: String,
    unique: true,
  }
});

2) mongoose will help you a lot in interacting with MongoDB Any
questions?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question