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
parent 25caa3124d
commit 653260b2fc
6 changed files with 12 additions and 10 deletions

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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