Skip to main content

Build Auth & Sessions into our Fruits App Part 2

Lesson Objectives

  • add users model and controller
  • add bcrypt and hash the password

Set up

  • We only need two routes for our user
  • the new form to sign up a new user
  • the post route to create a new user
  • We want users to have their passwords encrypted on sign up

Users model

  • on same level as package.json
  • npm install bcrypt

in models/users.js

models/users.js
const mongoose = require("mongoose");
const Schema = mongoose.Schema;

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

const User = mongoose.model("User", userSchema);

module.exports = User;

in server.js

server.js
const userController = require("./controllers/users_controller.js");
app.use("/users", userController);

in controllers/users_controller.js

controllers/users_controller.js
const bcrypt = require("bcrypt");
const express = require("express");
const users = express.Router();
const User = require("../models/users.js");

users.get("/new", (req, res) => {
res.render("users/new.ejs");
});

users.post("/", async (req, res) => {
//overwrite the user password with the hashed password, then pass that in to our database
req.body.password = bcrypt.hashSync(
req.body.password,
bcrypt.genSaltSync(10)
);
try {
const createdUser = await User.create(req.body);
console.log("created user is: ", createdUser);
res.redirect("/");
} catch (error) {
console.log(error);
};
});

module.exports = users;

Check console for successful creation of user

Check Console