From 888ca3ca167caa2c8492a47cc163a336a3f1a490 Mon Sep 17 00:00:00 2001 From: AhmedHamed3699 Date: Fri, 15 Dec 2023 08:07:52 +0200 Subject: [PATCH 1/8] =?UTF-8?q?=E2=9C=A8=20feat:=20SingUp=20page?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client/src/App.jsx | 3 +- .../src/assets/styles/components/signUp.css | 86 +++++++++++++++++ .../src/assets/styles/components/signUp.scss | 96 +++++++++++++++++++ .../components/module_OR_PAGE/DELETEME.txt | 0 .../module_OR_PAGE/logIn.jsx} | 0 .../src/components/module_OR_PAGE/signUp.jsx | 62 ++++++++++++ client/src/redux/DELETEME.txt | 0 client/src/services/DELETEME.txt | 0 client/src/theme.css | 52 ++++++++++ client/src/theme.scss | 73 +++++++++----- 10 files changed, 349 insertions(+), 23 deletions(-) create mode 100644 client/src/assets/styles/components/signUp.css create mode 100644 client/src/assets/styles/components/signUp.scss delete mode 100644 client/src/components/module_OR_PAGE/DELETEME.txt rename client/src/{assets/styles/components/DELETEME.txt => components/module_OR_PAGE/logIn.jsx} (100%) create mode 100644 client/src/components/module_OR_PAGE/signUp.jsx delete mode 100644 client/src/redux/DELETEME.txt delete mode 100644 client/src/services/DELETEME.txt create mode 100644 client/src/theme.css diff --git a/client/src/App.jsx b/client/src/App.jsx index 4c0210df..031e195d 100644 --- a/client/src/App.jsx +++ b/client/src/App.jsx @@ -1,9 +1,10 @@ import "./theme.scss"; +import SignUp from "./components/module_OR_PAGE/signUp"; function App() { return ( <> -

🚀 Sanity Check

+ ); } diff --git a/client/src/assets/styles/components/signUp.css b/client/src/assets/styles/components/signUp.css new file mode 100644 index 00000000..bde59f00 --- /dev/null +++ b/client/src/assets/styles/components/signUp.css @@ -0,0 +1,86 @@ +.hero { + display: flex; + width: 22.5rem; + height: 93.375rem; + flex-direction: column; + align-items: center; + margin: auto; + padding: 1.75rem 0.25rem; + gap: 1.1875rem; +} + +.container { + background: #014451; + border-radius: 1.25rem; + display: flex; + width: 19rem; + padding: 1.5rem 0.4375rem; + flex-direction: column; + justify-content: center; + align-items: center; + gap: 1.0625rem; +} + +.card { + background-color: #1f2a37; + border-radius: 1.875rem; + display: flex; + width: 16.9375rem; + padding: 1.0625rem 0rem; + flex-direction: column; + justify-content: center; + align-items: center; + gap: 0.875rem; +} +.card .input-field { + display: flex; + width: 14.16319rem; + padding: 0.125rem 0.75rem; + justify-content: flex-end; + align-items: center; + align-content: center; + gap: 0rem 9.25rem; + flex-wrap: wrap; +} +.card .input-field input { + background: #374151; + margin-block: 0.3rem; + border: 1px solid #4b5563; + border-radius: 0.25rem; + width: 12.66319rem; + height: 1.75rem; + flex-shrink: 0; +} +.card .radio-field { + display: flex; + width: 14.16319rem; + padding: 0.5625rem 0.75rem; + flex-direction: column; + justify-content: flex-end; + align-items: flex-end; + flex-wrap: wrap; + gap: 0.0625rem; +} +.card .radio-field .radio-buttons { + display: flex; + padding: 0.3125rem 0.5rem; + margin: auto; + gap: 3.1875rem; +} +.card .radio-field .radio-buttons small input { + margin: 0.5rem; +} + +button { + color: #e2efee; + font-family: "Noto Kufi Arabic"; + background: #0e0218; + border-radius: 0.3125rem; + display: flex; + width: 12.5rem; + height: 3.125rem; + padding: 0.8125rem 0.6875rem 0.8125rem 0.625rem; + justify-content: center; + align-items: center; + gap: 2.5rem; +}/*# sourceMappingURL=signUp.css.map */ \ No newline at end of file diff --git a/client/src/assets/styles/components/signUp.scss b/client/src/assets/styles/components/signUp.scss new file mode 100644 index 00000000..3c32efc8 --- /dev/null +++ b/client/src/assets/styles/components/signUp.scss @@ -0,0 +1,96 @@ +$Teal-900: #014451; +$grey-800: #1f2a37; +$grey-700: #374151; +$grey-600: #4b5563; +$Primary-900: #0e0218; +$text-color: #e2efee; +$font: 'Noto Kufi Arabic'; + +.hero { + display: flex; + width: 22.5rem; + height: 93.375rem; + flex-direction: column; + align-items: center; + margin: auto; + padding: 1.75rem 0.25rem; + gap: 1.1875rem; +} + +.container { + background: $Teal-900; + border-radius: 1.25rem; + display: flex; + width: 19rem; + padding: 1.5rem 0.4375rem; + flex-direction: column; + justify-content: center; + align-items: center; + gap: 1.0625rem; +} + +.card { + background-color: $grey-800; + border-radius: 1.875rem; + display: flex; + width: 16.9375rem; + padding: 1.0625rem 0rem; + flex-direction: column; + justify-content: center; + align-items: center; + gap: 0.875rem; + .input-field { + display: flex; + width: 14.16319rem; + padding: 0.125rem 0.75rem; + justify-content: flex-end; + align-items: center; + align-content: center; + gap: 0rem 9.25rem; + flex-wrap: wrap; + input { + background: $grey-700; + margin-block: 0.3rem; + border: 1px solid $grey-600; + border-radius: 0.25rem; + width: 12.66319rem; + height: 1.75rem; + flex-shrink: 0; + } + } + + .radio-field { + display: flex; + width: 14.16319rem; + padding: 0.5625rem 0.75rem; + flex-direction: column; + justify-content: flex-end; + align-items: flex-end; + flex-wrap: wrap; + gap: 0.0625rem; + .radio-buttons { + display: flex; + padding: 0.3125rem 0.5rem; + margin: auto; + gap: 3.1875rem; + small input { + margin: 0.5rem; + } + } + } +} + + +button { + color: $text-color; + font-family: $font; + background: $Primary-900; + border-radius: 0.3125rem; + display: flex; + width: 12.5rem; + height: 3.125rem; + padding: 0.8125rem 0.6875rem 0.8125rem 0.625rem; + justify-content: center; + align-items: center; + gap: 2.5rem; +} diff --git a/client/src/components/module_OR_PAGE/DELETEME.txt b/client/src/components/module_OR_PAGE/DELETEME.txt deleted file mode 100644 index e69de29b..00000000 diff --git a/client/src/assets/styles/components/DELETEME.txt b/client/src/components/module_OR_PAGE/logIn.jsx similarity index 100% rename from client/src/assets/styles/components/DELETEME.txt rename to client/src/components/module_OR_PAGE/logIn.jsx diff --git a/client/src/components/module_OR_PAGE/signUp.jsx b/client/src/components/module_OR_PAGE/signUp.jsx new file mode 100644 index 00000000..8316d4fb --- /dev/null +++ b/client/src/components/module_OR_PAGE/signUp.jsx @@ -0,0 +1,62 @@ +import '../../assets/styles/components/signUp.css' + +export default function SignUp() { + return ( +
+

تسجيل حساب

+
+
+
الاسم
+ + + +
+
+
معلومات الحساب
+ + + +
+
+
معلومات أخرى
+ + +
+ +
+
+ ) +} diff --git a/client/src/redux/DELETEME.txt b/client/src/redux/DELETEME.txt deleted file mode 100644 index e69de29b..00000000 diff --git a/client/src/services/DELETEME.txt b/client/src/services/DELETEME.txt deleted file mode 100644 index e69de29b..00000000 diff --git a/client/src/theme.css b/client/src/theme.css new file mode 100644 index 00000000..e8dd6711 --- /dev/null +++ b/client/src/theme.css @@ -0,0 +1,52 @@ +* { + margin: 0; + padding: 0; +} + +:root { + text-align: center; + background-color: #111928; + color: #E2EFEE; + font-family: "Noto Kufi Arabic"; +} + +h1 { + font-size: 2.98625rem; + font-weight: 800; + line-height: 139.429%; /* 4.16369rem */ +} + +h2 { + font-size: 2.48813rem; + font-weight: 700; +} + +h3 { + font-size: 2.07375rem; + font-weight: 700; +} + +h4 { + font-size: 1.72813rem; + font-weight: 600; +} + +h5 { + font-size: 1.44rem; + font-weight: 600; +} + +h6 { + font-size: 1.2rem; + font-weight: 500; +} + +p, label { + font-size: 1rem; + font-weight: 500; +} + +small { + font-size: 0.83313rem; + font-weight: 400; +}/*# sourceMappingURL=theme.css.map */ \ No newline at end of file diff --git a/client/src/theme.scss b/client/src/theme.scss index 0199cb61..9c654028 100644 --- a/client/src/theme.scss +++ b/client/src/theme.scss @@ -1,27 +1,56 @@ +$text-color: #E2EFEE; +$grey-900: #111928; +$font: 'Noto Kufi Arabic'; + +* { + margin: 0; + padding: 0; +} + :root { text-align: center; - font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; + background-color: $grey-900; + color: $text-color; + font-family: $font; } -// TODO: Delete later this is just a sanity check + +// Fonts h1 { - animation: wiggle 1s infinite; - margin-top: 45vh; -} - -@keyframes wiggle { - 0% { - transform: rotate(0deg); - } - 25% { - transform: rotate(5deg); - } - 50% { - transform: rotate(0deg); - } - 75% { - transform: rotate(-5deg); - } - 100% { - transform: rotate(0deg); - } + font-size: 2.98625rem; + font-weight: 800; + line-height: 139.429%; /* 4.16369rem */ +} +h2 { + font-size: 2.48813rem; + font-weight: 700; +} + +h3 { + font-size: 2.07375rem; + font-weight: 700; +} + +h4 { + font-size: 1.72813rem; + font-weight: 600; +} + +h5 { + font-size: 1.44rem; + font-weight: 600; +} + +h6 { + font-size: 1.2rem; + font-weight: 500; +} + +p, label { + font-size: 1rem; + font-weight: 500; +} + +small { + font-size: 0.83313rem; + font-weight: 400; } From f60bc2f6373f39ca5e18cb08bb9a800cd6bfe273 Mon Sep 17 00:00:00 2001 From: AhmedHamed3699 Date: Fri, 15 Dec 2023 18:32:07 +0200 Subject: [PATCH 2/8] =?UTF-8?q?=E2=9C=A8=20feat:=20LogIn=20page?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client/src/App.jsx | 3 +- client/src/assets/styles/components/logIn.css | 64 ++++++++++++++++ .../src/assets/styles/components/logIn.scss | 74 +++++++++++++++++++ .../src/assets/styles/components/signUp.css | 1 + .../src/assets/styles/components/signUp.scss | 1 + .../src/components/module_OR_PAGE/logIn.jsx | 21 ++++++ 6 files changed, 163 insertions(+), 1 deletion(-) create mode 100644 client/src/assets/styles/components/logIn.css create mode 100644 client/src/assets/styles/components/logIn.scss diff --git a/client/src/App.jsx b/client/src/App.jsx index 031e195d..adb716ed 100644 --- a/client/src/App.jsx +++ b/client/src/App.jsx @@ -1,10 +1,11 @@ import "./theme.scss"; import SignUp from "./components/module_OR_PAGE/signUp"; +import LogIn from "./components/module_OR_PAGE/logIn"; function App() { return ( <> - + ); } diff --git a/client/src/assets/styles/components/logIn.css b/client/src/assets/styles/components/logIn.css new file mode 100644 index 00000000..133f3432 --- /dev/null +++ b/client/src/assets/styles/components/logIn.css @@ -0,0 +1,64 @@ +.hero { + display: flex; + width: 22.5rem; + height: 40rem; + flex-direction: column; + align-items: center; + justify-content: center; + margin: auto; + padding: 1.75rem 0.25rem; + gap: 1.1875rem; +} + +.card { + background-color: #1f2a37; + border-radius: 1.875rem; + display: flex; + width: 16.9375rem; + padding: 1.0625rem 0rem; + margin-top: 3rem; + flex-direction: column; + justify-content: center; + align-items: center; + gap: 0.875rem; +} +.card .input-field { + display: flex; + width: 14.16319rem; + padding: 0.125rem 0.75rem; + justify-content: flex-end; + align-items: center; + align-content: center; + gap: 0rem 9.25rem; + flex-wrap: wrap; +} +.card .input-field input { + background: #374151; + margin-block: 0.3rem; + border: 1px solid #4b5563; + border-radius: 0.25rem; + width: 12.66319rem; + height: 1.75rem; + flex-shrink: 0; +} + +.no-account { + color: #0694A2; + font-family: "Noto Kufi Arabic"; + margin-bottom: 1rem; +} + +button { + color: #e2efee; + font-family: "Noto Kufi Arabic"; + background: #046C4E; + border: 0px solid; + border-radius: 0.3125rem; + display: flex; + width: 7.25rem; + height: 1.875rem; + padding: 0.8125rem 0.625rem; + justify-content: center; + align-items: center; + gap: 1.25rem; +}/*# sourceMappingURL=logIn.css.map */ \ No newline at end of file diff --git a/client/src/assets/styles/components/logIn.scss b/client/src/assets/styles/components/logIn.scss new file mode 100644 index 00000000..7b4053b1 --- /dev/null +++ b/client/src/assets/styles/components/logIn.scss @@ -0,0 +1,74 @@ +$Teal-900: #014451; +$Teal-500: #0694A2; +$grey-800: #1f2a37; +$grey-700: #374151; +$grey-600: #4b5563; +$green-700: #046C4E; +$Primary-900: #0e0218; +$text-color: #e2efee; +$font: 'Noto Kufi Arabic'; + +.hero { + display: flex; + width: 22.5rem; + height: 40rem; + flex-direction: column; + align-items: center; + justify-content: center; + margin: auto; + padding: 1.75rem 0.25rem; + gap: 1.1875rem; +} + +.card { + background-color: $grey-800; + border-radius: 1.875rem; + display: flex; + width: 16.9375rem; + padding: 1.0625rem 0rem; + margin-top: 3rem; + flex-direction: column; + justify-content: center; + align-items: center; + gap: 0.875rem; + .input-field { + display: flex; + width: 14.16319rem; + padding: 0.125rem 0.75rem; + justify-content: flex-end; + align-items: center; + align-content: center; + gap: 0rem 9.25rem; + flex-wrap: wrap; + input { + background: $grey-700; + margin-block: 0.3rem; + border: 1px solid $grey-600; + border-radius: 0.25rem; + width: 12.66319rem; + height: 1.75rem; + flex-shrink: 0; + } + } +} + +.no-account { + color: $Teal-500; + font-family: $font; + margin-bottom: 1rem; +} + +button { + color: $text-color; + font-family: $font; + background: $green-700; + border: 0px solid; + border-radius: 0.3125rem; + display: flex; + width: 7.25rem; + height: 1.875rem; + padding: 0.8125rem 0.625rem; + justify-content: center; + align-items: center; + gap: 1.25rem; +} diff --git a/client/src/assets/styles/components/signUp.css b/client/src/assets/styles/components/signUp.css index bde59f00..e40032e3 100644 --- a/client/src/assets/styles/components/signUp.css +++ b/client/src/assets/styles/components/signUp.css @@ -75,6 +75,7 @@ button { color: #e2efee; font-family: "Noto Kufi Arabic"; background: #0e0218; + border: 0px solid; border-radius: 0.3125rem; display: flex; width: 12.5rem; diff --git a/client/src/assets/styles/components/signUp.scss b/client/src/assets/styles/components/signUp.scss index 3c32efc8..9705ee79 100644 --- a/client/src/assets/styles/components/signUp.scss +++ b/client/src/assets/styles/components/signUp.scss @@ -85,6 +85,7 @@ button { color: $text-color; font-family: $font; background: $Primary-900; + border: 0px solid; border-radius: 0.3125rem; display: flex; width: 12.5rem; diff --git a/client/src/components/module_OR_PAGE/logIn.jsx b/client/src/components/module_OR_PAGE/logIn.jsx index e69de29b..65533d91 100644 --- a/client/src/components/module_OR_PAGE/logIn.jsx +++ b/client/src/components/module_OR_PAGE/logIn.jsx @@ -0,0 +1,21 @@ +import '../../assets/styles/components/logIn.css' + +export default function LogIn() { + return ( +
+

تسجيل الدخول

+
+ + + +
+ ليس لديك حساب؟ +
+ ) +} From d7cac10d00482bb0cb88e1f8d9b5e875443a8afe Mon Sep 17 00:00:00 2001 From: AhmedHamed3699 Date: Fri, 15 Dec 2023 20:40:19 +0200 Subject: [PATCH 3/8] =?UTF-8?q?=F0=9F=97=83=EF=B8=8F=20feat:=20connect=20t?= =?UTF-8?q?o=20database=20and=20set=20environment=20variables?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/.env.example | 12 ++++++++++ server/app.js | 36 +++++++++++++++++++++++++++++ server/database/createDatabase.psql | 4 ++-- 3 files changed, 50 insertions(+), 2 deletions(-) diff --git a/server/.env.example b/server/.env.example index e69de29b..57d26084 100644 --- a/server/.env.example +++ b/server/.env.example @@ -0,0 +1,12 @@ +DB_HOST= +DB_PORT= +DB_USER= +DB_PASS= +DB_DATABASE= + +########## Example ########## +# DB_HOST=localhost +# DB_PORT=5432 +# DB_USER=postgres +# DB_PASS=******** +# DB_DATABASE=scoutsManagementSystem \ No newline at end of file diff --git a/server/app.js b/server/app.js index e69de29b..ad02be8d 100644 --- a/server/app.js +++ b/server/app.js @@ -0,0 +1,36 @@ +const dotenv = require('dotenv') +const express = require('express') +const cors = require('cors') +const pg = require('pg') + +dotenv.config() +const PORT = process.env.PORT || 3000 +const sever = new pg.Pool({ + host: process.env.DB_HOST, + port: process.env.DB_PORT, + user: process.env.DB_USER, + password: process.env.DB_PASS, + database: process.env.DB_DATABASE, +}) + +sever + .connect() + .then(() => { + console.log('Database is connected') + }) + .catch((err) => { + if (err) return console.error(err) + }) + +const app = express() +app.use(cors()) +app.use(express.json()) + +app.get('/', (req, res) => { + res.send('Hello World') +}) + +app.listen(PORT, (err) => { + if (err) return console.error(err) + console.log(`Server started listening at port ${PORT}`) +}) diff --git a/server/database/createDatabase.psql b/server/database/createDatabase.psql index e9e99f36..d79c0781 100644 --- a/server/database/createDatabase.psql +++ b/server/database/createDatabase.psql @@ -1089,7 +1089,7 @@ ALTER TABLE ONLY public."ScoutProfile" -- ALTER TABLE ONLY public."Scout" - ADD CONSTRAINT "scout_sector_FK" FOREIGN KEY ("sectorBaseName", "sectorBaseName") REFERENCES public."Sector"("suffixName", "baseName") ON UPDATE CASCADE ON DELETE SET NULL NOT VALID; + ADD CONSTRAINT "scout_sector_FK" FOREIGN KEY ("sectorSuffixName", "sectorBaseName") REFERENCES public."Sector"("suffixName", "baseName") ON UPDATE CASCADE ON DELETE SET NULL NOT VALID; -- @@ -1150,4 +1150,4 @@ ALTER TABLE ONLY public."Week" -- -- PostgreSQL database dump complete --- +-- \ No newline at end of file From 83cdf10d8fe1d34f8e585b3fac0c94d2046cf009 Mon Sep 17 00:00:00 2001 From: AhmedHamed3699 Date: Sat, 16 Dec 2023 05:23:31 +0200 Subject: [PATCH 4/8] =?UTF-8?q?=E2=9C=A8=20feat:=20Add=20authController?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/app.js | 22 +--- server/controllers/auth.controller.js | 121 ++++++++++++++++++ server/database/createDatabase.psql | 4 +- server/database/db.js | 13 ++ .../auth.middleware.js} | 0 server/middlewares/example.middleware.js | 0 server/models/example.model.js | 0 server/package-lock.json | 6 + server/package.json | 1 + server/routes/auth.route.js | 9 ++ server/routes/example.route.js | 0 server/utils/convert.js | 20 +++ server/utils/fileExample.js | 0 13 files changed, 179 insertions(+), 17 deletions(-) create mode 100644 server/controllers/auth.controller.js create mode 100644 server/database/db.js rename server/{controllers/example.controller.js => middlewares/auth.middleware.js} (100%) delete mode 100644 server/middlewares/example.middleware.js delete mode 100644 server/models/example.model.js create mode 100644 server/routes/auth.route.js delete mode 100644 server/routes/example.route.js create mode 100644 server/utils/convert.js delete mode 100644 server/utils/fileExample.js diff --git a/server/app.js b/server/app.js index ad02be8d..1220007b 100644 --- a/server/app.js +++ b/server/app.js @@ -1,20 +1,11 @@ -const dotenv = require('dotenv') const express = require('express') const cors = require('cors') -const pg = require('pg') - -dotenv.config() +const db = require('./database/db') +const app = express() const PORT = process.env.PORT || 3000 -const sever = new pg.Pool({ - host: process.env.DB_HOST, - port: process.env.DB_PORT, - user: process.env.DB_USER, - password: process.env.DB_PASS, - database: process.env.DB_DATABASE, -}) +const authRouter = require('./routes/auth.route') -sever - .connect() +db.connect() .then(() => { console.log('Database is connected') }) @@ -22,10 +13,11 @@ sever if (err) return console.error(err) }) -const app = express() app.use(cors()) app.use(express.json()) +app.use("/auth", authRouter); + app.get('/', (req, res) => { res.send('Hello World') }) @@ -33,4 +25,4 @@ app.get('/', (req, res) => { app.listen(PORT, (err) => { if (err) return console.error(err) console.log(`Server started listening at port ${PORT}`) -}) +}) \ No newline at end of file diff --git a/server/controllers/auth.controller.js b/server/controllers/auth.controller.js new file mode 100644 index 00000000..8d52d2b9 --- /dev/null +++ b/server/controllers/auth.controller.js @@ -0,0 +1,121 @@ +const bcrypt = require('bcryptjs') +//const jwt = require('jsonwebtoken') +const db = require('../database/db') +const { jsonToArray, arrayToJson } = require('../utils/convert') + +const authController = { + signup: async (req, res) => { + try { + // Deconstruct the request body + console.log(req.body) + const { + firstName, + middleName, + lastName, + email, + password, + phoneNumber, + gender, + } = req.body + + // Check if email already exists + /* + const captain = await db.query('CALL getCaptain($1)', [email]) + if (captain) { + console.log('Email is taken!!') + return res.status(400).json({ error: 'Email is taken!!' }) + } + */ + // Hash the password + const hashedPassword = await bcrypt.hash(password, 10) + + // Create a new Captain + req.body = { ...req.body, password: hashedPassword } + const params = jsonToArray(req.body) + console.log(params) + let newCaptain = await db.query( + 'INSERT INTO "Captain"("firstName", "middleName", "lastName", "email", "password", "phoneNumber", "gender", "type") ' + + 'VALUES($1, $2, $3, $4, $5, $6, $7, "regular")', + params + ) + + // Generate a JWT token containing the user's id + /* + const token = jwt.sign( + { id: newCaptain.captainId }, + process.env.JWT_SECRET, + { + expiresIn: process.env.JWT_EXPIRES_IN, + } + ) + */ + + res.status(201).json({ + message: 'Captain created successfully', + newCaptain, + //token, + }) + } catch (error) { + console.log(error) + res.status(500).json({ + error: 'An error occurred while creating a new captain!!', + }) + } + }, + /* + login: async (req, res) => { + try { + // Deconstruct the request body + const { email, password } = req.body + + // Check if the user exists + const user = await User.findOne({ email }, '+password') + if (!user) { + return res.status(400).json({ + error: 'Invalid email', + }) + } + // Check if the password is correct + const isValidPassword = await bcrypt.compare( + password, + user.password + ) + if (!isValidPassword) { + return res.status(400).json({ + error: 'Invalid password', + }) + } + + // Generate a JWT token containing the user's id + const token = jwt.sign({ id: user._id }, process.env.JWT_SECRET, { + expiresIn: process.env.JWT_EXPIRES_IN, + }) + console.log(user) + const image = user.image + res.status(200).json({ + message: 'Logged in successfully', + token, + image, + }) + } catch (error) { + console.log(error) + res.status(500).json({ + error: 'An error occurred while logging you in. Please try again.', + }) + } + }, + */ + + // This controller is responsible for fetching data of the logged-in captain + me: (req, res) => { + try { + res.status(200).json({ user: req.user }) + } catch (error) { + console.log(error) + res.status(500).json({ + error: 'An error occurred while fetching data.', + }) + } + }, +} +module.exports = authController diff --git a/server/database/createDatabase.psql b/server/database/createDatabase.psql index d79c0781..dcc0f22d 100644 --- a/server/database/createDatabase.psql +++ b/server/database/createDatabase.psql @@ -245,8 +245,8 @@ CREATE TABLE public."Captain" ( "middleName" character varying(255) NOT NULL, "lastName" character varying(255) NOT NULL, "phoneNumber" character varying(255) NOT NULL, - email character varying(255) NOT NULL, - passward character varying(255) NOT NULL, + "email" character varying(255) NOT NULL, + "password" character varying(255) NOT NULL, "rSectorBaseName" character varying(255), "rSectorSuffixName" character varying(255), gender public."Gender" NOT NULL, diff --git a/server/database/db.js b/server/database/db.js new file mode 100644 index 00000000..bacd5a53 --- /dev/null +++ b/server/database/db.js @@ -0,0 +1,13 @@ +const pg = require('pg') +const dotenv = require('dotenv') + +dotenv.config() +const db = new pg.Pool({ + host: process.env.DB_HOST, + port: process.env.DB_PORT, + user: process.env.DB_USER, + password: process.env.DB_PASS, + database: process.env.DB_DATABASE, +}) + +module.exports = db \ No newline at end of file diff --git a/server/controllers/example.controller.js b/server/middlewares/auth.middleware.js similarity index 100% rename from server/controllers/example.controller.js rename to server/middlewares/auth.middleware.js diff --git a/server/middlewares/example.middleware.js b/server/middlewares/example.middleware.js deleted file mode 100644 index e69de29b..00000000 diff --git a/server/models/example.model.js b/server/models/example.model.js deleted file mode 100644 index e69de29b..00000000 diff --git a/server/package-lock.json b/server/package-lock.json index 11ca95fd..ed9efe0c 100644 --- a/server/package-lock.json +++ b/server/package-lock.json @@ -9,6 +9,7 @@ "version": "1.0.0", "license": "ISC", "dependencies": { + "bcryptjs": "^2.4.3", "cors": "^2.8.5", "dotenv": "^16.3.1", "express": "^4.18.2", @@ -60,6 +61,11 @@ "integrity": "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==", "dev": true }, + "node_modules/bcryptjs": { + "version": "2.4.3", + "resolved": "https://registry.npmjs.org/bcryptjs/-/bcryptjs-2.4.3.tgz", + "integrity": "sha512-V/Hy/X9Vt7f3BbPJEi8BdVFMByHi+jNXrYkW3huaybV/kQ0KJg0Y6PkEMbn+zeT+i+SiKZ/HMqJGIIt4LZDqNQ==" + }, "node_modules/binary-extensions": { "version": "2.2.0", "resolved": "https://registry.npmjs.org/binary-extensions/-/binary-extensions-2.2.0.tgz", diff --git a/server/package.json b/server/package.json index 5c476c07..cc2763ca 100644 --- a/server/package.json +++ b/server/package.json @@ -10,6 +10,7 @@ "author": "", "license": "ISC", "dependencies": { + "bcryptjs": "^2.4.3", "cors": "^2.8.5", "dotenv": "^16.3.1", "express": "^4.18.2", diff --git a/server/routes/auth.route.js b/server/routes/auth.route.js new file mode 100644 index 00000000..f4c293ad --- /dev/null +++ b/server/routes/auth.route.js @@ -0,0 +1,9 @@ +const authRouter = require("express").Router(); +const authController = require("../controllers/auth.controller"); +//const authMiddleware = require("../middlewares/auth.middleware"); + +authRouter.post("/signUp", authController.signup); +//authRouter.post("/logIn", authController.login); +//authRouter.get("/me", authMiddleware, authController.me); + +module.exports = authRouter; \ No newline at end of file diff --git a/server/routes/example.route.js b/server/routes/example.route.js deleted file mode 100644 index e69de29b..00000000 diff --git a/server/utils/convert.js b/server/utils/convert.js new file mode 100644 index 00000000..69ac8ce7 --- /dev/null +++ b/server/utils/convert.js @@ -0,0 +1,20 @@ +const jsonToArray = (json) => { + const arr = [] + for (const key in json) { + arr.push(json[key]) + } + return arr +} + +const arrayToJson = (arr) => { + const json = {} + for (const key in arr) { + json[key] = arr[key] + } + return json +} + +module.exports = { + jsonToArray, + arrayToJson, +} diff --git a/server/utils/fileExample.js b/server/utils/fileExample.js deleted file mode 100644 index e69de29b..00000000 From c17a91f1d06c54b34537a520aab388e7137328cc Mon Sep 17 00:00:00 2001 From: AhmedHamed3699 Date: Sat, 16 Dec 2023 19:07:27 +0200 Subject: [PATCH 5/8] =?UTF-8?q?=F0=9F=94=A7=20chore:=20update=20environmen?= =?UTF-8?q?t=20example=20file=20&=20add=20new=20dependencies?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/.env.example | 12 ++++- server/package-lock.json | 99 ++++++++++++++++++++++++++++++++++++++-- server/package.json | 1 + 3 files changed, 107 insertions(+), 5 deletions(-) diff --git a/server/.env.example b/server/.env.example index 57d26084..59dfe3ad 100644 --- a/server/.env.example +++ b/server/.env.example @@ -1,12 +1,22 @@ +# DB Config Variables DB_HOST= DB_PORT= DB_USER= DB_PASS= DB_DATABASE= +# Token Config Variables +JWT_SECRET= +JWT_EXPIRES_IN= + ########## Example ########## # DB_HOST=localhost # DB_PORT=5432 # DB_USER=postgres # DB_PASS=******** -# DB_DATABASE=scoutsManagementSystem \ No newline at end of file +# DB_DATABASE=scoutsManagementSystem + +# To get a random string for JWT_SECRET +# Run This in terminal => node -e "console.log(require('crypto').randomBytes(32).toString('hex'))" + +# JWT_EXPIRES_IN='48h' \ No newline at end of file diff --git a/server/package-lock.json b/server/package-lock.json index ed9efe0c..a6128cfd 100644 --- a/server/package-lock.json +++ b/server/package-lock.json @@ -13,6 +13,7 @@ "cors": "^2.8.5", "dotenv": "^16.3.1", "express": "^4.18.2", + "jsonwebtoken": "^9.0.2", "pg": "^8.11.3" }, "devDependencies": { @@ -120,6 +121,11 @@ "node": ">=8" } }, + "node_modules/buffer-equal-constant-time": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/buffer-equal-constant-time/-/buffer-equal-constant-time-1.0.1.tgz", + "integrity": "sha512-zRpUiDwd/xk6ADqPMATG8vc9VPrkck7T07OIx0gnjmJAnHnTVXNQG3vfvWNuiZIkwu9KrKdA1iJKfsfTVxE6NA==" + }, "node_modules/buffer-writer": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/buffer-writer/-/buffer-writer-2.0.0.tgz", @@ -275,6 +281,14 @@ "url": "https://github.com/motdotla/dotenv?sponsor=1" } }, + "node_modules/ecdsa-sig-formatter": { + "version": "1.0.11", + "resolved": "https://registry.npmjs.org/ecdsa-sig-formatter/-/ecdsa-sig-formatter-1.0.11.tgz", + "integrity": "sha512-nagl3RYrbNv6kQkeJIpt6NJZy8twLB/2vtz6yN9Z4vRKHN4/QZJIEbqohALSgwKdnksuY3k5Addp5lg8sVoVcQ==", + "dependencies": { + "safe-buffer": "^5.0.1" + } + }, "node_modules/ee-first": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz", @@ -586,11 +600,90 @@ "node": ">=0.12.0" } }, + "node_modules/jsonwebtoken": { + "version": "9.0.2", + "resolved": "https://registry.npmjs.org/jsonwebtoken/-/jsonwebtoken-9.0.2.tgz", + "integrity": "sha512-PRp66vJ865SSqOlgqS8hujT5U4AOgMfhrwYIuIhfKaoSCZcirrmASQr8CX7cUg+RMih+hgznrjp99o+W4pJLHQ==", + "dependencies": { + "jws": "^3.2.2", + "lodash.includes": "^4.3.0", + "lodash.isboolean": "^3.0.3", + "lodash.isinteger": "^4.0.4", + "lodash.isnumber": "^3.0.3", + "lodash.isplainobject": "^4.0.6", + "lodash.isstring": "^4.0.1", + "lodash.once": "^4.0.0", + "ms": "^2.1.1", + "semver": "^7.5.4" + }, + "engines": { + "node": ">=12", + "npm": ">=6" + } + }, + "node_modules/jsonwebtoken/node_modules/ms": { + "version": "2.1.3", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz", + "integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==" + }, + "node_modules/jwa": { + "version": "1.4.1", + "resolved": "https://registry.npmjs.org/jwa/-/jwa-1.4.1.tgz", + "integrity": "sha512-qiLX/xhEEFKUAJ6FiBMbes3w9ATzyk5W7Hvzpa/SLYdxNtng+gcurvrI7TbACjIXlsJyr05/S1oUhZrc63evQA==", + "dependencies": { + "buffer-equal-constant-time": "1.0.1", + "ecdsa-sig-formatter": "1.0.11", + "safe-buffer": "^5.0.1" + } + }, + "node_modules/jws": { + "version": "3.2.2", + "resolved": "https://registry.npmjs.org/jws/-/jws-3.2.2.tgz", + "integrity": "sha512-YHlZCB6lMTllWDtSPHz/ZXTsi8S00usEV6v1tjq8tOUZzw7DpSDWVXjXDre6ed1w/pd495ODpHZYSdkRTsa0HA==", + "dependencies": { + "jwa": "^1.4.1", + "safe-buffer": "^5.0.1" + } + }, + "node_modules/lodash.includes": { + "version": "4.3.0", + "resolved": "https://registry.npmjs.org/lodash.includes/-/lodash.includes-4.3.0.tgz", + "integrity": "sha512-W3Bx6mdkRTGtlJISOvVD/lbqjTlPPUDTMnlXZFnVwi9NKJ6tiAk6LVdlhZMm17VZisqhKcgzpO5Wz91PCt5b0w==" + }, + "node_modules/lodash.isboolean": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/lodash.isboolean/-/lodash.isboolean-3.0.3.tgz", + "integrity": "sha512-Bz5mupy2SVbPHURB98VAcw+aHh4vRV5IPNhILUCsOzRmsTmSQ17jIuqopAentWoehktxGd9e/hbIXq980/1QJg==" + }, + "node_modules/lodash.isinteger": { + "version": "4.0.4", + "resolved": "https://registry.npmjs.org/lodash.isinteger/-/lodash.isinteger-4.0.4.tgz", + "integrity": "sha512-DBwtEWN2caHQ9/imiNeEA5ys1JoRtRfY3d7V9wkqtbycnAmTvRRmbHKDV4a0EYc678/dia0jrte4tjYwVBaZUA==" + }, + "node_modules/lodash.isnumber": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/lodash.isnumber/-/lodash.isnumber-3.0.3.tgz", + "integrity": "sha512-QYqzpfwO3/CWf3XP+Z+tkQsfaLL/EnUlXWVkIk5FUPc4sBdTehEqZONuyRt2P67PXAk+NXmTBcc97zw9t1FQrw==" + }, + "node_modules/lodash.isplainobject": { + "version": "4.0.6", + "resolved": "https://registry.npmjs.org/lodash.isplainobject/-/lodash.isplainobject-4.0.6.tgz", + "integrity": "sha512-oSXzaWypCMHkPC3NvBEaPHf0KsA5mvPrOPgQWDsbg8n7orZ290M0BmC/jgRZ4vcJ6DTAhjrsSYgdsW/F+MFOBA==" + }, + "node_modules/lodash.isstring": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/lodash.isstring/-/lodash.isstring-4.0.1.tgz", + "integrity": "sha512-0wJxfxH1wgO3GrbuP+dTTk7op+6L41QCXbGINEmD+ny/G/eCqGzxyCsh7159S+mgDDcoarnBw6PC1PS5+wUGgw==" + }, + "node_modules/lodash.once": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/lodash.once/-/lodash.once-4.1.1.tgz", + "integrity": "sha512-Sb487aTOCr9drQVL8pIxOzVhafOjZN9UU54hiN8PU3uAiSV7lx1yYNpbNmex2PK6dSJoNTSJUUswT651yww3Mg==" + }, "node_modules/lru-cache": { "version": "6.0.0", "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-6.0.0.tgz", "integrity": "sha512-Jo6dJ04CmSjuznwJSS3pUeWmd/H0ffTlkXXgwZi+eq1UCmqQwCh+eLsYOYCwY991i2Fah4h1BEMCx4qThGbsiA==", - "dev": true, "dependencies": { "yallist": "^4.0.0" }, @@ -1018,7 +1111,6 @@ "version": "7.5.4", "resolved": "https://registry.npmjs.org/semver/-/semver-7.5.4.tgz", "integrity": "sha512-1bCSESV6Pv+i21Hvpxp3Dx+pSD8lIPt8uVjRrxAUt/nbswYc+tK6Y2btiULjd4+fnq15PX+nqQDC7Oft7WkwcA==", - "dev": true, "dependencies": { "lru-cache": "^6.0.0" }, @@ -1228,8 +1320,7 @@ "node_modules/yallist": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/yallist/-/yallist-4.0.0.tgz", - "integrity": "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==", - "dev": true + "integrity": "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==" } } } diff --git a/server/package.json b/server/package.json index cc2763ca..9cac5295 100644 --- a/server/package.json +++ b/server/package.json @@ -14,6 +14,7 @@ "cors": "^2.8.5", "dotenv": "^16.3.1", "express": "^4.18.2", + "jsonwebtoken": "^9.0.2", "pg": "^8.11.3" }, "devDependencies": { From b15b5a438fef011985505429640ad91ec6bf0bbd Mon Sep 17 00:00:00 2001 From: AhmedHamed3699 Date: Sat, 16 Dec 2023 19:08:47 +0200 Subject: [PATCH 6/8] =?UTF-8?q?=F0=9F=9B=82=20feat:=20done=20with=20signUp?= =?UTF-8?q?=20controller?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/controllers/auth.controller.js | 34 ++++++++++++++------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/server/controllers/auth.controller.js b/server/controllers/auth.controller.js index 8d52d2b9..da36e09e 100644 --- a/server/controllers/auth.controller.js +++ b/server/controllers/auth.controller.js @@ -1,5 +1,5 @@ const bcrypt = require('bcryptjs') -//const jwt = require('jsonwebtoken') +const jwt = require('jsonwebtoken') const db = require('../database/db') const { jsonToArray, arrayToJson } = require('../utils/convert') @@ -7,7 +7,6 @@ const authController = { signup: async (req, res) => { try { // Deconstruct the request body - console.log(req.body) const { firstName, middleName, @@ -19,28 +18,32 @@ const authController = { } = req.body // Check if email already exists - /* - const captain = await db.query('CALL getCaptain($1)', [email]) - if (captain) { + const captain = await db.query( + `SELECT "email", "password" + FROM "Captain" + WHERE "email" = $1;`, + [email] + ) + if (captain.rows.length) { console.log('Email is taken!!') return res.status(400).json({ error: 'Email is taken!!' }) } - */ + // Hash the password const hashedPassword = await bcrypt.hash(password, 10) // Create a new Captain req.body = { ...req.body, password: hashedPassword } const params = jsonToArray(req.body) - console.log(params) - let newCaptain = await db.query( - 'INSERT INTO "Captain"("firstName", "middleName", "lastName", "email", "password", "phoneNumber", "gender", "type") ' + - 'VALUES($1, $2, $3, $4, $5, $6, $7, "regular")', - params + const result = await db.query( + `INSERT INTO "Captain"("firstName", "middleName", "lastName", "email", "password", "phoneNumber", "gender", "type") + VALUES($1, $2, $3, $4, $5, $6, $7, $8) RETURNING *;`, + params.concat(['regular']) ) + const newCaptain = result.rows[0] - // Generate a JWT token containing the user's id - /* + // Generate a JWT token containing the captain's id + // Bearer token is the token that we will send to the client const token = jwt.sign( { id: newCaptain.captainId }, process.env.JWT_SECRET, @@ -48,15 +51,14 @@ const authController = { expiresIn: process.env.JWT_EXPIRES_IN, } ) - */ res.status(201).json({ message: 'Captain created successfully', newCaptain, - //token, + token, }) } catch (error) { - console.log(error) + console.log(error.detail) res.status(500).json({ error: 'An error occurred while creating a new captain!!', }) From 3d91ae3826f653edc2671193213398c959031ad2 Mon Sep 17 00:00:00 2001 From: AhmedHamed3699 Date: Sat, 16 Dec 2023 19:42:51 +0200 Subject: [PATCH 7/8] =?UTF-8?q?=F0=9F=9B=82=20feat:=20Add=20Log=20In=20aut?= =?UTF-8?q?hentication?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/app.js | 5 +-- server/controllers/auth.controller.js | 63 ++++++++++++++------------- server/routes/auth.route.js | 2 +- 3 files changed, 34 insertions(+), 36 deletions(-) diff --git a/server/app.js b/server/app.js index 1220007b..622cdf5b 100644 --- a/server/app.js +++ b/server/app.js @@ -16,12 +16,9 @@ db.connect() app.use(cors()) app.use(express.json()) +// Routes app.use("/auth", authRouter); -app.get('/', (req, res) => { - res.send('Hello World') -}) - app.listen(PORT, (err) => { if (err) return console.error(err) console.log(`Server started listening at port ${PORT}`) diff --git a/server/controllers/auth.controller.js b/server/controllers/auth.controller.js index da36e09e..d0af96c3 100644 --- a/server/controllers/auth.controller.js +++ b/server/controllers/auth.controller.js @@ -7,15 +7,7 @@ const authController = { signup: async (req, res) => { try { // Deconstruct the request body - const { - firstName, - middleName, - lastName, - email, - password, - phoneNumber, - gender, - } = req.body + const { email, password } = req.body // Check if email already exists const captain = await db.query( @@ -25,7 +17,6 @@ const authController = { [email] ) if (captain.rows.length) { - console.log('Email is taken!!') return res.status(400).json({ error: 'Email is taken!!' }) } @@ -52,6 +43,7 @@ const authController = { } ) + // Send the response res.status(201).json({ message: 'Captain created successfully', newCaptain, @@ -64,56 +56,65 @@ const authController = { }) } }, - /* + login: async (req, res) => { try { // Deconstruct the request body const { email, password } = req.body - // Check if the user exists - const user = await User.findOne({ email }, '+password') - if (!user) { + // Check if email already exists + const result = await db.query( + `SELECT "email", "password" + FROM "Captain" + WHERE "email" = $1;`, + [email] + ) + if (!result.rows.length) { return res.status(400).json({ error: 'Invalid email', }) } + + // Get Captain's data + const captain = result.rows[0] + // Check if the password is correct - const isValidPassword = await bcrypt.compare( - password, - user.password - ) - if (!isValidPassword) { + const isCorrect = await bcrypt.compare(password, captain.password) + if (!isCorrect) { return res.status(400).json({ error: 'Invalid password', }) } - // Generate a JWT token containing the user's id - const token = jwt.sign({ id: user._id }, process.env.JWT_SECRET, { - expiresIn: process.env.JWT_EXPIRES_IN, - }) - console.log(user) - const image = user.image + // Generate a JWT token containing the captain's id + // Bearer token is the token that we will send to the client + const token = jwt.sign( + { id: captain.captainId }, + process.env.JWT_SECRET, + { + expiresIn: process.env.JWT_EXPIRES_IN, + } + ) + + // Send the response res.status(200).json({ message: 'Logged in successfully', token, - image, }) } catch (error) { - console.log(error) + console.log(error.detail) res.status(500).json({ - error: 'An error occurred while logging you in. Please try again.', + error: 'An error occurred while logging you in', }) } }, - */ // This controller is responsible for fetching data of the logged-in captain me: (req, res) => { try { - res.status(200).json({ user: req.user }) + res.status(200).json({ user: req.captain }) } catch (error) { - console.log(error) + console.log(error.detail) res.status(500).json({ error: 'An error occurred while fetching data.', }) diff --git a/server/routes/auth.route.js b/server/routes/auth.route.js index f4c293ad..e73a421f 100644 --- a/server/routes/auth.route.js +++ b/server/routes/auth.route.js @@ -3,7 +3,7 @@ const authController = require("../controllers/auth.controller"); //const authMiddleware = require("../middlewares/auth.middleware"); authRouter.post("/signUp", authController.signup); -//authRouter.post("/logIn", authController.login); +authRouter.post("/logIn", authController.login); //authRouter.get("/me", authMiddleware, authController.me); module.exports = authRouter; \ No newline at end of file From 4034ecab4a58a3e79001e59e91cbf1316fda4f68 Mon Sep 17 00:00:00 2001 From: AhmedHamed3699 Date: Sat, 16 Dec 2023 21:51:40 +0200 Subject: [PATCH 8/8] =?UTF-8?q?=F0=9F=9B=82=20feat:=20done=20with=20author?= =?UTF-8?q?ization=20of=20logged-In=20captain?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/controllers/auth.controller.js | 15 +++++---- server/middlewares/auth.middleware.js | 47 +++++++++++++++++++++++++++ server/routes/auth.route.js | 4 +-- 3 files changed, 57 insertions(+), 9 deletions(-) diff --git a/server/controllers/auth.controller.js b/server/controllers/auth.controller.js index d0af96c3..27840b52 100644 --- a/server/controllers/auth.controller.js +++ b/server/controllers/auth.controller.js @@ -6,8 +6,9 @@ const { jsonToArray, arrayToJson } = require('../utils/convert') const authController = { signup: async (req, res) => { try { - // Deconstruct the request body - const { email, password } = req.body + // get email and password from request body + const email = req.body['email'] + const password = req.body['password'] // Check if email already exists const captain = await db.query( @@ -36,7 +37,7 @@ const authController = { // Generate a JWT token containing the captain's id // Bearer token is the token that we will send to the client const token = jwt.sign( - { id: newCaptain.captainId }, + { id: newCaptain.captainId }, // Payload process.env.JWT_SECRET, { expiresIn: process.env.JWT_EXPIRES_IN, @@ -50,7 +51,7 @@ const authController = { token, }) } catch (error) { - console.log(error.detail) + console.log(error) res.status(500).json({ error: 'An error occurred while creating a new captain!!', }) @@ -89,7 +90,7 @@ const authController = { // Generate a JWT token containing the captain's id // Bearer token is the token that we will send to the client const token = jwt.sign( - { id: captain.captainId }, + { id: captain.captainId }, // Payload process.env.JWT_SECRET, { expiresIn: process.env.JWT_EXPIRES_IN, @@ -102,7 +103,7 @@ const authController = { token, }) } catch (error) { - console.log(error.detail) + console.log(error) res.status(500).json({ error: 'An error occurred while logging you in', }) @@ -114,7 +115,7 @@ const authController = { try { res.status(200).json({ user: req.captain }) } catch (error) { - console.log(error.detail) + console.log(error) res.status(500).json({ error: 'An error occurred while fetching data.', }) diff --git a/server/middlewares/auth.middleware.js b/server/middlewares/auth.middleware.js index e69de29b..ac5cbb34 100644 --- a/server/middlewares/auth.middleware.js +++ b/server/middlewares/auth.middleware.js @@ -0,0 +1,47 @@ +const jwt = require('jsonwebtoken') +const db = require('../database/db') + +const authMiddleware = async (req, res, next) => { + // Get authorization header and check if it exists + const auth = req.headers.authorization + if (!auth) { + return res.status(401).json({ error: 'No token provided' }) + } + + // Check if the authorization header is "Bearer " + if (!auth.startsWith('Bearer') || auth.split(' ').length !== 2) { + return res.status(401).json({ error: 'Invalid token, not Bearer' }) + } + + // Get token from the authorization header + const token = auth.split(' ')[1] + + try { + // Verify token and get captain's id + const id = jwt.verify(token, process.env.JWT_SECRET).id + + // Get captain's data + const result = await db.query( + `SELECT * + FROM "Captain" + WHERE "captainId" = $1;`, + [id] + ) + if (!result.rows.length) { + return res.status(404).json({ error: 'Captain not found' }) + } + + // Attach captain to the request object + req.captain = result.rows[0] + + next() + } catch (err) { + console.log(err) + if (err.name === 'TokenExpiredError') { + return res.status(401).json({ error: 'Provided token has expired' }) + } + res.status(401).json({ error: 'Invalid token' }) + } +} + +module.exports = authMiddleware diff --git a/server/routes/auth.route.js b/server/routes/auth.route.js index e73a421f..880e7482 100644 --- a/server/routes/auth.route.js +++ b/server/routes/auth.route.js @@ -1,9 +1,9 @@ const authRouter = require("express").Router(); const authController = require("../controllers/auth.controller"); -//const authMiddleware = require("../middlewares/auth.middleware"); +const authMiddleware = require("../middlewares/auth.middleware"); authRouter.post("/signUp", authController.signup); authRouter.post("/logIn", authController.login); -//authRouter.get("/me", authMiddleware, authController.me); +authRouter.get("/me", authMiddleware, authController.me); module.exports = authRouter; \ No newline at end of file