会话不保存后获取post请求与express [英] session does not save after fetch post request with express

查看:236
本文介绍了会话不保存后获取post请求与express的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过提取并将令牌保存到快速api后端中的会话,我发出了请求。但是,令牌不会保存在会话中,即使我在获取中设置凭据到 include 任何人都能识别我的问题吗?

  fetch(' http:// localhost:3000 / api / users',{
method:'post',
header:{
'Accept':'application / json',
' Content-Type':'application / json'
},
credentials:'include',
body:JSON.stringify({
accessToken:user.accessToken,

})
})
.then(res => res.json())
.then(json => {
console.log json);
})
.catch(error => {
console.log(error);
});

服务器端:

  app.use(morgan('dev'))
app.use(cors({credentials:true}));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:false})));
app.use(cookieParser());
app.use(session({secret:'ssshhhhh',resave:true,saveUninitialized:true}));


app.post('/ api / users',function(req,res){
if(!req.session.accessToken){

req.session.accessToken = req.body.accessToken;
}
...
...
})

app.get ('/ api / users / token',function(req,res){
console.log(req.session);
if(req.session.accessToken){
return res。 JSON({的accessToken:req.session.accessToken});
}
返回res.json({的accessToken:空});
})



从会话中检索令牌:

  fetch 'http:// localhost:3000 / api / users / token',{
credentials:'include'
})
.then(checkStatus)
。 > {
return response.json();
})
.then(json => {
console.log(json);
})
.catch(err => {
console.error(err);
});

编辑
这是请求的标题:

  {host:'localhost:3000',
accept:'* / *',
'if-没有匹配':'W /4D-oW2yTGBOs6aaA4LAPgNxNQ,
饼干:connect.sid = S%3AHXDL6Az0hRZeSGsJgjhw4kvVONBLz-yn.sOOAqbxaLQ6Z%2FfCdfGOmXf9XsYl3JdHvP%2FmfkUln1xA',
'的用户代理: cairn / 1 CFNetwork / 758.2.8 Darwin / 15.3.0',
'accept-language':'en-us',
'accept-encoding':'gzip,deflate',
connection:'keep-alive'}
会话{
cookie:
{path:'/',
_expires:null,
originalMaxAge:null,
httpOnly:false}}
GET / api / users / token 304 23.780 ms - -
{host:'localhost:3000',
'content-type':'application / json ',
饼干:connect.sid = S%3A1jat379BSZQCKvJV1pKL4_Cub9ApMFVI.m%2BQHkIewMl9Uzjg315GTC5aD5qpWESiQVNDnXrFFSbQ',
连接:'保活',
如果 - 无 - 匹配:W / 270 toaMUba3dhoaPENGp5EGaA,
接受:应用/ JSON',
接受语言:EN-US,
内容长度':'484',
'的Accept-Encoding':'gzip的,放气',
'的用户代理:山/ CFNetwork的1 / 758.2.8达尔文/ 15.3.0'}


解决方案

我在我的博客上回答了这个问题: http://tonyspiro.com/how-to-keep-session-data -after-fetch-post-with-express /#comment-2544007117


Im making a post request with fetch and saving a token to session in an express api backend. However, the token wont be saved in the session even though i set credentials to include in fetch. Can anybody identify the problem for me? Thanks.

sending token to api server:

 fetch('http://localhost:3000/api/users', {
    method: 'post',
    headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json'
    },
    credentials: 'include',
    body: JSON.stringify({
      accessToken: user.accessToken,

    })
  })
  .then(res => res.json())
  .then(json => {
    console.log(json);
  })
  .catch(error => {
    console.log(error);
  });

server side:

app.use(morgan('dev'))
app.use(cors({credentials: true}));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(session({secret: 'ssshhhhh', resave: true, saveUninitialized: true}));


app.post('/api/users', function(req, res) {
  if (!req.session.accessToken) {

    req.session.accessToken = req.body.accessToken;
  }
  ...
  ...
})

app.get('/api/users/token', function(req, res) {
  console.log(req.session);
  if (req.session.accessToken) {
    return res.json({ accessToken: req.session.accessToken });
  }
  return res.json({ accessToken: null });
})

retrieving token from session:

fetch('http://localhost:3000/api/users/token', {
  credentials: 'include' 
})
.then(checkStatus)
.then(response => {
  return response.json();
})
.then(json => {
  console.log(json);
})
.catch(err => {
  console.error(err);
});

Edit: this is headers from request:

{ host: 'localhost:3000',
  accept: '*/*',
  'if-none-match': 'W/"4d-oW2yTGBOs6aaA4LAPgNxNQ"',
  cookie: 'connect.sid=s%3AHXDL6Az0hRZeSGsJgjhw4kvVONBLz-yn.sOOAqbxaLQ6Z%2FfCdfGOmXf9XsYl3JdHvP%2FmfkUln1xA',
  'user-agent': 'cairn/1 CFNetwork/758.2.8 Darwin/15.3.0',
  'accept-language': 'en-us',
  'accept-encoding': 'gzip, deflate',
  connection: 'keep-alive' }
Session {
  cookie:
   { path: '/',
     _expires: null,
     originalMaxAge: null,
     httpOnly: false } }
GET /api/users/token 304 23.780 ms - -
{ host: 'localhost:3000',
  'content-type': 'application/json',
  cookie: 'connect.sid=s%3A1jat379BSZQCKvJV1pKL4_Cub9ApMFVI.m%2BQHkIewMl9Uzjg315GTC5aD5qpWESiQVNDnXrFFSbQ',
  connection: 'keep-alive',
  'if-none-match': 'W/"270-toaMUba3dhoaPENGp5EGaA"',
  accept: 'application/json',
  'accept-language': 'en-us',
  'content-length': '484',
  'accept-encoding': 'gzip, deflate',
  'user-agent': 'cairn/1 CFNetwork/758.2.8 Darwin/15.3.0' }

解决方案

I've answered this back on my blog: http://tonyspiro.com/how-to-keep-session-data-after-fetch-post-with-express/#comment-2544007117

这篇关于会话不保存后获取post请求与express的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆