BLAH stupid middleware error
This commit is contained in:
parent
057fbe2afb
commit
ba498a4d4b
@ -8,7 +8,7 @@ moment = require('moment')
|
|||||||
|
|
||||||
//getuser
|
//getuser
|
||||||
//api/v0/user/me
|
//api/v0/user/me
|
||||||
async function getUserID(userid) {
|
async function getUserByID(userid) {
|
||||||
//console.log(userid);
|
//console.log(userid);
|
||||||
//console.log(userid.id);
|
//console.log(userid.id);
|
||||||
let userRes = await userModel.findByPk(userid.id, {
|
let userRes = await userModel.findByPk(userid.id, {
|
||||||
@ -131,7 +131,7 @@ async function updateProfile(user, body) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
getUserID,
|
getUserByID,
|
||||||
addUser,
|
addUser,
|
||||||
loginUser,
|
loginUser,
|
||||||
updateProfile,
|
updateProfile,
|
||||||
|
@ -10,7 +10,6 @@ async function auth(req, res, next) {
|
|||||||
const authToken = req.header("auth-token");
|
const authToken = req.header("auth-token");
|
||||||
if (!authToken) {
|
if (!authToken) {
|
||||||
const error = new Error("No Token key was supplied. Invalid request");
|
const error = new Error("No Token key was supplied. Invalid request");
|
||||||
error.status = 401;
|
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -22,15 +21,14 @@ async function auth(req, res, next) {
|
|||||||
|
|
||||||
if (!token) {
|
if (!token) {
|
||||||
const error = new Error("Token key not found. Invalid request");
|
const error = new Error("Token key not found. Invalid request");
|
||||||
error.status = 401;
|
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
|
|
||||||
const isMatch = await compareHash(suppliedToken, token.token);
|
const isMatch = await compareHash(suppliedToken, token.token);
|
||||||
|
|
||||||
|
console.log(isMatch);
|
||||||
if (!isMatch) {
|
if (!isMatch) {
|
||||||
const error = new Error("Token key not found. Invalid request");
|
const error = new Error("Token key not found. Invalid request");
|
||||||
error.status = 401;
|
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
//if token is a match
|
//if token is a match
|
||||||
@ -43,14 +41,10 @@ async function auth(req, res, next) {
|
|||||||
if (route.includes("/user/") && permission === "canRead") {
|
if (route.includes("/user/") && permission === "canRead") {
|
||||||
next();
|
next();
|
||||||
}
|
}
|
||||||
else if ((req.method === "GET" && permission === "canRead") || (["GET", "POST", "PUT", "DELETE"].includes(req.method) && permission === "canWrite")) {
|
if ((req.method === "GET" && permission === "canRead") || (["GET", "POST", "PUT", "DELETE"].includes(req.method) && permission === "canWrite")) {
|
||||||
next();
|
next();
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
const error = new Error("Insufficient permission");
|
|
||||||
error.status = 401;
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
if (!isValid(token.expiration)){
|
if (!isValid(token.expiration)){
|
||||||
req.token.destroy();
|
req.token.destroy();
|
||||||
throw new Error("Token expired");
|
throw new Error("Token expired");
|
||||||
@ -63,3 +57,12 @@ async function auth(req, res, next) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
module.exports = { auth };
|
module.exports = { auth };
|
||||||
|
|
||||||
|
/*
|
||||||
|
else {
|
||||||
|
const error = new Error("Insufficient permission");
|
||||||
|
error.status = 401;
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
|
||||||
|
*/
|
@ -186,13 +186,14 @@ app.auth = (function (app) {
|
|||||||
function isLoggedIn(callback) {
|
function isLoggedIn(callback) {
|
||||||
|
|
||||||
if (getToken()) {
|
if (getToken()) {
|
||||||
|
console.log("you shldnt appear at all");
|
||||||
return app.api.get("user/me", function (error, data) {
|
return app.api.get("user/me", function (error, data) {
|
||||||
console.log(error, data);
|
console.log(error, data);
|
||||||
if (!error) app.auth.user = data;
|
if (!error) app.auth.user = data;
|
||||||
return callback(error, data);
|
return callback(error, data);
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
callback(null, false);
|
callback(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
const { getUserID, updateProfile } = require("../functions/user");
|
const { getUserByID, updateProfile } = require("../functions/user");
|
||||||
|
|
||||||
const express = require("express");
|
const express = require("express");
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
@ -7,18 +7,11 @@ const router = express.Router();
|
|||||||
//getbyid
|
//getbyid
|
||||||
router.get("/me", async function (req, res, next) {
|
router.get("/me", async function (req, res, next) {
|
||||||
try {
|
try {
|
||||||
let user = await getUserID(req.user);
|
let user = await getUserByID(req.user);
|
||||||
if (!user) {
|
console.log(user);
|
||||||
let error = new Error("User not found");
|
|
||||||
error.status = 400;
|
|
||||||
console.log(error);
|
|
||||||
return next(error);
|
|
||||||
}
|
|
||||||
if (user){
|
|
||||||
res.json({
|
res.json({
|
||||||
user: user,
|
user: user,
|
||||||
});
|
});
|
||||||
}
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
next(error);
|
next(error);
|
||||||
}
|
}
|
||||||
|
@ -56,7 +56,8 @@
|
|||||||
$(document).ready(function () {
|
$(document).ready(function () {
|
||||||
//check if user is logged in
|
//check if user is logged in
|
||||||
app.auth.isLoggedIn(function (error, data) {
|
app.auth.isLoggedIn(function (error, data) {
|
||||||
if (data) {
|
if (!error) {
|
||||||
|
console.log(error);
|
||||||
$.scope.getUsername.update(data);
|
$.scope.getUsername.update(data);
|
||||||
|
|
||||||
if (location.pathname == "/profile") {
|
if (location.pathname == "/profile") {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user