Skip to content

setNotFoundHandler does not include session variable in request object #1039

Open
@clubside

Description

@clubside

💬 Question here

Normally all request objects include the session variables if a user is logged in. However setNotFoundHandler does not making my display, which normally shows the user's avatar if logged in or a login button if not, always show the login button. How do I get the session variables when using setNotFoundHandler or is there an alternative approach to catch all invalid routes that will return session variables?

app.setNotFoundHandler(async (request, reply) => {
	const pageInfo = {
		title: 'Page Not Found',
		role: 'guest'
	}
	console.log({ pageInfoUser: request.user })
	if (request.user) {
		pageInfo.role = request.user.role
		pageInfo.user = request.user.email
	}
	pageInfo.errorImage = '/img/errors/404-error.svg'
	pageInfo.errorMessage = 'This is not the page you\'re looking for.'
	reply.code(404)
	reply.header('Content-Type', 'text/html; charset=utf-8')
	reply.type('text/html')
	reply.send(await generatePage(pageInfo, 'error.html'))
})

In this case console.log({ pageInfoUser: request.user }) returns undefined even when logged in whereas every other route displays the user session variable when logged in.

Your Environment

  • node version: 22.3.0
  • fastify version: 4.28.1
  • @fastify/secure-session version: 7.5.1
  • os: Windows

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wantedExtra attention is neededquestionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions