diff --git a/Auth/Auth.js b/Auth/Auth.js index f480df2..d5b10cc 100644 --- a/Auth/Auth.js +++ b/Auth/Auth.js @@ -32,12 +32,12 @@ exports.register = async (req, res, next) => { const maxAge = 3 * 60 * 60; // 3hrs in sec const token = jwt.sign( { id: user._id, username, role: user.role }, - jwtsecret, + jwtsecret, // used crypto lib {expiresIn: maxAge, } ); res.cookie("jwt", token, { - secure: true, - httpOnly: true, + secure: true, // cookie is sent only via http + httpOnly: true, // prevents scripts from browser from reading cookie maxAge: maxAge * 1000, // 3hrs in ms }); @@ -71,19 +71,6 @@ exports.login = async (req, res, next) => { // find user in db const user = await User.findOne({ username }) if (!user) { - - const maxAge = 3 * 60 * 60; // 3hrs in sec - const token = jwt.sign( - { id: user._id, username, role: user.role }, - jwtsecret, - {expiresIn: maxAge, } - ); - res.cookie("jwt", token, { - secure: true, - httpOnly: true, - maxAge: maxAge * 1000, // 3hrs in ms - }); - res.status(401).json({ message: "Login not successful", error: "User not found", diff --git a/config.js b/config.js index 6ec0455..316be8c 100644 --- a/config.js +++ b/config.js @@ -5,4 +5,5 @@ dotenv.config(); module.exports = { jwtsecret: process.env.JWTSECRET, port: process.env.PORT, + hostname: process.env.HOSTNAME }; diff --git a/server.js b/server.js index d38d8ea..7bb5351 100644 --- a/server.js +++ b/server.js @@ -13,13 +13,13 @@ app.use(express.json()); app.use(cookieParser()); // read env variables from config file -const { port } = require('./config'); +const { port,hostname } = require('./config'); -// adding routes +// login & register route app.use("/api/auth", require("./Auth/Route")); -app.get("/admin", adminJWTAuth, (req, res) => res.send("Admin Route")); -app.get("/basic", userJWTAuth, (req, res) => res.send("User Route")); +app.get("/admin", adminJWTAuth, (req, res) => res.send("Admin Dashboard")); +app.get("/basic", userJWTAuth, (req, res) => res.send("User Dahboard")); app.get("/logout", (req, res) => {res.cookie("jwt", "", { maxAge: "1" }) res.redirect("/") }) @@ -36,7 +36,7 @@ const options = { // creating expresss https server that listens on port 8000 https.createServer(options,app).listen(port, ()=>{ - console.log('server is runing at port 8000') + console.log(`Server running at http://${hostname}:${port}/`) }); // Handling Error @@ -45,7 +45,7 @@ process.on("unhandledRejection", err => { server.close(() => process.exit(1)) }) -// try route + // Heart beat app.get('/', (req,res)=>{ res.send("Hello from express server.") })