Creating a system for handling multiple users in my to-do app with MERN stack: What is the best approach

Currently, I am working on adding a feature that would allow multiple users to use my to-do app. However, I am uncertain about the most effective way to do this. Essentially, I want users to be able to create accounts, log in, and have access to their own lists and tasks.

One concept that came to mind is to have a separate document for each user, with data structured like this:

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

Are there any alternative methods that could be more efficient?

Answer №1

If my interpretation is correct, based on the fact that you included mongoose as a tag, it seems like you are utilizing it for your project. Start by designing a User model:

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

Next, decide whether you want to store lists within the user document or reference them externally (storing lists in separate documents and linking with user IDs). Create user routes to allow for user creation, deletion, and login functionality. Implement authentication using tools such as express-jwt and jsonwebtoken to pass tokens between your API and the client. With the model and routes in place, multiple users can be managed.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

ReactJS Antd displays a faulty design on the webpage

Currently, I am utilizing Antd for a ReactJs project, however, I have noticed an issue with the rendering of components in the layout. Below is the code snippet: import React from 'react'; export default class Test extends React.Component { ...

JavaScript - changing object into a string (not functioning properly)

Looking to convert a JavaScript object into a string? Here's an example: var obj = {"name": "XXX", "age": "27"}; After doing some research, I found out about JSON.stringify(obj); JSON.stringify(obj); works perfectly when the IE8 modes are set as fo ...

What if there was a magical jQuery method that could automatically trigger a callback function? What could it possibly be named?

Is there a way to load only images and iframes, similar to the .load() function? I want to automatically add a selector element into the "this" variable for this purpose. $('document').ready(function({ $('a').<Something to trigg ...

Keys have been included in the JSON data stored within a variable

When making an AJAX call from my application to send a JSON message, I include the following variable: json_msg = {"object":"page", "entry":[ {"id":"317614815243036", "time":1473615625653, ...

Error handling in a Try Catch statement fails when using SSJS Platform.Response.Redirect

I am currently facing an issue with threadabortexception in .NET, and despite trying various options, I have not been able to resolve it. In essence, the Redirect function is throwing errors and landing in the catch block, regardless of whether the second ...

Grails MongoDB encountering difficulties when attempting to deploy the application on Tomcat

Currently using Grails 2.3.8 and MongoDB plugin 3.0.3 with a MongoDB database hosted by MongoLab. Everything works fine when the application is running locally. However, when creating a WAR file and deploying it on a Tomcat server, an error occurs: 2015- ...

Encountering an issue while setting up the ContextAPI in nextJS, where properties of undefined Provider cannot be read

I'm encountering difficulties in implementing ContextAPI with a nextjs application. The error message I keep receiving is: TypeError: Cannot read properties of undefined (reading 'Provider') This is my approach to creating the context: impo ...

Using React to update state with a function that returns a value

I am facing an issue where the value returned by the function checkSuggestionList is not being passed to this.state.validSearchParentInput. It seems like the value is being set using setState before the function checkSuggestionList finishes executing. ...

Is there a tutorial for JavaScript with Selenium WebDriver?

I've recently embarked on the journey of writing tests with Selenium WebDriver and JavaScript. However, I'm facing a roadblock in finding a comprehensive tutorial that covers element locators, commands, verification commands for elements, clickin ...

Using req.param in res.render results in unexpected console output

Currently, I am utilizing EJS templates with Node.js and Express. My goal is to pass a request parameter to my EJS template, which is successful. Strangely though, my console log is displaying something unexpected. Software Versions: Node 0.10.26 Expres ...

What is the reason for `change()` not being defined in the constructor? Is it supposed to be accessed through the `__proto__` link in `

Why is the change() method not defined in the constructor? Shouldn't it be accessed through the proto link in the p.proto? // The first change() is not defined, why? class Person { constructor() { console.log(this) console.log(this.__prot ...

Navigate to the end of the chat window using AngularJS

Is there a way to automatically scroll to the bottom whenever a new message is received? I have code that moves the scrollbar, but it doesn't go all the way to the bottom. Can someone please help me with this? Here is my code on Plunker: http://plnk ...

Press the second form submit button after the completion of another Observable

Below is the unique process we aim to accomplish using solely RXJS Observables: Press Login button (form with username & password). We bind Observable.fromEvent with our form. Upon submitting the form, call loginUser() to send an http request to serv ...

"Encountering an error: invalid CSRF token in Node.js csurf

I have been utilizing the npm module csurf to generate a token. Initially, I retrieve the token from the server and then utilize it for the /register request. When I replicate these steps in Postman, everything seems to function correctly, but unfortunatel ...

Blazor Server: Seamless breadcrumb navigation updates with JavaScript on all pages

I have implemented a breadcrumb feature in my Blazor Server project within the AppLayout that functions for all pages: <ul id="breadcrumbs" class="breadcrumbs"> <li><a href="#">Home</a></li> ...

Establish an index in MongoDB that is dependent on greater than and less than operations

I have a Collection with many documents named "products", I am looking to enhance the performance by setting up an index for it. However, the problem is I am unsure about how the index works and whether it will be beneficial to me or not. The fields "s ...

"Troubleshooting the issue of nested jQuery Ajax requests failing to execute properly

UPDATE: I'm getting an error saying that my li tag is considered an illegal token. I'm unsure how to resolve this issue as I believed I was appending it within a ul tag I'm tasked with fetching a JSON array of public Github repositories and ...

Display a PDF file within an IFrame using JavaScript and then print it

Why is it so challenging to achieve? I've dedicated 48 hours to research this, yet it seems impossible! Although recent Chrome versions allow the parent window to access PDFs in iframes, both FF and IE prevent any interaction with the iframe that dis ...

Issue with CKEditor5 Link popup not displaying in both Bootstrap 5 modal and RSuite library modal in React

React: How to troubleshoot CKEditor5 Link popup not appearing in a modal using either Bootstrap 5 or rsuite library? Seeking advice on resolving this issue. Your help is appreciated! ...

Creating a Music Bingo game to ring in the New Year

Looking to create a Music Bingo game for New Year's Eve with randomized songs that can be selected, but experiencing an issue where nothing happens when you have 4 in a row. Attempted adding a submit button, but it doesn't trigger any action. Ide ...