Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

res.locals value not available in template engine #5081

Closed
NazCodeland opened this issue Jan 2, 2023 · 3 comments
Closed

res.locals value not available in template engine #5081

NazCodeland opened this issue Jan 2, 2023 · 3 comments

Comments

@NazCodeland
Copy link

NazCodeland commented Jan 2, 2023

Hey, I have this middleware in app.js

app.use("*", isUserLoggedIn);

within isUserLoggedInMiddleware.js I have

import User from "../../../model/usersSchema.js";
import jwt from "jsonwebtoken";

export default function isUserLoggedIn(req, res, next) {
	const jsonWebToken = req.cookies.jwt;

	if (jsonWebToken) {
		//
		jwt.verify(jsonWebToken, process.env.JWT_SECRET, async (error, decodedToken) => {
			//
			if (error) {
				console.log(error.message);
				res.locals.user = "";
				next();
			} //
			else {
				const user = await User.findById(decodedToken.id).lean();
				res.locals.user = user;
				next();
			}
		});
	} //
	else {
		console.log("user is not logged in");
		res.locals.user = "";
		next();
	}
}

in my navigation.hbs template file I have

<nav>
	<h1><a href="/">Ninja Smoothies</a></h1>
	<ul>
			{{#if user}} 
				<span>Welcome, {{user.email}}</span>
				<li><a href="/logout">Log out</a></li>
			{{else}}
				<li><a href="/login">Log in</a></li>
				<li><a href="/signup" class="btn">Sign up</a></li>
			{{/if}}
	</ul>
</nav>

prior to to upgrading to express@>=5.0.0-beta.1, once a user logged in, the navigation would only show the Log out option, but after upgrading, it still shows the Log in and Sign up options when a user is logged in.

I tried to see if I could access the user on res.locals.user = user within the template engine, and it seems like it's not avaialble within it.

@mhamzabcs
Copy link

mhamzabcs commented Jan 11, 2023

Hi, after mimicking your situation I was able to reproduce it. Although not quite sure why its happening, though you can try the below as a work around if you like:

In your routes file:

router.use(isUserLoggedIn);  
// other routes as per requirements

PS: Do remember it will run for all the routes below itself
Will look into it, Thanks!

@krzysdz
Copy link

krzysdz commented Jan 11, 2023

Express 5.0 has a different path matching syntax than Express 4. It is documented in Moving to Express 5 on the expressjs website.

If you want to execute middleware for all paths, use @mhamzabcs' suggestion and skip the path argument in router.use call.

Path route matching syntax is when a string is supplied as the first parameter to the app.all(), app.use(), app.METHOD(), router.all(), router.METHOD(), and router.use() APIs. The following changes have been made to how the path string is matched to an incoming request:

  • Add new ?, *, and + parameter modifiers
  • Matching group expressions are only RegExp syntax.
    (*) is no longer valid and must be written as (.*), for example
  • Named matching groups no longer available by position in req.params.
    /:foo(.*) only captures as req.params.foo and not available as req.params[0].
  • Regular expressions can only be used in a matching group.
    /\\d+ is no longer valid and must be written as /(\\d+).
  • Special * path segment behavior removed.
    /foo/*/bar will match a literal * as the middle segment.

Related issues and PRs: #2057, #4321, pillarjs/router#42

@NazCodeland
Copy link
Author

Awesome, that works. Thank you @mhamzabcs and @krzysdz.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants