Moran_bzh
Moran_bzh

Reputation: 49

How use JWT payload to store session user data?

I am building a web application with an API BACK-END side written in Symfony and a FRONT-END side written with Angular 7. My problem is to retrieve user data from the back-end and to store it in session on front-end side. I use JWT for authentification between the front and the back-end.

I want to use the JWT payload to store user data from the back-end. I have read that it is not safe to store the JWT token in localStorage and I have to store it in cookies with httpOnly and secure options. But if i use this system, my angular front-end app will not be able to read the JWT payload data (not possible to read httpOnly cookies in Javascript).

My question is : how can I use JWT for authentication with httpOnly/secure options AND use the payload data for user session data ? Do I have to use the JWT just for authentification and make another call to the back-end API to retrieve user sessions data ?

Thank you !

Upvotes: 0

Views: 671

Answers (1)

Khaled Sameer
Khaled Sameer

Reputation: 306

To get your the user details from token for your front-end application use like this in javascript

function parseJwt (token) {
        var base64Url = token.split('.')[1];
        var base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/');
        return JSON.parse(window.atob(base64));
    };

Upvotes: 0

Related Questions