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
ch5
folder to a newch6
folder, as follows:$ cp -R ch5 ch6
- Open the
ch6
folder in VS Code. - Create a new
backend/src/db/models/user.js
file and define a newuserSchema
inside it:import mongoose, { Schema } from 'mongoose' const userSchema = new Schema({
- A user should have a required unique
username
and a requiredpassword
:username: { type: String, required: true, unique: true }, password: { type: String, required: true }, })
- Create and export the...