Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Global properties in Express & Handlebars

I'm using Handlebars (using express3-handlebars) for templates and Passport for authentication in a NodeJS app. All is working great but I wondered if there is a way to pass the req.user object created by Passport to Handlebars globally.

So my header partial might look something like this:

<header>
    <h1>My Title</h1>
    {{#if user}}
        <p>Hello {{user.name}}</p>
    {{else}}
        <p>Please <a href='/login'>Log In</a></p>
    {{/if}}
</header>

As it stands I have to pass the user object explicitly with every page render:

app.get('/', function(req, res){
    res.render('home', {
        page_title: 'welcome',
        user: req.user
    }); 
});

This seems like the wrong way to go about it as I require it on every page, can I not just set it once and have all pages have access to it?

I can't do this when I instantiate Handlebars as it's dependent on the user being logged in with Passport, which won't always be the case.

Would creating a global 'page_options' object, appending and passing it to every render be the right solution or does Handlebars/Express have a way to handle this?

like image 793
lewis Avatar asked Feb 26 '14 11:02

lewis


People also ask

How do I define a global variable in Express?

To set up a global variable, we need to create it on the global object. The global object is what gives us the scope of the entire project, rather than just the file (module) the variable was created in. In the code block below, we create a global variable called globalString and we give it a value.

What is global variable in node?

Global variables are variables that can be declared with a value, and which can be accessed anywhere in a program. The scope of global variables is not limited to the function scope or any particular JavaScript file. It can be declared at one place and then used in multiple places.

What is global scope in nodeJS?

While in browsers the global scope is the window object, in nodeJS the global scope of a module is the module itself, so when you define a variable in the global scope of your nodeJS module, it will be local to this module. You can read more about it in the NodeJS documentation where it says: global.

What is RES locals in Express?

The res. locals is an object that contains the local variables for the response which are scoped to the request only and therefore just available for the views rendered during that request or response cycle.


1 Answers

I haven't personally used Passport before, but based on the Passport README and what I've done with other authentication schemes, this should work.

Express 3

app.configure(function() {
    app.use(passport.initialize());
    app.use(passport.session());
    app.use(function(req, res, next) {
        res.locals.user = req.user; // This is the important line

        next();
    });
    app.use(app.router);
});

Express 4

app.use(passport.initialize());
app.use(passport.session());
app.use(function(req, res, next) {
    res.locals.user = req.user; // This is the important line

    next();
});

Basically, right before rendering, your app.locals, res.locals, and the locals you pass into the render function (the second argument) all get combined and passed along to your view engine.

like image 86
ZachRabbit Avatar answered Sep 21 '22 23:09

ZachRabbit