From 195f28022eed99a41ce025d4c65949e824e90a33 Mon Sep 17 00:00:00 2001 From: Yujin Hong Date: Mon, 15 Apr 2024 23:29:39 -0400 Subject: [PATCH] finished authentication --- back-end/App.js | 56 ++++++++++++++------------------------ back-end/package.json | 3 +- front-end/src/App.js | 3 ++ front-end/src/LoginForm.js | 1 + front-end/src/Matches.js | 2 +- 5 files changed, 28 insertions(+), 37 deletions(-) diff --git a/back-end/App.js b/back-end/App.js index 88f85e5..2a17cfd 100644 --- a/back-end/App.js +++ b/back-end/App.js @@ -9,6 +9,7 @@ const mongoose = require('mongoose'); const fs = require("fs"); const path = require("path"); const compat = require("./Compatibility") +const cookieParser = require('cookie-parser'); const User = mongoose.model('User'); const newUser = new User({}); @@ -30,22 +31,29 @@ const dbPath = path.join(__dirname, 'mockDatabase.json'); const userData = require('./mockDatabase.json'); const { profile } = require('console'); -app.use(cors()); // allow cross-origin resource sharing +app.use(cookieParser()); +app.use(cors({credentials: true, origin: 'http://localhost:3000'})); // allow cross-origin resource sharing app.use(express.json()); // decode JSON-formatted incoming POST data app.use(express.urlencoded({ extended: true })); // decode url-encoded incoming POST data //sessions middleware const sessionOptions = { - secret: 'secret for signing session id', - saveUninitialized: false, - resave: false + secret: 'secret-for-signing-session-id', + saveUninitialized: true, + resave: false, + cookie: { + httpOnly: true, + maxAge: 3600000 + } }; app.use(session(sessionOptions)); app.use(function (req, res, next) { - req.session.user = req.session.user || ""; - req.session.matches = req.session.matches || {}; + console.log(req.session.user); + req.session.user = req.session.user || "a"; + req.session.matches = req.session.matches || []; + console.log(req.session) next(); }); @@ -96,6 +104,9 @@ app.post('/login', (req, res) => { //placeholder code until authentication is complete logindict = {username: username, password: password}; newUser.login = logindict; + req.session.user = username; + req.session.save(); + console.log('login: ', req.session.user); res.json({ message: "Login successful" }); } else { @@ -160,29 +171,7 @@ app.post('/survey', (req, res) => { newUser.profile = profiledict; newUser.answers = answersdict; - newUser.preferences = preferencesdict - - // newUser.answers.gender = surveyData.genderAns; - // newUser.answers.year = surveyData.year; - // newUser.answers.pets = surveyData.petsAns; - // newUser.answers.guests = surveyData.guestsAns; - // newUser.answers.smoke = surveyData.smokeAns; - // newUser.answers.drink = surveyData.drinkAns; - // newUser.answers.rent_max = surveyData.maxRent; - // newUser.answers.rent_min = surveyData.minRent; - // newUser.answers.bedtime = surveyData.bedAns; - // newUser.answers.quietness = surveyData.quietAns; - // newUser.answers.cleanliness = surveyData.cleanAns; - - // newUser.preferences.gender = surveyData.genderPref; - // newUser.preferences.year = surveyData.yearPref; - // newUser.preferences.pets = surveyData.petsPref; - // newUser.preferences.guests = surveyData.guestsPref; - // newUser.preferences.smoke = surveyData.smokePref; - // newUser.preferences.drink = surveyData.drinkPref; - // newUser.preferences.bedtime = surveyData.bedPref; - // newUser.preferences.quietness = surveyData.quietPref; - // newUser.preferences.cleanliness = surveyData.cleanPref; + newUser.preferences = preferencesdict; newUser.save() .then(() => { @@ -193,18 +182,15 @@ app.post('/survey', (req, res) => { console.log(err); res.status(500).send('server error'); }); - - //Now tell the frontend that it is safe to proceed (the frontend survey.js will navigate to matches after this) }); app.get('/matches', async (req, res) => { - console.log(req.session.user) + console.log('matches:', req.session.user) + req.session.user = req.session.user || "randomname"; try { - User.find() .then(foundUser => { //jsonArray.push(foundUser); - console.log("HERE!") res.json(foundUser) }) .catch(err => { @@ -224,7 +210,7 @@ app.get('/matches', async (req, res) => { app.get('/chatlist', async (req, res) => { try { //Here, we will send a request to the database, searching for users that the user currently has an active chat with (not sure that determiend at the moment) - const jsonArray = await newUser.find(); + const jsonArray = await User.find(); //jsonArray will be a list of all the user jsons retrieved from the database //We could maybe sort this based on the most recent message first diff --git a/back-end/package.json b/back-end/package.json index e6bc23a..1a65972 100644 --- a/back-end/package.json +++ b/back-end/package.json @@ -11,6 +11,7 @@ "author": "", "license": "ISC", "dependencies": { + "cookie-parser": "^1.4.6", "cors": "^2.8.5", "dotenv": "^16.4.5", "express": "^4.18.3", @@ -25,4 +26,4 @@ "mocha": "^10.4.0", "nodemon": "^3.1.0" } -} \ No newline at end of file +} diff --git a/front-end/src/App.js b/front-end/src/App.js index 96a45f2..a419ccf 100644 --- a/front-end/src/App.js +++ b/front-end/src/App.js @@ -10,6 +10,9 @@ import Profile from './Profile' import EditProfile from './EditProfile'; import MyPreferences from './MyPreferences' import OtherProfile from './OtherProfile'; +import axios from 'axios' + +axios.defaults.withCredentials = true; function App() { return ( diff --git a/front-end/src/LoginForm.js b/front-end/src/LoginForm.js index d89c4b7..a82e51c 100644 --- a/front-end/src/LoginForm.js +++ b/front-end/src/LoginForm.js @@ -19,6 +19,7 @@ function LoginForm() { 'Content-Type': 'application/json', }, body: JSON.stringify({ username, password }), + credentials: 'include', }); const data = await response.json(); diff --git a/front-end/src/Matches.js b/front-end/src/Matches.js index c4e5731..8090568 100644 --- a/front-end/src/Matches.js +++ b/front-end/src/Matches.js @@ -16,7 +16,7 @@ const Matches = props => { const fetchMatches = () => { axios - .get('http://localhost:3001/matches') + .get('http://localhost:3001/matches')//, { withCredentials: true}) .then(response => { const matchesData = response.data; //response is an array of JSON objects setMatches(matchesData);