Home > Back-end >  How to implement multiple users in my to do app (MERNstack)?
How to implement multiple users in my to do app (MERNstack)?

Time:03-30

I am trying to implement multiple users for my to do app, but im unsure of the best approach. Basically, i want to allow users to register and login, and only CRUD lists and tasks for their account.

One idea I had was to create a document per user and the data would look like:

{
user: "john doe",
password: "qwerty",
_id: ObjectID("1234567890"),
lists:[
   {title: "school",
    tasks: ["math", "english", "science"]
   },
   {title: "work",
    tasks: ["budget", "presentation", "excel"]
   }
]

is there an alternative approach? Thanks in advance

CodePudding user response:

If I understand you correctly, and from what I can see you listed mongoose as a tag, so I'm assuming you are implementing it. Create a User model:

const userSchema = new Schema({
   username: {
      type: String,
      required: true
   },
   password: {
      type: String,
      required: true
   }
})

Then you need to figure out if you want to store lists in the document, or if you want to reference them (meaning you put the lists in different documents and attach the id to user). You will then need to create the user routes, where users can be created, and deleted. you will also want to implement a login. You will then need to create authentication using something like express-jwt and jsonwebtoken where tokens will be sent back and forth from your api and the client. By creating the model and the routes, multiple users can be created.

  • Related