Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create userController middleware functions, still WIP #41

Merged
merged 1 commit into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion client/containers/MainContainer.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React from 'react';
import TopNav from '../components/TopNav';

export default function MainContainer() {

Expand Down
58 changes: 58 additions & 0 deletions server/controllers/userController.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,62 @@ userController.getUsers = (req, res, next) => {
}
}


/*-------------CREATE A NEW USER--------------*/
userController.createUser = (req, res, next) => {

if (req.body.username && req.body.password) {
res.locals.newUser = {...req.body}; //Object.assign(req.body)
// insert the newUser object the model document inside our mongoose
const query = {
text:'';
values: [req.params.username, req.params.password]
};
try{
userModel.query(query)
.then((data) => {
console.log(data);
return next();
})
}
catch(err) {
next(err);
}
return next();
} else {
return next('Error: Invalid user input');
}

};

/**
* -------------LOGIN & VERIFY EXISTING USER--------------
* verifyUser - Obtain username and password from the request body, locate
* the appropriate user in the database, and then authenticate the submitted password
* against the password stored in the database.
*/
userController.verifyUser = (req, res, next) => {
// write code here
res.locals.user = req.body.username;
res.locals.pw = req.body.password;


User.findOne({username: `${res.locals.user}`})
.then((data) => {
console.log(data);
if (data !== null && data.password === res.locals.pw) {
res.locals.id = data._id.toString();
console.log(data._id.toString(), 'data id')
console.log(req.body, 'req.body');
return next();
}
else {
res.redirect('/signup');
}
});
// return next();

};


module.exports = userController;
6 changes: 3 additions & 3 deletions server/infra/server.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const express = require('express');
const path = require('path');
// const apiRouter = require('./routes/api');
const userRouter = require('../routers/userRouter.js');
require("dotenv").config();

const app = express();
Expand All @@ -25,8 +25,8 @@ app.use('/build', express.static(path.join(__dirname, '../client/build')));
// serve index.html to any get request on the path '/'
app.get('/', (req, res) => res.status(200).sendFile(path.join(__dirname, '../client/index.html')));

// will send any calls to our page through our proxy server
// app.use('/api', apiRouter);
/* ROUTE REQUESTS THROUGH userRouter */
app.use('/user', userRouter);

// 404 error handler
app.use('/*', (req, res) => {
Expand Down
16 changes: 16 additions & 0 deletions server/routes/userRouter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const express = require('express');

const router = express.Router();

const userController = require('../controllers/userController.js');

router.get('/login/:username/:password', userController.verifyUser, (req,res) => {
return res.status(200).json(res.locals.user);
});

router.post('/signup', userController.createUser, (req,res) => {
return res.status(200).json(res.locals.newUser);
});


module.exports = router;
4 changes: 2 additions & 2 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ module.exports = {
proxy: {
'/api': { // whenever we make an http request to the path /api, it redirects it from 8080 to 3000 to go to the backend automatically
target: 'http://localhost:3000',
pathRewrite: { '^/api': '/api' },
pathRewrite: { '^/user': '/user' },
},
},
compress: true,
port: 8080,
port: 8090,
// For React Router refreshing
historyApiFallback: true,
},
Expand Down