Hey everyone! I'm building a chat application using React, Express, and Socket.IO, and I'm a bit stuck on how to include the username in the message prefix, like 'From: '. I initially thought about using a JWT token stored in cookies, but that doesn't seem quite right. Then I considered writing the response data to localStorage after login/register, but that also feels off. I'm wondering if I should implement a custom authContext hook or if there's a better way to handle this. I'm not asking for code reviews, just interested in some different implementation ideas. How would you approach adding a username to messages?
1 Answer
Why not just include the username directly in the message structure? For example, you could use something like `{from: "username", message: "Hello, World"}`. It's a straightforward solution and keeps everything tidy.
But I don't have access to the username on the frontend. I'm not sure where I should be fetching that data from, or if it should be handled on the front or back end. Plus, I'd prefer a nicer convention rather than just random IDs.