[GH-ISSUE #6] Layout related doubt #4

Open
opened 2026-02-26 21:30:40 +03:00 by kerem · 5 comments
Owner

Originally created by @ayush-848 on GitHub (Aug 24, 2024).
Original GitHub issue: https://github.com/RaddyTheBrand/25.NodeJs-Express-EJS-MongoDB--Blog/issues/6

@RaddyTheBrand
I have register 2 users
and I want after login the home page i.e index.ejs say "Hello, <%= username %>"

how to pass the username through both the layouts coz the "/" falls under the main.js

Originally created by @ayush-848 on GitHub (Aug 24, 2024). Original GitHub issue: https://github.com/RaddyTheBrand/25.NodeJs-Express-EJS-MongoDB--Blog/issues/6 @RaddyTheBrand I have register 2 users and I want after login the home page i.e index.ejs say "Hello, <%= username %>" how to pass the username through both the layouts coz the "/" falls under the main.js
Author
Owner

@RaddyTheBrand commented on GitHub (Aug 25, 2024):

Hey Ayush,
Since you already have registered users with usernames in your DB, you can include the username in the JWT token. Currently, your JWT token only includes userId and iat. You can verify that by going to jwt.io and pasting your jwt token there + your secret.

For example, in Routes -> admin.js - Admin Check Login code:

// include username

const token = jwt.sign({ 
      userId: user._id,
      username: username
 }, jwtSecret );

Now you your JWT token (when decoded) should look something like this:

{
  "userId": "66cb35559b287fc273f87c7a",
  "username": "Ayush",
  "iat": 1724593503
}

Then next step would be to decode that and display it on your home page.
In Routes -> main.js include JWT and your JWT secret at the top:

const jwt = require('jsonwebtoken');
const jwtSecret = process.env.JWT_SECRET;

Then in GET Home code you can grab the token from cookies and decode it:

/**
 * GET /
 * HOME
*/
router.get('', async (req, res) => {
  const token = req.cookies.token;

  if(!token) {
    return res.status(401).json( { message: 'Unauthorized'} );
  }

  const decoded = jwt.verify(token, jwtSecret);
  const myUsername = decoded.username;

...

Then you can use "myUsername" to render send it to the page:

    res.render('index', { 
      locals,
      data,
      current: page,
      myUsername,
      nextPage: hasNextPage ? nextPage : null,
      currentRoute: '/'
    });

Finally, in your index.ejs you should have access to myUsername.
<%= myUsername %>

This is one way of doing it :-)
Just make sure that you re-login after you change the code so you get a new JTW token with the username.

I hope this helps!

<!-- gh-comment-id:2308862967 --> @RaddyTheBrand commented on GitHub (Aug 25, 2024): Hey Ayush, Since you already have registered users with usernames in your DB, you can include the username in the JWT token. Currently, your JWT token only includes userId and iat. You can verify that by going to jwt.io and pasting your jwt token there + your secret. For example, in Routes -> admin.js - Admin Check Login code: // include username ``` const token = jwt.sign({ userId: user._id, username: username }, jwtSecret ); ``` Now you your JWT token (when decoded) should look something like this: ``` { "userId": "66cb35559b287fc273f87c7a", "username": "Ayush", "iat": 1724593503 } ``` Then next step would be to decode that and display it on your home page. In Routes -> main.js include JWT and your JWT secret at the top: ``` const jwt = require('jsonwebtoken'); const jwtSecret = process.env.JWT_SECRET; ``` Then in GET Home code you can grab the token from cookies and decode it: ``` /** * GET / * HOME */ router.get('', async (req, res) => { const token = req.cookies.token; if(!token) { return res.status(401).json( { message: 'Unauthorized'} ); } const decoded = jwt.verify(token, jwtSecret); const myUsername = decoded.username; ... ``` Then you can use "myUsername" to render send it to the page: ``` res.render('index', { locals, data, current: page, myUsername, nextPage: hasNextPage ? nextPage : null, currentRoute: '/' }); ``` Finally, in your index.ejs you should have access to myUsername. <%= myUsername %> This is one way of doing it :-) Just make sure that you re-login after you change the code so you get a new JTW token with the username. I hope this helps!
Author
Owner

@ayush-848 commented on GitHub (Aug 25, 2024):

Hey Ayush, Since you already have registered users with usernames in your DB, you can include the username in the JWT token. Currently, your JWT token only includes userId and iat. You can verify that by going to jwt.io and pasting your jwt token there + your secret.

For example, in Routes -> admin.js - Admin Check Login code:

// include username

const token = jwt.sign({ 
      userId: user._id,
      username: username
 }, jwtSecret );

Now you your JWT token (when decoded) should look something like this:

{
  "userId": "66cb35559b287fc273f87c7a",
  "username": "Ayush",
  "iat": 1724593503
}

Then next step would be to decode that and display it on your home page. In Routes -> main.js include JWT and your JWT secret at the top:

const jwt = require('jsonwebtoken');
const jwtSecret = process.env.JWT_SECRET;

Then in GET Home code you can grab the token from cookies and decode it:

/**
 * GET /
 * HOME
*/
router.get('', async (req, res) => {
  const token = req.cookies.token;

  if(!token) {
    return res.status(401).json( { message: 'Unauthorized'} );
  }

  const decoded = jwt.verify(token, jwtSecret);
  const myUsername = decoded.username;

...

Then you can use "myUsername" to render send it to the page:

    res.render('index', { 
      locals,
      data,
      current: page,
      myUsername,
      nextPage: hasNextPage ? nextPage : null,
      currentRoute: '/'
    });

Finally, in your index.ejs you should have access to myUsername. <%= myUsername %>

This is one way of doing it :-) Just make sure that you re-login after you change the code so you get a new JTW token with the username.

I hope this helps!

Sure, thanks

<!-- gh-comment-id:2308881805 --> @ayush-848 commented on GitHub (Aug 25, 2024): > Hey Ayush, Since you already have registered users with usernames in your DB, you can include the username in the JWT token. Currently, your JWT token only includes userId and iat. You can verify that by going to jwt.io and pasting your jwt token there + your secret. > > For example, in Routes -> admin.js - Admin Check Login code: > > // include username > > ``` > const token = jwt.sign({ > userId: user._id, > username: username > }, jwtSecret ); > ``` > > Now you your JWT token (when decoded) should look something like this: > > ``` > { > "userId": "66cb35559b287fc273f87c7a", > "username": "Ayush", > "iat": 1724593503 > } > ``` > > Then next step would be to decode that and display it on your home page. In Routes -> main.js include JWT and your JWT secret at the top: > > ``` > const jwt = require('jsonwebtoken'); > const jwtSecret = process.env.JWT_SECRET; > ``` > > Then in GET Home code you can grab the token from cookies and decode it: > > ``` > /** > * GET / > * HOME > */ > router.get('', async (req, res) => { > const token = req.cookies.token; > > if(!token) { > return res.status(401).json( { message: 'Unauthorized'} ); > } > > const decoded = jwt.verify(token, jwtSecret); > const myUsername = decoded.username; > > ... > ``` > > Then you can use "myUsername" to render send it to the page: > > ``` > res.render('index', { > locals, > data, > current: page, > myUsername, > nextPage: hasNextPage ? nextPage : null, > currentRoute: '/' > }); > ``` > > Finally, in your index.ejs you should have access to myUsername. <%= myUsername %> > > This is one way of doing it :-) Just make sure that you re-login after you change the code so you get a new JTW token with the username. > > I hope this helps! Sure, thanks
Author
Owner

@ayush-848 commented on GitHub (Oct 1, 2024):

hi @RaddyTheBrand
please see this https://myday-gklz.onrender.com/
I have added few extra stuffs

<!-- gh-comment-id:2386200477 --> @ayush-848 commented on GitHub (Oct 1, 2024): hi @RaddyTheBrand please see this https://myday-gklz.onrender.com/ I have added few extra stuffs
Author
Owner

@RaddyTheBrand commented on GitHub (Oct 1, 2024):

That's pretty cool! Everything seems to work quite well

<!-- gh-comment-id:2386232124 --> @RaddyTheBrand commented on GitHub (Oct 1, 2024): That's pretty cool! Everything seems to work quite well
Author
Owner

@ayush-848 commented on GitHub (Oct 1, 2024):

That's pretty cool! Everything seems to work quite well

Thanks

<!-- gh-comment-id:2386254981 --> @ayush-848 commented on GitHub (Oct 1, 2024): > That's pretty cool! Everything seems to work quite well Thanks
Sign in to join this conversation.
No labels
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/25.NodeJs-Express-EJS-MongoDB--Blog#4
No description provided.