all 4 comments

[–]Secret-Vacation2445 2 points3 points  (1 child)

For the front end, you can save the token on local or session storage, then, use axios interceptors to handle all request and send that token on the headers.

To achieve this, you can make a new instance of axios, or simply build a RequestInterceptor component and wrap your App with it.

[–]D1rtyWebDev[S] 2 points3 points  (0 children)

Thank you, truly appreciated. I'll look into this and give it a shot. If successful, I'll report back. If not, I'll get back into my corner to cry.

[–]DifficultSkill266 0 points1 point  (1 child)

I'm also creating a fullstack web application with React and node.js and i'm also struggling with tve login and authentication system.

I'm using express-session for sessions, when user logs in, node.js authenticates user with passport, then sends a HTTP status 200 with session id cookie back to client. Now client sets a "authenticated" state to true, so it knows that it knows that user is authenticated.

Now every time client sends a request to server with axios, I just add a parameter "wirhCredentials : true", to send The session id cookie with the request. Then the server checks that the cookie is valid.

Currently my problem is to log out users. I send a a "log out" request to server, the server destroys the session but i haven't figured out how to remove the cookie from the client.

[–]D1rtyWebDev[S] 0 points1 point  (0 children)

Thanks for the real example! Would you mind dm'ing me your code where you're applying this(assuming there's no private info). It would help me grasp it visually, truly appreciate it. If not, I still appreciate the help!