From 5cfae3fbcb17d5f7a05841b067a2831a419d46d5 Mon Sep 17 00:00:00 2001 From: Caracal23 <154092424+Caracal23@users.noreply.github.com> Date: Wed, 22 May 2024 11:30:02 +0200 Subject: [PATCH 01/29] starting up week project; manage to create a new user --- backend/package.json | 1 + backend/server.js | 47 ++++++++++++++++++++++++++++++++++++++++++++ package.json | 3 +++ 3 files changed, 51 insertions(+) diff --git a/backend/package.json b/backend/package.json index 8de5c4ce0..7499fa261 100644 --- a/backend/package.json +++ b/backend/package.json @@ -12,6 +12,7 @@ "@babel/core": "^7.17.9", "@babel/node": "^7.16.8", "@babel/preset-env": "^7.16.11", + "bcrypt": "^5.1.1", "cors": "^2.8.5", "express": "^4.17.3", "mongoose": "^8.0.0", diff --git a/backend/server.js b/backend/server.js index dfe86fb8e..992e3950b 100644 --- a/backend/server.js +++ b/backend/server.js @@ -1,11 +1,31 @@ import cors from "cors"; import express from "express"; import mongoose from "mongoose"; +import bcrypt from "bcrypt"; const mongoUrl = process.env.MONGO_URL || "mongodb://localhost/project-mongo"; mongoose.connect(mongoUrl); mongoose.Promise = Promise; +const User = mongoose.model("User", { + name: { + type: String, + unique: true, + }, + email: { + type: String, + unique: true, + }, + password: { + type: String, + required: true, + }, + accessToken: { + type: String, + default: () => bcrypt.genSaltSync(), + }, +}); + // 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 @@ -21,6 +41,33 @@ app.get("/", (req, res) => { res.send("Hello Technigo!"); }); +// Registration endpoint +app.post("/users", async (req, res) => { + const salt = bcrypt.genSaltSync(10); + + try { + const { name, email, password } = req.body; + const user = new User({ + name, + email, + password: bcrypt.hashSync(password, salt), + }); + user.save(); + res.status(201).json({ id: user._id, accessToken: user.accessToken }); + } catch (error) { + res.status(400).json({ + response: error.message, + success: false, + message: "Could not create user", + errors: error.errors, + }); + } +}); + +// Sign-in endpoint + +// Endpoint once user is signed in + // Start the server app.listen(port, () => { console.log(`Server running on http://localhost:${port}`); diff --git a/package.json b/package.json index d774b8cc3..6d01d9860 100644 --- a/package.json +++ b/package.json @@ -3,5 +3,8 @@ "version": "1.0.0", "scripts": { "postinstall": "npm install --prefix backend" + }, + "dependencies": { + "bcrypt": "^5.1.1" } } From 1892dca0a26f4e49b8a307bd0ffa6efad178b02c Mon Sep 17 00:00:00 2001 From: Caracal23 <154092424+Caracal23@users.noreply.github.com> Date: Wed, 22 May 2024 12:03:14 +0200 Subject: [PATCH 02/29] completed backend part --- backend/server.js | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/backend/server.js b/backend/server.js index 992e3950b..c10741ac3 100644 --- a/backend/server.js +++ b/backend/server.js @@ -36,12 +36,22 @@ const app = express(); app.use(cors()); app.use(express.json()); +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 }); + } +}; + // Start defining your routes here app.get("/", (req, res) => { res.send("Hello Technigo!"); }); -// Registration endpoint +// Create user endpoint app.post("/users", async (req, res) => { const salt = bcrypt.genSaltSync(10); @@ -64,9 +74,21 @@ app.post("/users", async (req, res) => { } }); -// Sign-in endpoint - // Endpoint once user is signed in +app.get("/user-page", authenticateUser); +app.get("/user-page", (req, res) => { + res.json({ message: "You are logged in!" }); +}); + +// Sign-in endpoint +app.post("/sessions", 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, accessToken: user.accessToken}) + } else { + res.json({notFound:true}) + } +}) // Start the server app.listen(port, () => { From e0d7677be0c7ea8480885065258530e9138a9a7d Mon Sep 17 00:00:00 2001 From: Caracal23 <154092424+Caracal23@users.noreply.github.com> Date: Wed, 22 May 2024 12:52:45 +0200 Subject: [PATCH 03/29] components creation in frontend --- frontend/package.json | 3 ++- frontend/src/App.jsx | 17 ++++++++++++++++- frontend/src/components/LoginForm.jsx | 7 +++++++ frontend/src/components/LogoutButton.jsx | 5 +++++ frontend/src/components/RegistrationForm.jsx | 7 +++++++ frontend/src/components/UserPage.jsx | 7 +++++++ 6 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 frontend/src/components/LoginForm.jsx create mode 100644 frontend/src/components/LogoutButton.jsx create mode 100644 frontend/src/components/RegistrationForm.jsx create mode 100644 frontend/src/components/UserPage.jsx diff --git a/frontend/package.json b/frontend/package.json index e9c95b79f..db1a62a6e 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -11,7 +11,8 @@ }, "dependencies": { "react": "^18.2.0", - "react-dom": "^18.2.0" + "react-dom": "^18.2.0", + "react-router-dom": "^6.23.1" }, "devDependencies": { "@types/react": "^18.2.15", diff --git a/frontend/src/App.jsx b/frontend/src/App.jsx index 1091d4310..e9bd18a90 100644 --- a/frontend/src/App.jsx +++ b/frontend/src/App.jsx @@ -1,3 +1,18 @@ +import { BrowserRouter, Routes, Route } from "react-router-dom" +import { RegistrationForm } from "./components/RegistrationForm"; +import { LoginForm } from "./components/LoginForm"; +import { UserPage } from "./components/UserPage"; + + export const App = () => { - return
Find me in src/app.jsx!
; + return ( + + + }/> + }/> + }/> + + + + ) }; diff --git a/frontend/src/components/LoginForm.jsx b/frontend/src/components/LoginForm.jsx new file mode 100644 index 000000000..beaae74fc --- /dev/null +++ b/frontend/src/components/LoginForm.jsx @@ -0,0 +1,7 @@ +//POST request to sessions with email and password + +export const LoginForm = () => { + return ( +
LoginForm
+ ) +} diff --git a/frontend/src/components/LogoutButton.jsx b/frontend/src/components/LogoutButton.jsx new file mode 100644 index 000000000..5c9758f05 --- /dev/null +++ b/frontend/src/components/LogoutButton.jsx @@ -0,0 +1,5 @@ +export const LogoutButton = () => { + return ( +
LogoutButton
+ ) +} diff --git a/frontend/src/components/RegistrationForm.jsx b/frontend/src/components/RegistrationForm.jsx new file mode 100644 index 000000000..70558fbce --- /dev/null +++ b/frontend/src/components/RegistrationForm.jsx @@ -0,0 +1,7 @@ +//POST to the API endpoint /users to create a new user (name, email, password) + +export const RegistrationForm = () => { + return ( +
RegistrationForm
+ ) +} diff --git a/frontend/src/components/UserPage.jsx b/frontend/src/components/UserPage.jsx new file mode 100644 index 000000000..af6bb0bc5 --- /dev/null +++ b/frontend/src/components/UserPage.jsx @@ -0,0 +1,7 @@ +//GET call from /user-page + +export const UserPage = () => { + return ( +
UserPage
+ ) +} From 6166f44d5240efe4a8831fbe1c747173b0a13a17 Mon Sep 17 00:00:00 2001 From: Alma Herrstrom Date: Wed, 22 May 2024 16:01:13 +0200 Subject: [PATCH 04/29] started with registration form in frontend --- frontend/index.html | 11 +++- frontend/public/favicon.png | Bin 0 -> 372 bytes frontend/public/vite.svg | 1 - frontend/src/App.css | 14 +++++ frontend/src/App.jsx | 27 ++++---- frontend/src/components/RegistrationForm.css | 35 +++++++++++ frontend/src/components/RegistrationForm.jsx | 61 ++++++++++++++++++- frontend/src/index.css | 13 ---- 8 files changed, 131 insertions(+), 31 deletions(-) create mode 100644 frontend/public/favicon.png delete mode 100644 frontend/public/vite.svg create mode 100644 frontend/src/App.css create mode 100644 frontend/src/components/RegistrationForm.css diff --git a/frontend/index.html b/frontend/index.html index 0c589eccd..56807424e 100644 --- a/frontend/index.html +++ b/frontend/index.html @@ -1,10 +1,15 @@ - + - + - Vite + React + + + + Authentication Project
diff --git a/frontend/public/favicon.png b/frontend/public/favicon.png new file mode 100644 index 0000000000000000000000000000000000000000..e64bc851c5cc1863dc51a9e3c9688c66d2bcf382 GIT binary patch literal 372 zcmV-)0gL{LP)q{u1(?SAwWRtmMV-DEWy-NuDCx5f|&&;2l-P?f!#0k&@hQPCMC~Nv#skj2( zfouyE&?>8V4_NOgo>ObOzKh=jsT5cNBj5r!6J7#i!=TpQ`6^xq7Lxb}xZa_?0hXej z1Lr;r^o{KuSm>V&y>8pT@g~W>fSQH=6!?~yS04sGCE1HZ%tT^7d>BY2S>h0r7>Dss zXaKjuyq{BJZ6BvbT)w7rS|hf&@2=E2g8^&Ap#>bkA)Enip{m-z42TSUWMRUqfro;a zPL&4Q1u>6R8c@GlP}?)Am2wD`Y