refactor auth and api/index to ensure token verified and sent to dispatch ADD_USER

This commit is contained in:
Sorrel Bri 2020-01-17 21:18:50 -08:00 committed by sorrelbri
parent 5c8da62edb
commit e90abc283a
6 changed files with 12 additions and 10 deletions

View file

@ -5,13 +5,13 @@ const apiIndex = async (req, res, next) => {
try {
if (req.cookies && req.cookies.token) {
const token = req.cookies.token;
const verifiedToken = verifyToken(token);
res.status(200).json(verifiedToken.user)
const verifiedToken = await verifyToken(token);
res.status(200).send(verifiedToken.user)
}
res.status(200).json()
}
catch {
catch (err) {
res.status(500).json(err);
}
}

View file

@ -26,7 +26,8 @@ const signup = async (req, res, next) => {
}
const newUser = await userQueries.insertUser(secureUser)
signToken(res, newUser).status(201).json({...newUser});
signToken(res, newUser)
res.status(201).json({...newUser});
}
catch (err) {

View file

@ -25,7 +25,6 @@ const validate = (req, res, next) => {
}
const extractedErrors = []
errors.array().map(err => extractedErrors.push({ [err.param]: err.msg }))
return res.status(422).json({
errors: extractedErrors,
})

View file

@ -19,9 +19,10 @@ const apiRouter = require('./routes/api');
const app = express();
const allowedOrigin = process.env.NODE_ENV === 'production' ? process.env.REACT_ADDRESS : '*';
const allowedOrigin = process.env.REACT_ADDRESS;
const corsOptions = {
origin: allowedOrigin
origin: allowedOrigin,
credentials: true
}
app.options('*', cors(corsOptions));

View file

@ -12,8 +12,10 @@ const signToken = (res, user) => {
});
return res.cookie('token', token, {
expires: new Date(Date.now() + expiration),
secure: false, // set to true if your using https
httpOnly: true,
domain: process.env.DOMAIN,
// secure: false, // set to true if your using https
httpOnly: true
// path: '/api/v1'
});
};
module.exports = signToken;

View file

@ -2,7 +2,6 @@
module.exports = {
enableSocket: io => {
// io.on('connection', () => console.log('connected'))
io.on('connection', ()=> {
io.emit('connected', {message: 'socket connected'});
})