为什么 nodeJS 返回不同的 'req.session'?
Why is nodeJS returning different 'req.session'?
我正在尝试验证使用 Google API 登录的用户是否已成功登录。在我的 React App 我获取到 'http://localhost:3001/auth/google/check' 路由来获取这个。
我的 React 应用程序中的提取:
fetch('http://localhost:3001/auth/google/check', {
method: 'GET',
headers: {'Content-Type' : 'application/json'}
})
.then(response => response.json())
.then(data => {
console.log(data)
if(data === 'LOGIN_SUCCESS') {
const {login, changePage} = this.props;
login('LOGIN_SUCCESS');
changePage('GO_TO_PROFILE_PAGE');
}
})
在我的服务器中,我使用 passportJS 进行身份验证:
app.get('/auth/google/check', (req, res) => {
console.log(req.session);
if(req.isAuthenticated()) {
res.status(200).json('LOGIN_SUCCESS');
}
else {
res.status(400).json('Login failed.');
}
})
在我的应用程序页面 ('http://localhost:3000') 上,当我获取时,我收到状态 400,'Login failed' 消息,在服务器端 'req.session.passport' 未定义,但是,当我打开一个新选项卡并将 url 'http://localhost:3001/auth/google/check' 放入 returns 'LOGIN_SUCCESS' 并且在我的服务器端我有 'req.session.passport.user'序列化成功。为什么我收到不同的 'req.session'?
您可能需要配置 fetch
以根据请求传递 cookie。
fetch('http://localhost:3001/auth/google/check', {
method: 'GET',
headers: {'Content-Type' : 'application/json'},
credentials: 'include'
})
我正在尝试验证使用 Google API 登录的用户是否已成功登录。在我的 React App 我获取到 'http://localhost:3001/auth/google/check' 路由来获取这个。
我的 React 应用程序中的提取:
fetch('http://localhost:3001/auth/google/check', {
method: 'GET',
headers: {'Content-Type' : 'application/json'}
})
.then(response => response.json())
.then(data => {
console.log(data)
if(data === 'LOGIN_SUCCESS') {
const {login, changePage} = this.props;
login('LOGIN_SUCCESS');
changePage('GO_TO_PROFILE_PAGE');
}
})
在我的服务器中,我使用 passportJS 进行身份验证:
app.get('/auth/google/check', (req, res) => {
console.log(req.session);
if(req.isAuthenticated()) {
res.status(200).json('LOGIN_SUCCESS');
}
else {
res.status(400).json('Login failed.');
}
})
在我的应用程序页面 ('http://localhost:3000') 上,当我获取时,我收到状态 400,'Login failed' 消息,在服务器端 'req.session.passport' 未定义,但是,当我打开一个新选项卡并将 url 'http://localhost:3001/auth/google/check' 放入 returns 'LOGIN_SUCCESS' 并且在我的服务器端我有 'req.session.passport.user'序列化成功。为什么我收到不同的 'req.session'?
您可能需要配置 fetch
以根据请求传递 cookie。
fetch('http://localhost:3001/auth/google/check', {
method: 'GET',
headers: {'Content-Type' : 'application/json'},
credentials: 'include'
})