41 lines
1.2 KiB
JavaScript
41 lines
1.2 KiB
JavaScript
import jwt from "jsonwebtoken";
|
|
import Patient from "../resources/Patients/PatientModel.js";
|
|
|
|
export const isAuthenticatedPatient = async (req, res, next) => {
|
|
try {
|
|
if (!req.headers.authorization) {
|
|
return res.status(401).json({
|
|
success: false,
|
|
message: "Please Login to access this resource",
|
|
});
|
|
}
|
|
const getToken = req.headers;
|
|
//remove Bearer from token
|
|
const fronttoken = getToken.authorization.slice(7);
|
|
|
|
const frontdecoded = jwt.verify(fronttoken, process.env.JWT_SECRET);
|
|
if (!frontdecoded) {
|
|
return res.status(400).json({
|
|
success: false,
|
|
message: "incorrect token",
|
|
});
|
|
}
|
|
const fpatient = await Patient.findById(frontdecoded.id);
|
|
if (fpatient) {
|
|
req.patient = fpatient;
|
|
return next();
|
|
} else {
|
|
return res.status(401).json({
|
|
success: false,
|
|
message: "Unauthorized",
|
|
});
|
|
}
|
|
} catch (error) {
|
|
return res.status(401).json({
|
|
success: false,
|
|
message: error.message,
|
|
});
|
|
}
|
|
};
|
|
|