gpt4 book ai didi

javascript - ReactJS:如何在 Action 中正确实现 react-cookie?

转载 作者:行者123 更新时间:2023-11-30 14:17:37 26 4
gpt4 key购买 nike

我读过一些文章,说将 token 保存在 localstorage 中对 XSS 攻击是危险的。所以我决定将我的 token 存储在 cookie 中。我正在使用 react-cookie .我看到了例子,我正在尝试这样做,但我的 auth.jsconst 组成,不是一个类,所以我不知道如何使用 withCookies() 有了它,这是我的 auth.js 我想将 token 存储到 cookies 的地方:

import {
LOGIN,
LOGIN_SUCCESS,
LOGIN_FAILED,

GET_USER_DATA,
GET_USER_DATA_SUCCESS,
GET_USER_DATA_FAILED,

LOGOUT,
LOGOUT_SUCCESS,
LOGOUT_FAILED,
} from './types'

import axios from 'axios'
var api = require ('./../api.js');


export const login = (email, pass) => {
return (dispatch) => {
dispatch({
type: LOGIN
})
var url = api.logInApi
axios.post(url, {
email: email,
password: pass
})
.then(res => {
dispatch({
type: LOGIN_SUCCESS,
payload: res.data
})
localStorage.setItem('token', res.data.token)
dispatch(getUserData())
})
.catch(err => dispatch({
type: LOGIN_FAILED,
payload: err
}))
}
}



export const getUserData = () => {
return (dispatch) => {
dispatch({
type: GET_USER_DATA
})
var url = api.getUserDataApi
axios.post(url, {}, {headers: {
"Authorization": `Bearer ${localStorage.getItem("token")}`
}})
.then(res => {
dispatch({
type: GET_USER_DATA_SUCCESS,
payload: res.data
})
})
.catch(err => dispatch({
type: GET_USER_DATA_FAILED,
payload: err
}))
}
}


export const logout = () => {
return (dispatch) => {
dispatch({
type: LOGOUT
})
var url = api.logoutApi
axios.post(url, {}, {headers: {
"Authorization": `Bearer ${localStorage.getItem("token")}`
}})
.then(res => {
window.location.replace("")
dispatch({
type: LOGOUT_SUCCESS,
payload: res.data
})
})
.catch(err => dispatch({
type: LOGOUT_FAILED,
payload: err
}))
}
}

现在,我尝试这样做,当然它不起作用:

import {
LOGIN,
LOGIN_SUCCESS,
LOGIN_FAILED,

GET_USER_DATA,
GET_USER_DATA_SUCCESS,
GET_USER_DATA_FAILED,

LOGOUT,
LOGOUT_SUCCESS,
LOGOUT_FAILED,
} from './types'

import axios from 'axios'
import { withCookies, Cookies } from 'react-cookie'; <<added this
var api = require ('./../api.js');

const login = (email, pass) => {
return (dispatch) => {
dispatch({
type: LOGIN
})
const { cookies } = props; <<added this
var url = api.logInApi
axios.post(url, {
email: email,
password: pass
})
.then(res => {
dispatch({
type: LOGIN_SUCCESS,
payload: res.data
})
cookies.set('token', res.data.token, { path: '/' }); <<added this
dispatch(getUserData())
})
.catch(err => dispatch({
type: LOGIN_FAILED,
payload: err
}))
}
}
export default withCookies(login) <<added this(wrong)



const getUserData = () => {
return (dispatch) => {
dispatch({
type: GET_USER_DATA
})
const { cookies } = props; <<added this
var token = cookies.get('token'); <<added this
var url = api.getUserDataApi
axios.post(url, {}, {headers: {
"Authorization": `Bearer ${token}` <<added this(this is where I wanna get the cookie)
}})
.then(res => {
dispatch({
type: GET_USER_DATA_SUCCESS,
payload: res.data
})
})
.catch(err => dispatch({
type: GET_USER_DATA_FAILED,
payload: err
}))
}
}
export default withCookies(getUserData) <<added this(wrong)


const logout = () => {
return (dispatch) => {
dispatch({
type: LOGOUT
})
const { cookies } = props;
var token = cookies.get('token');
var url = api.logoutApi
axios.post(url, {}, {headers: {
"Authorization": `Bearer ${token}` <<added this
}})
.then(res => {
window.location.replace("")
dispatch({
type: LOGOUT_SUCCESS,
payload: res.data
})
})
.catch(err => dispatch({
type: LOGOUT_FAILED,
payload: err
}))
}
}
export default withCookies(logout) <<added this(wrong)

这个是错误的,因为应该只有一个导出默认值。但是我不知道如何将 withCookies 实现为 const,示例中也包含这些,我不知道我是否需要它们或将它们放在哪里:

  static propTypes = {
cookies: instanceOf(Cookies).isRequired
};

constructor(props) {
super(props);

const { cookies } = props;
this.state = {
name: cookies.get('name') || 'Ben'
};
}

另外,另一个问题是,我可以在我的项目中的任何地方访问我的 cookie,对吧?就像我的项目如何访问 localstorage 一样?

我希望有人能帮助我,我是新手。我以前从未使用过 cookie,所以感谢您的考虑。

最佳答案

我个人更愿意使用 js-cookie写入/读取 cookie。

它有一个非常基本的 API:

Cookie.set('cookie_name', 'value') // will set "cookie_name" to "value"
Cookie.get('cookie_name') // will return "value"

这意味着:

const login  = (email, pass, cookie) => {
return (dispatch) => {
dispatch({
type: LOGIN
})
var url = api.logInApi
axios.post(url, {
email: email,
password: pass
})
.then(res => {
dispatch({
type: LOGIN_SUCCESS,
payload: res.data
})
cookies.set('token', res.data.token);
dispatch(getUserData())
})
.catch(err => dispatch({
type: LOGIN_FAILED,
payload: err
}))
}
}

在第三个参数中传递给 login 函数调用 js-cookie 的 Cookie。

现在,您仍然可以使用相同的 react-cookie 包来读取 cookie 值(我相信应该不会有任何冲突)。或者您可以将其替换为 js-cookie。但是,为此,您必须将 Cookie 对象传递给 props。如果您正在使用 Redux 或只是通过 JSX

传递它,我可能会使用 mapStateToProps 来做到这一点

关于javascript - ReactJS:如何在 Action 中正确实现 react-cookie?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53293002/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com