How do I store JWT and send them with every request using react

前端 未结 3 508
星月不相逢
星月不相逢 2020-12-09 09:15

So happy right know because I got my basic registration/authentication system going on.

so basically I got this :

app.post(\'/login\', function(req,r         


        
3条回答
  •  一整个雨季
    2020-12-09 09:25

    1- login component send a login request to the API server endpoint

    2- server API endpoint returns a token

    3- I save the token in user's localStorage

    4- all the API calls from now on will have in the header

    Example: https://github.com/joshgeller/react-redux-jwt-auth-example

    Security update: As @Dan mentioned in the comment, tokens should not be stored in Localstorage because every javascript script has access to that one, which means third party scripts you don't own could access tokens and do whatevery they want with it.

    A better place is to store it as a Cookie with HttpOnly flag.

提交回复
热议问题