Skip to content

Commit

Permalink
added jwt
Browse files Browse the repository at this point in the history
  • Loading branch information
bryentsariwati committed Apr 22, 2023
1 parent 12f1136 commit 6aa02ce
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 5 deletions.
11 changes: 10 additions & 1 deletion client/src/pages/Dashboard/Dashboard.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import HomeDisplay from '../../components/HomeDisplay';
import TraceList from '../../components/TraceList';
import Metrics from '../../components/Metrics';
import { Route, Routes } from 'react-router-dom';
import { useNavigate } from 'react-router-dom';
import './Dashboard.css';
import LeftBar from '../../components/LeftBar';
import sampleTraces from '../../assets/sampleTraces.json';
Expand All @@ -24,6 +25,9 @@ const Dashboard = () => {
const [refresh, setRefresh] = useState(false);
const [loading, setLoading] = useState(false);

const navigate = useNavigate();


useEffect(() => {
console.log('FIRED OFF USEFFECT');
setLoading(true);
Expand All @@ -33,7 +37,12 @@ const Dashboard = () => {
'Content-type': 'application/json',
},
})
.then((result) => result.json())
.then((result) => {
if (result.status === 419) {
navigate('/');
}
return result.json()
})
.then((data) => {
setLoading(false);
if (data.length) {
Expand Down
74 changes: 74 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@
"aws-sdk": "^2.1354.0",
"bcrypt": "^5.1.0",
"concurrently": "^8.0.1",
"cookie-parser": "^1.4.6",
"cors": "^2.8.5",
"dotenv": "^16.0.3",
"express": "^4.18.2",
"install": "^0.13.0",
"jsonwebtoken": "^9.0.0",
"nodemon": "^2.0.22",
"npm": "^9.6.4",
"react-data-table-component": "^7.5.3",
Expand Down
2 changes: 1 addition & 1 deletion server/aws_sdk/sortingSegments.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ const parseData = (segmentArray) => {
};

const reuslt = parseData(realData);
console.log(reuslt);
// console.log(reuslt);

function Node(segment) {
this.id = segment.Document.id;
Expand Down
38 changes: 38 additions & 0 deletions server/controllers/jwtController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const jwt = require("jsonwebtoken");


const jwtController = {
createJwt: (req,res,next) => {
console.log("increate jwt")
try {
const token = jwt.sign({email:res.locals.email}, process.env.JWT_KEY, {expiresIn:"1h"});
res.cookie("token", token, {
httpOnly:true,
// secure: true,
// maxAge: 100000,
// signed: true,
})
return next();
} catch(err) {
console.log('Error', err);
let error = {
log: 'Express error handler caught jwtController.createJwt',
message: { err: err },
};
return next(error);
}
},
verifyJwt: (req,res,next) => {
const token = req.cookies.token;
console.log(token);
// console.log("this is req", req);
try {
const user = jwt.verify(token,process.env.JWT_KEY);
return next();
} catch(err) {
res.clearCookie("token");
res.sendStatus(419);
}
},
}
module.exports = jwtController;
5 changes: 4 additions & 1 deletion server/controllers/userController.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const bcrypt = require('bcrypt');
const { db, Users } = require('../db.config.js');
const jwt = require("jsonwebtoken");

const createUser = async (req, res, next) => {
const TableName = Users;
Expand Down Expand Up @@ -52,6 +53,7 @@ const createUser = async (req, res, next) => {
// .promise();

console.log('User created successfully');
res.locals.email = email;
return next();
} catch (err) {
console.log('Error', err);
Expand Down Expand Up @@ -79,10 +81,11 @@ const verifyUser = async (req, res, next) => {
const isMatch = await bcrypt.compare(password, userData.password);
console.log('matched?', isMatch);


if (!isMatch) {
return res.sendStatus(401);
}

res.locals.email = userData.email;
return next();
} catch (err) {
console.log('Error', err);
Expand Down
8 changes: 6 additions & 2 deletions server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ const userController = require('./controllers/userController');
const redisController = require('./controllers/redisController');
const awsCredentialsController = require('./controllers/awsCredentialsController');
const getTraceMiddleware = require('./aws_sdk/traceDetails');
const jwtController = require('./controllers/jwtController');
const cookieParser = require('cookie-parser');

app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(cookieParser());

app.use(function (req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
Expand All @@ -25,12 +28,12 @@ app.get('/api', (req, res) => {
let data = 'hello';
res.status(200).json(data);
});
app.post('/createUser', userController.createUser, (req, res) => {
app.post('/createUser', userController.createUser, jwtController.createJwt, (req, res) => {
console.log('in create user');
res.sendStatus(201);
});

app.post('/verifyUser', userController.verifyUser, (req, res) => {
app.post('/verifyUser', userController.verifyUser, jwtController.createJwt, (req, res) => {
//successful login
// res.redirect('homepage');
res.sendStatus(200);
Expand All @@ -44,6 +47,7 @@ app.post('/setLogs', redisController.setLogs, (req, res) => {

app.get(
'/getTraces',
jwtController.verifyJwt,
redisController.getRedisTraces,
awsCredentialsController.getCredentials,
getTraceMiddleware.getSummary,
Expand Down

0 comments on commit 6aa02ce

Please sign in to comment.