Doubt/problem with "res.locals" after authentication

Asked

Viewed 45 times

0

I am trying to get the data of the authenticated user, I read some things and I was trying, I was able to get all the user’s data but I can not only get information, as for example the name of the logged in user.

 // Middleware
        app.use((req, res, next) => {
            res.locals.usuario = req.user
            res.locals.success_msg = req.flash("success_msg")
            res.locals.error_msg = req.flash("error_msg")
            res.locals.error = req.flash("error")
            res.locals.user = req.user || null;
            next();
        })

index js.

<h4>Categorias: </h4>
<hr>
{{usuario}}
{{#each categorias}}
   <a href="/categorias/{{slug}}"> <h5>{{nome}}</h5></a>
{{else}}

{{/each}}

When I authenticate and enter the page informed below, appears all user information, I just wanted to take the name for example. What could I be missing?

  • I didn’t get it right, you want to take a person’s name in the middleware where the token is generated?

  • 1

    Yes, I had forgotten to leave the solution of this problem, I managed to solve today! Vlw ae anyway

1 answer

0


The handlebars it with the new updates it "blocks" access to information of this type, but it has to be requesting this data normally, just be putting the following excerpt in the code

 app.use(function (req, res, next) {
            if (req.user) {
              res.locals.usuarioLogado = req.user.toObject();
            }
            next();
          });

With this all the information of the logged-in user can be used.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.