Implementing login, signup, and authenticated routes in the backend using JWTs
Now that we’ve learned about JWTs, we’ll implement them in our backend. First, we need to create a user model in our database, after which we can create routes to sign up and log into our app. Finally, we will implement authenticated routes that are only accessible with a JWT.
Creating the user model
We’ll start the backend implementation by creating a user model, as follows:
- Copy the
ch5folder to a newch6folder, as follows:$ cp -R ch5 ch6
- Open the
ch6folder in VS Code. - Create a new
backend/src/db/models/user.jsfile and define a newuserSchemainside it:import mongoose, { Schema } from 'mongoose' const userSchema = new Schema({ - A user should have a required unique
usernameand a requiredpassword:Â Â username: { type: String, required: true, unique: true }, Â Â password: { type: String, required: true }, }) - Create and export the...