How do I not allow access to the home page without sign-in?

Issue

I’m new in nodeJs, so I prepared a sign in page with the database and it works normally.
Now I have prepared a home page, but I notice that the client can access the home page directly without connection in his account by this link http://127.0.0.1:5500/public/Home.html
So what should I learn or should I do?
Any examples or propositions are welcome.

Solution

you should use a middleware to check the user is logged in or not.

for example

const express = require('express');
const router = express.Router();
const Auth = require('../middleware/auth');//require the Auth middleware
 

router.get('/',Auth,async(req,res)=>{
       //here ^^^^^
  //your code...

})

Answered By – Hidden Soldier

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published