How to access cookies in handlebars?

299 views Asked by At

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.

1

There are 1 answers

2
hungtran273 On

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>