通过快速会话获得问题并做出反应

时间:2017-07-02 22:42:55

标签: reactjs session express

更新 添加的代码,适用于邮递员,但不适用于反应

我开始对基本的auth app做出反应,但似乎会话不是持久性的。我有一个快速api,它运行在端口8080上,我的反应应用程序在3000上运行。

我做了一个登录页面,在api上发布请求并在服务器上创建会话。但是,当我请求isAuthenticated()护照方法时,返回false。

这是我的护照配置:


      app.use(session({ secret: 'ilovescotchscotchyscotchscotch', cookie: {maxAge : 86400000, secure : false, httpOnly : false}, resave : false, saveUninitialized : true })); // session secret

此外,当我调用另一个api url时,session对象为空:


    router.get('/test', (req, res) => {
        console.log(req.session) //returns empty
    })

开始做反应:


    axios.post('http://localhost:3000/api/v1/auth/login', {
          login: inputLogin,
          password: inputPassword
        })
        .then(res => {
          console.log(res,this)  // works
          // this.props.history.push('/dashboard')
          axios.get('http://localhost:3000/api/v1/auth/isAuthenticated')
          .then( res => {
            console.log('isAuthenticated ?', res)
          })
          .catch( err => {
            console.error(err);
          })

        })
        .catch(err => {
          console.info(err);
        })

登录已发送标题


    Accept:application/json, text/plain, */*
    Accept-Encoding:gzip, deflate, br
    Accept-Language:fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4
    Connection:keep-alive
    Content-Length:37
    Content-Type:application/json;charset=UTF-8
    DNT:1
    Host:localhost:3000
    Origin:http://localhost:8080
    Referer:http://localhost:8080/
    User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36

登录回复标题


    Access-Control-Allow-Credentials:true
    Access-Control-Allow-Headers:X-Requested-With,content-type
    Access-Control-Allow-Methods:GET, POST, OPTIONS, PUT, PATCH, DELETE
    Access-Control-Allow-Origin:http://localhost:8080
    Cache-Control:no-cache, no-store, must-revalidate
    Connection:keep-alive
    Content-Length:34
    Content-Type:application/json; charset=utf-8
    Date:Mon, 03 Jul 2017 14:08:57 GMT
    ETag:W/"22-SiewJVLmTt/LdfZZq/tT3axots0"
    Expires:0
    Pragma:no-cache
    set-cookie:connect.sid=s%3AE44uhyOcrptOlYWuQY6_qtFHi4dSVqrw.bPYLhVKQCO2nrVMIRr6fK5XLFT%2F0AxcmZVx86z%2Be8C4; Domain=localhost; Path=/; Expires=Tue, 04 Jul 2017 14:08:57 GMT
    Vary:Accept-Encoding
    X-Powered-By:Express

IsAuthenticated请求标题


    Accept:application/json, text/plain, */*
    Accept-Encoding:gzip, deflate, sdch, br
    Accept-Language:fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4
    Connection:keep-alive
    DNT:1
    Host:localhost:3000
    Origin:http://localhost:8080
    Referer:http://localhost:8080/
    User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36

IsAuthenticated响应标题


    Access-Control-Allow-Credentials:true
    Access-Control-Allow-Headers:X-Requested-With,content-type
    Access-Control-Allow-Methods:GET, POST, OPTIONS, PUT, PATCH, DELETE
    Access-Control-Allow-Origin:http://localhost:8080
    Cache-Control:no-cache, no-store, must-revalidate
    Connection:keep-alive
    Content-Length:5
    Content-Type:application/json; charset=utf-8
    Date:Mon, 03 Jul 2017 14:08:57 GMT
    ETag:W/"5-fLbvuYullyqbUJDcLlF/4U0SywQ"
    Expires:0
    Pragma:no-cache
    set-cookie:connect.sid=s%3A0z6yb4ltDn_Ymym8XcHIt7COq_QQ4ty3.pxCBJboowCq99TKG5CmiVqj3sEA3Z9kKliS5YSgSvCQ; Domain=localhost; Path=/; Expires=Tue, 04 Jul 2017 14:08:57 GMT
    Vary:Accept-Encoding
    X-Powered-By:Express

这让我登录确定但isAuthenticated返回false。在邮递员工作。

有没有办法解决这个问题?

感谢

0 个答案:

没有答案