Skip to content

Commit

Permalink
updated middleware to properly cover routes/update delete function to…
Browse files Browse the repository at this point in the history
… relocate correctly
  • Loading branch information
Napica committed Nov 12, 2020
1 parent e9a6fb4 commit c81e37c
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 41 deletions.
6 changes: 3 additions & 3 deletions client/src/App.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import "./App.css";
import { useState, useEffect, useContext } from "react";
import { useState, useEffect} from "react";
import UserContext from "./context/UserContext"
import Navbar from "./components/Navbar/Navbar";
import SigninPage from "./containers/SigninPage/SigninPage";
Expand All @@ -13,7 +13,7 @@ import AuthContext from "./context/AuthContext";
import "materialize-css";
import { setAxiosDefaults } from "./utils/axiosDefaults";
import Footer from "./components/Footer/Footer";
import Audio from "./components/Audio/Audio";
// import Audio from "./components/Audio/Audio";
// import { Button, Card, Row, Col } from "react-materialize";
import ProtectedRoute from "./components/ProtectedRoute/ProtectedRoute";

Expand All @@ -38,7 +38,7 @@ function App() {

return (
<div className="App">
<Audio />
{/* <Audio /> */}
<UserContext.Provider value={{ userId, setUserId }}>
<Router>
<AuthContext.Provider value={{ jwt, setJwt }}>
Expand Down
4 changes: 2 additions & 2 deletions client/src/containers/UpdateForm/UpdateForm.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ const [show2, setShow2] = useState(false)
hideModal()
event.preventDefault()
const userId = id
history.push("/")
window.location.replace("/")
API.deleteUser(userId)
.then(console.log("Your journey has ended..."))

Expand Down Expand Up @@ -149,7 +149,7 @@ const [show2, setShow2] = useState(false)
norestriction: formObject.categoryType.norestriction,
homebrew: formObject.categoryType.homebrew,
lvl1only: formObject.categoryType.lvl1only,
rpersonly: formObject.categoryType.rprsonly,
rpersonly: formObject.categoryType.rpersonly,
oneshots: formObject.categoryType.oneshots,
displaydice: formObject.categoryType.displaydice,
voyuerallowed: formObject.categoryType.voyuerallowed
Expand Down
13 changes: 5 additions & 8 deletions controllers/usersController.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const db = require("../models");
const jwt = require("jsonwebtoken");

// const jwt = require("jsonwebtoken");

// Defining methods for the booksController
module.exports = {
Expand All @@ -12,9 +11,8 @@ module.exports = {

findAllDms: function (req, res) {
db.User.find({ isDm: true })
.then((dms) => res.json(dms))
.catch((err) => res.json(err));

.then((dms) => res.json(dms))
.catch((err) => res.json(err));
},

findById: function (req, res) {
Expand All @@ -33,15 +31,14 @@ module.exports = {
// Authentication would go here

updateField: function (req, res) {
// console.log("route hit")
console.log(req.body);
db.User.updateOne({ _id: req.params.id }, req.body)
db.User.updateOne({ _id: req.user._id }, req.body)
.then((user) => res.json(user))
.catch((err) => res.json(err));
},

remove: function (req, res) {
db.User.findById({ _id: req.params.id })
db.User.findById({ _id: req.user._id })
.then((dbUser) => dbUser.remove())
.then((dbUser) => res.json(dbUser))
.catch((err) => res.status(422).json(err));
Expand Down
55 changes: 27 additions & 28 deletions middleware/auth.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,31 @@
const jwt = require('jsonwebtoken');
const jwt = require("jsonwebtoken");

function auth(req, res, next){
console.log(req.headers);
if (!req.headers.authorization) {
return res.status(401).json({
error: true,
data: null,
message: "Unauthorized.",
});
}
jwt.verify(
req.headers.authorization,
process.env.REACT_APP_SECRET,
(err, decoded) => {
if (err) {
console.log(err);
return res.status(401).json({
error: true,
data: null,
message: "Invalid token.",
});
} else {
console.log(decoded);
next();

}
function auth(req, res, next) {
console.log(req.headers);
if (!req.headers.authorization) {
return res.status(401).json({
error: true,
data: null,
message: "Unauthorized.",
});
}
jwt.verify(
req.headers.authorization,
process.env.REACT_APP_SECRET,
(err, decoded) => {
if (err) {
console.log(err);
return res.status(401).json({
error: true,
data: null,
message: "Invalid token.",
});
} else {
req.user = decoded;
next();
}
);
}
);
}

module.exports = auth;
module.exports = auth;

0 comments on commit c81e37c

Please sign in to comment.