Skip to content

Commit

Permalink
finished authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
Yujin Hong committed Apr 16, 2024
1 parent b4773e7 commit 195f280
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 37 deletions.
56 changes: 21 additions & 35 deletions back-end/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({});
Expand All @@ -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();
});

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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(() => {
Expand All @@ -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 => {
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion back-end/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -25,4 +26,4 @@
"mocha": "^10.4.0",
"nodemon": "^3.1.0"
}
}
}
3 changes: 3 additions & 0 deletions front-end/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
1 change: 1 addition & 0 deletions front-end/src/LoginForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ function LoginForm() {
'Content-Type': 'application/json',
},
body: JSON.stringify({ username, password }),
credentials: 'include',
});

const data = await response.json();
Expand Down
2 changes: 1 addition & 1 deletion front-end/src/Matches.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 195f280

Please sign in to comment.