Skip to content
This repository has been archived by the owner on Dec 31, 2019. It is now read-only.

Commit

Permalink
Fixed user verification (#13)
Browse files Browse the repository at this point in the history
* Fixed Atlas db name

* Fixed user verify route
  • Loading branch information
lincolnanders5 authored and pcshah1996 committed Nov 26, 2018
1 parent fba5729 commit 6fbc152
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
34 changes: 29 additions & 5 deletions backend/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

const express = require('express');
const router = express.Router({});
const transactionsApi = require('./transactions')
const transactionsApi = require('./transactions');

const passport = require('passport');
const jwt = require('jsonwebtoken');
Expand Down Expand Up @@ -108,19 +108,43 @@ router.post('/login', function (req, res) {
const email = req.body.email;
const password = req.body.password; // Password is scrambled in newUser, must get original
const token = await jwt.sign({ email, password }, config.JWT_SECRET);
res.json({ complete: true, userVerify: true, urlRedirect: "dashboard", setCookies: { token: token } })
res.cookie("token", token).json({ complete: true, userVerify: true, urlRedirect: "dashboard", setCookies: { token } })
})(req, res)
});

router.post('/verify', async function (req, res, next) {
try {
const encodedToken = req.cookies.token || req.body.token || (() => { throw "No token cookie provided" })();
const decodedToken = await jwt.verify(encodedToken, config.JWT_SECRET);

const email = decodedToken.email;
const password = decodedToken.password;

const user = await UserDB.findOne({ email }) || (() => { throw `No user with email "${email}" found` })();
const validate = await user.isValidPassword(password);

if (user && validate) {
return res.json({ userVerify : true, user : { _id : user._id, email : user.email } });
}

return res.json({ userVerify: false, urlRedirect: "login" })
} catch (error) {
console.log(error);
return res.json({ userVerify: false, urlRedirect: "login", message : error })
}
});

router.use(async function (req, res, next) {
try {
const encodedToken = req.cookies.token;
const encodedToken = req.cookies.token || req.body.token || (() => { throw "No token cookie provided" })();
const decodedToken = await jwt.verify(encodedToken, config.JWT_SECRET);

const email = decodedToken.email;
const password = decodedToken.password;

const user = await UserDB.findOne({ email });
console.log(decodedToken.email);
const user = await UserDB.findOne({ email }) || (() => { throw `No user with email "${email}" found` })();
console.log(email);
const validate = await user.isValidPassword(password);

if (user && validate) {
Expand All @@ -130,7 +154,7 @@ router.use(async function (req, res, next) {
return res.json({ userVerify: false, urlRedirect: "login" })
} catch (error) {
console.log(error);
return res.json({ userVerify: false, urlRedirect: "login" })
return res.json({ userVerify: false, urlRedirect: "login", message : error })
}
});

Expand Down
2 changes: 1 addition & 1 deletion config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ require('dotenv').config();
var config = {};

config.port = 5000;
config.db_path = "hello";
config.db_path = "test";
config.db_url = `mongodb+srv://${process.env.DB_USER}:${process.env.DB_PASS}@ircdb-adim8.mongodb.net/test?retryWrites=true`;
config.JWT_SECRET = "KLD24Fj#U_RI@JRIJi5!jdf20rk3jlk4?jfa";

Expand Down

0 comments on commit 6fbc152

Please sign in to comment.