mirror of
https://github.com/RaddyTheBrand/25.NodeJs-Express-EJS-MongoDB--Blog.git
synced 2026-04-25 06:35:57 +03:00
[GH-ISSUE #6] Layout related doubt #4
Labels
No labels
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
starred/25.NodeJs-Express-EJS-MongoDB--Blog#4
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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
@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
Now you your JWT token (when decoded) should look something like this:
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:
Then in GET Home code you can grab the token from cookies and decode it:
Then you can use "myUsername" to render send it to the page:
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!
@ayush-848 commented on GitHub (Aug 25, 2024):
Sure, thanks
@ayush-848 commented on GitHub (Oct 1, 2024):
hi @RaddyTheBrand
please see this https://myday-gklz.onrender.com/
I have added few extra stuffs
@RaddyTheBrand commented on GitHub (Oct 1, 2024):
That's pretty cool! Everything seems to work quite well
@ayush-848 commented on GitHub (Oct 1, 2024):
Thanks