From 403394ad70ef1dcf395227541ef4fc4f7a583de2 Mon Sep 17 00:00:00 2001 From: Yifan-858 <133569248+Yifan-858@users.noreply.github.com> Date: Sun, 26 May 2024 19:20:23 +0200 Subject: [PATCH 1/7] first commit --- backend/package.json | 3 ++ backend/server.js | 89 +++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 87 insertions(+), 5 deletions(-) diff --git a/backend/package.json b/backend/package.json index 8de5c4ce0..3bda8e5d3 100644 --- a/backend/package.json +++ b/backend/package.json @@ -12,8 +12,11 @@ "@babel/core": "^7.17.9", "@babel/node": "^7.16.8", "@babel/preset-env": "^7.16.11", + "bcrypt": "^5.1.1", + "bcryptjs": "^2.4.3", "cors": "^2.8.5", "express": "^4.17.3", + "express-list-endpoints": "^7.1.0", "mongoose": "^8.0.0", "nodemon": "^3.0.1" } diff --git a/backend/server.js b/backend/server.js index dfe86fb8e..ae4d457b6 100644 --- a/backend/server.js +++ b/backend/server.js @@ -1,14 +1,35 @@ import cors from "cors"; import express from "express"; import mongoose from "mongoose"; +import crypto from "crypto"; +import bcrypt from "bcryptjs"; +import expressListEndpoints from "express-list-endpoints"; -const mongoUrl = process.env.MONGO_URL || "mongodb://localhost/project-mongo"; +const mongoUrl = process.env.MONGO_URL || "mongodb://127.0.0.1/project-auth"; mongoose.connect(mongoUrl); mongoose.Promise = Promise; -// Defines the port the app will run on. Defaults to 8080, but can be overridden -// when starting the server. Example command to overwrite PORT env variable value: -// PORT=9000 npm start +const User = mongoose.model("User", { + name: { type: String, unique: true, required: true }, + email: { type: String, unique: true, required: true }, + password: { type: String, required: true }, + accessToken: { + type: String, + default: () => crypto.randomBytes(128).toString("hex"), //user gets a string of random numbers as the accessToken + }, +}); + +//a middleware function for looking for the user based on the accessToken saved in "Authorization" in the header +const authenticateUser = async (req, res, next) => { + const user = await User.findOne({ accessToken: req.header("Authorization") }); + if (user) { + req.user = user; + next(); + } else { + res.status(401).json({ loggedOut: true }); + } +}; + const port = process.env.PORT || 8080; const app = express(); @@ -18,7 +39,65 @@ app.use(express.json()); // Start defining your routes here app.get("/", (req, res) => { - res.send("Hello Technigo!"); + const endpoints = expressListEndpoints(app); + const documentation = endpoints.map((endpoint) => ({ + method: endpoint.methods.join(", "), + path: endpoint.path, + })); + res.json(documentation); +}); + +app.post("/registration", async (req, res) => { + try { + const { name, email, password } = req.body; + + //error handling + if (!name || name.trim() === "") { + return res.status(400).json({ message: "Please enter username" }); + } + if (!email || email.trim() === "") { + return res.status(400).json({ message: "Please enter email" }); + } + if (!password || password.trim() === "") { + return res.status(400).json({ message: "Please enter password" }); + } + if (password.length < 6) { + return res + .status(400) + .json({ message: "Password must be at least 6 characters long" }); + } + + const salt = bcrypt.genSaltSync(); + const user = new User({ + name, + email, + password: bcrypt.hashSync(password, salt), + }); + user.save(); + res.status(201).json({ id: user._id, accessToken: user.accessToken }); + } catch (err) { + res + .status(400) + .json({ message: "Could not create the user", errors: err.errors }); + } +}); + +app.get("/dashboard", authenticateUser); +app.get("/dashboard", (req, res) => { + res.json({ message: "Your're logged in!" }); +}); + +app.post("/login", async (req, res) => { + const user = await User.findOne({ email: req.body.email }); + if (user && bcrypt.compareSync(req.body.password, user.password)) { + res.json({ + userId: user._id, + name: user.name, + accrssToken: user.accessToken, + }); + } else { + res.json({ notFound: true }); + } }); // Start the server From d255fb3beb9fb561f5ec28e48be23f52aa782e28 Mon Sep 17 00:00:00 2001 From: Yifan-858 <133569248+Yifan-858@users.noreply.github.com> Date: Sun, 26 May 2024 22:39:20 +0200 Subject: [PATCH 2/7] build structure --- backend/server.js | 7 +++---- frontend/index.html | 25 ++++++++++++++----------- frontend/package.json | 3 ++- frontend/src/App.jsx | 13 ++++++++++++- 4 files changed, 31 insertions(+), 17 deletions(-) diff --git a/backend/server.js b/backend/server.js index ae4d457b6..727f29761 100644 --- a/backend/server.js +++ b/backend/server.js @@ -82,9 +82,8 @@ app.post("/registration", async (req, res) => { } }); -app.get("/dashboard", authenticateUser); -app.get("/dashboard", (req, res) => { - res.json({ message: "Your're logged in!" }); +app.get("/dashboard", authenticateUser, (req, res) => { + res.json({ message: "You're logged in!" }); }); app.post("/login", async (req, res) => { @@ -96,7 +95,7 @@ app.post("/login", async (req, res) => { accrssToken: user.accessToken, }); } else { - res.json({ notFound: true }); + return res.status(400).json({ notFound: true }); } }); diff --git a/frontend/index.html b/frontend/index.html index 0c589eccd..69a744d15 100644 --- a/frontend/index.html +++ b/frontend/index.html @@ -1,13 +1,16 @@ -
- - - -