From 9205a3c0a1256c998321815111281e849a01e3f6 Mon Sep 17 00:00:00 2001 From: anaspacheco Date: Tue, 28 Nov 2023 13:33:00 -0500 Subject: [PATCH] removed login and added css to feedback --- back-end/app.js | 10 -- back-end/config/jwt-config.js | 38 ------- back-end/models/User.js | 63 ----------- back-end/package.json | 2 +- back-end/routes/authentication-routes.js | 106 ------------------ back-end/test/login.test.js | 49 -------- back-end/test/logout.test.js | 39 ------- front-end/src/App.js | 2 +- .../settings/FeedbackSupportPage.js | 7 +- front-end/src/css/settingsPage.css | 22 +++- 10 files changed, 28 insertions(+), 310 deletions(-) delete mode 100644 back-end/config/jwt-config.js delete mode 100644 back-end/models/User.js delete mode 100644 back-end/routes/authentication-routes.js delete mode 100644 back-end/test/login.test.js delete mode 100644 back-end/test/logout.test.js diff --git a/back-end/app.js b/back-end/app.js index 91c30bd..e4c991d 100644 --- a/back-end/app.js +++ b/back-end/app.js @@ -4,16 +4,8 @@ const app = express(); const cors = require("cors"); const morgan = require("morgan"); require("dotenv").config({ silent: true }); -const jwt = require("jsonwebtoken"); -const passport = require("passport"); - -const jwtStrategy = require("./config/jwt-config.js"); -passport.use(jwtStrategy); - -app.use(passport.initialize()); const mongoose = require("mongoose"); -const User = require("./models/User.js"); const Feedback = require("./models/Feedback.js"); // connect to the database @@ -32,10 +24,8 @@ app.use(express.urlencoded({ extended: true })); app.use(cors({ origin: process.env.FRONT_END_DOMAIN, credentials: true })); -const authenticationRoutes = require("./routes/authentication-routes.js"); const feedbackRoutes = require("./routes/feedback-routes.js"); -app.use("/auth", authenticationRoutes()); app.use("/feedback", feedbackRoutes()); diff --git a/back-end/config/jwt-config.js b/back-end/config/jwt-config.js deleted file mode 100644 index 528acb9..0000000 --- a/back-end/config/jwt-config.js +++ /dev/null @@ -1,38 +0,0 @@ -const mongoose = require("mongoose"); -const ObjectId = mongoose.Types.ObjectId; -const User = require("../models/User.js"); -const passportJWT = require("passport-jwt"); -const ExtractJwt = passportJWT.ExtractJwt; -const JwtStrategy = passportJWT.Strategy; -const dotenv = require("dotenv"); - -dotenv.config(); - -let jwtOptions = { - jwtFromRequest: ExtractJwt.fromAuthHeaderWithScheme("jwt"), - secretOrKey: process.env.JWT_SECRET, -}; - -const jwtVerifyToken = async function (jwt_payload, next) { - console.log("JWT payload received", jwt_payload); - - const expirationDate = new Date(jwt_payload.exp * 1000); - if (expirationDate < new Date()) { - return next(null, false, { message: "JWT token has expired." }); - } - - const userId = ObjectId(jwt_payload.id); - const user = await User.findOne({ _id: userId }).exec(); - if (user) { - next(null, user); - } else { - next(null, false, { message: "User not found" }); - } -}; - -const jwtStrategy = (jwtOptions) => { - const strategy = new JwtStrategy(jwtOptions, jwtVerifyToken); - return strategy; -}; - -module.exports = jwtStrategy(jwtOptions, jwtVerifyToken); diff --git a/back-end/models/User.js b/back-end/models/User.js deleted file mode 100644 index 166d641..0000000 --- a/back-end/models/User.js +++ /dev/null @@ -1,63 +0,0 @@ -// a mongoose model of a user -const mongoose = require("mongoose") -const Schema = mongoose.Schema -const bcrypt = require("bcryptjs") -const jwt = require("jsonwebtoken") -const jwtStrategy = require("../config/jwt-config.js") - -const UserSchema = new Schema({ - username: { - type: String, - unique: true, - required: true, - }, - password: { - type: String, - required: true, - }, -}) - -// hash the password before the user is saved - -UserSchema.pre("save", function (next) { - const user = this - if (!user.isModified("password")) return next() - bcrypt.hash(user.password, 10, (err, hash) => { - if (err) return next(err) - user.password = hash - next() - }) -}) - - -// compare a given password with the database hash -UserSchema.methods.validPassword = function (password) { - return bcrypt.compareSync(password, this.password) -} - - -UserSchema.methods.generateJWT = function () { - const today = new Date() - const exp = new Date(today) - exp.setDate(today.getDate() + process.env.JWT_EXP_DAYS) - - return jwt.sign( - { - id: this._id, - username: this.username, - exp: parseInt(exp.getTime() / 1000), - }, - process.env.JWT_SECRET - ) -} - -UserSchema.methods.toAuthJSON = function () { - return { - username: this.username, - token: this.generateJWT(), - } -} - -const User = mongoose.model("User", UserSchema) - -module.exports = User \ No newline at end of file diff --git a/back-end/package.json b/back-end/package.json index 0bf03fe..64475a8 100644 --- a/back-end/package.json +++ b/back-end/package.json @@ -4,7 +4,7 @@ "description": "NYU Shuttle Backend", "main": "server.js", "scripts": { - "test": "mocha --timeout 10000000", + "test": "mocha --timeout 2000", "coverage": "nyc --reporter=html --reporter=text --reporter=lcov --reporter=clover mocha --timeout 10000 --exit", "coveralls": "nyc report --reporter=text-lcov | coveralls" }, diff --git a/back-end/routes/authentication-routes.js b/back-end/routes/authentication-routes.js deleted file mode 100644 index ba2154c..0000000 --- a/back-end/routes/authentication-routes.js +++ /dev/null @@ -1,106 +0,0 @@ -const express = require("express"); - -const mongoose = require("mongoose"); -const User = require("../models/User.js"); - -// a method that constains code to handle authentication-specific routes -const authenticationRouter = () => { - const router = express.Router(); - - router.post("/signup", async (req, res, next) => { - const username = req.body.username; - const password = req.body.password; - - if (!username || !password) { - res.status(401).json({ - success: false, - message: `No username or password supplied.`, - }); - next(); - } - - // try to create a new user - try { - const user = await new User({ username, password }).save(); - console.error(`New user: ${user}`); - const token = user.generateJWT(); - res.json({ - success: true, - message: "User saved successfully.", - token: token, - username: user.username, - }); - next(); - } catch (err) { - console.error(`Failed to save user: ${err}`); - res.status(500).json({ - success: false, - message: "Error saving user to database.", - error: err, - }); - next(); - } - }); - - // a route to handle login attempts requested to /auth/login - router.post("/login", async function (req, res, next) { - const username = req.body.username; - const password = req.body.password; - - if (!username || !password) { - res - .status(401) - .json({ success: false, message: `No username or password supplied.` }); - next(); - } - - try { - const user = await User.findOne({ username: username }).exec(); - if (!user) { - console.error(`User not found.`); - return res.status(401).json({ - success: false, - message: "User not found in the database.", - }); - next(); - } else if (!user.validPassword(password)) { - console.error(`Incorrect password.`); - return res.status(401).json({ - success: false, - message: "Incorrect password.", - }); - next(); - } - console.log("User logged in successfully."); - const token = user.generateJWT(); - return res.json({ - success: true, - message: "User logged in successfully.", - token: token, - username: user.username, - }); - next(); - } catch (err) { - console.error(`Error looking up user: ${err}`); - return res.status(500).json({ - success: false, - message: "Error looking up user in the database.", - error: err, - }); - next(); - } - }); - - // a route to handle logging out requests to /auth/logout - router.get("/logout", function (req, res, next) { - res.json({ - success: true, - message: "Successfully logged out", - }); - next(); - }); - - return router; -}; - -module.exports = authenticationRouter; diff --git a/back-end/test/login.test.js b/back-end/test/login.test.js deleted file mode 100644 index b7327b9..0000000 --- a/back-end/test/login.test.js +++ /dev/null @@ -1,49 +0,0 @@ -process.env.NODE_ENV = "test" -const chai = require("chai") -const chaiHttp = require("chai-http") -chai.use(chaiHttp) -const expect = chai.expect -const should = chai.should() - -const server = require("../app") - -describe("Login", () => { - /** - * test the POST /login route - */ - const formData = { username: "bla", password: "wrong" } - describe("POST /auth/login with incorrect username/password", () => { - it("it should return a 401 HTTP response code", done => { - chai - .request(server) - .post("/auth/login") - .type("form") - .send(formData) - .end((err, res) => { - res.should.have.status(401) / - done() - }) - }) - }) - - /** - * test the POST /login route - */ - const validFormData = { username: "newuser", password: "newpassword" } - describe("POST /auth/login with correct username/password", () => { - it("it should return a 200 HTTP response code", done => { - chai - .request(server) - .post("/auth/login") - .type("form") - .send(validFormData) - .end((err, res) => { - res.should.have.status(200) - done() - }) - }) - }) - - - -}) \ No newline at end of file diff --git a/back-end/test/logout.test.js b/back-end/test/logout.test.js deleted file mode 100644 index b0a4694..0000000 --- a/back-end/test/logout.test.js +++ /dev/null @@ -1,39 +0,0 @@ -process.env.NODE_ENV = "test" - -const chai = require("chai") -const chaiHttp = require("chai-http") -chai.use(chaiHttp) -const expect = chai.expect -const should = chai.should() - -const server = require("../app") - -// a group of tests related to the /logout route -describe("Logout", () => { - /** - * test the GET /logout route - */ - describe("GET /auth/logout", () => { - it("it should return a 200 HTTP response code", done => { - chai - .request(server) - .get("/auth/logout") - .end((err, res) => { - res.should.have.status(200) - done() - }) - }) - - it("it should return an object with specific properties", done => { - chai - .request(server) - .get("/auth/logout") - .end((err, res) => { - res.body.should.be.a("object") - res.body.should.have.property("success", true) - res.body.should.have.keys("success", "message") - done() - }) - }) - }) -}) \ No newline at end of file diff --git a/front-end/src/App.js b/front-end/src/App.js index e471379..449f8c5 100644 --- a/front-end/src/App.js +++ b/front-end/src/App.js @@ -62,7 +62,7 @@ function App() { }; const devTools = (e) => { - if (e.keyCode === 82) { + if ((e.keyCode === 82 && e.metaKey) || (e.keyCode === 82 && e.ctrlKey)){ // R key console.log('Resetting local storage...'); localStorage.clear(); diff --git a/front-end/src/components/settings/FeedbackSupportPage.js b/front-end/src/components/settings/FeedbackSupportPage.js index 3ff7e4c..7ff7e3c 100644 --- a/front-end/src/components/settings/FeedbackSupportPage.js +++ b/front-end/src/components/settings/FeedbackSupportPage.js @@ -16,7 +16,8 @@ const FeedbackSupportPage = () => { const handleSubmit = async (e) => { e.preventDefault(); - if (category === "" || feedback === "") { + if (userId == null || category === "" || feedback === "") { + setResponse({}); setErrorMessage("Please fill out all fields"); return; } @@ -32,6 +33,7 @@ const FeedbackSupportPage = () => { requestData ); console.log(`Server response: ${JSON.stringify(response.data, null, 0)}`); + setErrorMessage(""); setResponse(response.data); } catch (err) { console.log(err); @@ -39,6 +41,7 @@ const FeedbackSupportPage = () => { } finally { setCategory(""); setFeedback(""); + } }; @@ -50,6 +53,8 @@ const FeedbackSupportPage = () => {

Feedback / Support

+ {errorMessage &&
{errorMessage}
} + {response.message &&
Thank you for the feedback!
}