0

I want to check in home page that user is logged in or not. I have stored jwt token in cookies. For authentication I want to use cookies in handlebars.

Om Thanki
  • 1
  • 1
  • It does not matter what framework you are using you can read them using the cookies API https://developer.mozilla.org/en-US/docs/Web/API/Document/cookie and https://www.w3schools.com/js/js_cookies.asp Also please read https://stackoverflow.com/help/how-to-ask – Marcello B. Jan 17 '23 at 15:49
  • Does this answer your question? [Read cookies with JavaScript](https://stackoverflow.com/questions/2905142/read-cookies-with-javascript) – Marcello B. Jan 17 '23 at 15:50

1 Answers1

0

Try this code:

Controller

const express = require('express');
const { engine } = require('express-handlebars');
var cookieParser = require('cookie-parser')

const app = express();
app.use(cookieParser())

app.engine('handlebars', engine());
app.set('view engine', 'handlebars');
app.set('views', './views');

app.get('/', (req, res) => {
  const isLogin = checkLogin(req.cookies.jwt); // Check login here
  res.render('home', { isLogin });
});

View

<h1>{{ isLogin }}</h1>
hungtran273
  • 1,180
  • 9
  • 11