如何使用React存储API身份验证令牌?

问题描述 投票:0回答:1

我正在尝试从我的API存储一个身份验证令牌,因此我可以使用它向另一个API发出GET请求,我的代码如下:

fetch('APIURL', {
        method: 'POST',
        body: JSON.stringify({
            username: currentUser.username,
            password: currentUser.password,
        }),
    })
        .then(response => console.log(response))
        .then(json => console.log(json))

用户名和密码来自我的表单输入,我已经成功发出了POST请求,并在浏览器中获得了令牌,但是我想将其存储在我的代码中,在此先感谢:)

reactjs authentication
1个回答
0
投票

您可以使用localstorage设置并获取身份验证令牌。

要在本地存储中保存字符串,可以使用

window.localStorage.setItem(key, value);

您稍后可以通过以下方式获取值:

window.localStorage.getItem(key);
© www.soinside.com 2019 - 2024. All rights reserved.