快速会话inSession:false

时间:2019-01-30 00:19:52

标签: javascript express

我不断得到

  

inSession:false

enter image description here

登录时,它应该返回true。

我正在使用express session以及邮寄费用来使自己保持平衡。

我console.log状态值并呈现值,所以它不是未定义的。

更深的错误

  

代理错误:无法将请求/ api从localhost:3000代理到   http://localhost:5000/(拒绝)。

routes / users.js (处理登录逻辑)

router.post('/login', function(req, res, next) {
  const email = req.body.email;
  const password = req.body.password;
  User.findOne({
    where: {email: email}

  }).then( user => {

    if(!user){
      res.status(200).send({ incorrectEmail: true, inSession: false, msg: "Incorrect Email" })
    }else if(!user.validPassword(password)){
      res.status(200).send({ incorrectPassword: true, inSession: false, msg: "Incorrect Password" })
    }else{
      res.status(200).send({
        inSession: true, msg: "Logged in!", loggedEmail: user.email
      })
    }

  }).catch(err => next(err))
});

signIn.js 处理前端。

import React, { Component } from 'react';
import TextField from '@material-ui/core/TextField';
import Button from '@material-ui/core/Button';
import axios from 'axios';
class signIn extends Component{

    constructor(props){
        super(props)

        this.state = {
            email:"",
            password: "", 
            loggedEmail:"",
            loginError: "",    
            userLoggedIn: false,
            emailBlank: true,
            passwordBlank: true,
            emailInvalid: false,
            passwordInValid: false,
        }

        this.handleChange = this.handleChange.bind(this);

    }

    handleChange = (e) =>{
        e.preventDefault();

        this.setState({
            [e.target.name]: e.target.value
        });

    }


    handleSubmit = () => {

        this.setState({
            email: this.state.email, 
            password: this.state.password


        });

        if (!this.state.emailBlank && !this.state.passwordBlank){
            axios.post('/api/users/login',{
                email: this.state.email, 
                password: this.state.password


            }).then ( res => { 
                if (res.data.incorrectEmail|| res.data.incorrectPassword ){
                    this.setState({ loginError: res.data.msg})
                }
                this.setState({ userLoggedIn: res.data.inSession, loggedEmail: res.data.loggedEmail})

            }).catch( err => console.log(err))

        }else{
            this.setState({ emailInvalid: true, passwordInValid: true})

            console.log(  this.state.emailInvalid, this.state.passwordInValid)
        }

    }

    render(){
        return (
            <div style={ {padding: '20px 100px'}}>
            <h1>Sign In</h1>
            <form onSubmit={this.handleSubmit}>      
                <TextField
                    id="outlined-name"
                    label="Email"
                    className=""
                    style={{width: 560}}
                    name="email"
                    value={this.state.email}
                    onChange={this.handleChange}
                    margin="normal"
                    variant="outlined"
                />  
                <br></br>
                <TextField
                    id="outlined-name"
                    label="Password"
                    name="password"
                    type="password"
                    style={{width: 560}}
                    className=""
                    value={this.state.password}
                    onChange={this.handleChange}
                    margin="normal"
                    variant="outlined"
                />  

                <br></br>

                <button type="submit"> Submit </button>

            </form>

            </div>

        );
    }





}

export default signIn;

服务器       ...

     app.use(session({
       key:'user_sid',
       secret: 'something',
       resave: false,
       saveUninitialized: false,
       cookie: {
       expires: 600000
      } 
     }))


    app.use((req, res, next) => {
      if (req.cookies.user_sid && !req.session.user){
        res.clearCookie('user_sid');
      }
      next();
    })

    sessionChecker = (req, res, next) => {
      if (req.session.user && req.cookies.user_sid){
        res.status(200).send({ inSession: true});
      } else {
        next();
      }
    }

    app.get('/api', sessionChecker, (req, res) => {
      res.status(200).send({ inSession: false });
    });

    app.use('/api/users', userRoute )

App.js(前端app.js)

class App extends Component {

  constructor(props){
    super(props);


    this.state = {
      inSession: false,
      loggedEmail: "",
    }

  }

  componentDidMount() {
    this.checkInSession()
  } 

  checkInSession = () => {
    axios.get('/api').then((res) => {
      this.setState({ inSession: res.data.inSession });
    }).catch(err => console.log(err));
  }

  ...

1 个答案:

答案 0 :(得分:0)

您是否尝试过从服务器上的sessionChecker登录?似乎其中可能有未定义的内容。就我个人而言,我将执行以下操作:

x = list(a = 1:5, b = 3:7, c = 6:9)  ## a list of 3 variables; variable `a` has the smallest 
                                     ## max among all variables in the list, so keep `a`
                                     ## untouched at the top of the output.

x[-1] <- Map(setdiff, x[-1], x[-length(x)]) ## Now, take the values of `b` not shared 
                                            ## with `a`, AND values of `c` not shared   
                                            ## with `b`.

x   

 # Output:                     # This output is OK now, but if we change order of `a`, `b`, 
                               # and `c` in the initial list the output will change.
                               # This is why the constraint above is necessary?
 $a
[1] 1 2 3 4 5

$b
[1] 6 7

$c
[1] 8 9