express session inSession: false

express session inSession: false

i keep getting

i keep getting

inSession:false

when logging in, it should return true.

I'm using express session, along with postges, sequalize.

I console.log the state values and it renders the values, so its not undefined.

A deeper error

Proxy error: Could not proxy request /api from localhost:3000 to http://localhost:5000/(ECONNREFUSED).

routes/users.js (handles the login logic)

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 That handles the Front End.

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;

server ...

     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 (front 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)); }

...


javascript express

What's new in Bootstrap 5 and when Bootstrap 5 release date?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Random Password Generator Online

HTML Color Picker online | HEX Color Picker | RGB Color Picker

The essential JavaScript concepts that you should understand

The essential JavaScript concepts that you should understand - For successful developing and to pass a work interview

Function Expression in JavaScript

We'll be learning about function expression in JavaScript. Make sure that you learn about functions in JavaScript before learning Fu...

Grokking Call(), Apply() and Bind() Methods in JavaScript

In this article, we will have a look at the call(), apply() and bind() methods of JavaScript. Basically these 3 methods are used to control the invocation of the function.

JavaScript course : Function Declaration Vs Function expression (Difference)

In this video we'll be learning about difference between function declaration and function expression in JavaScript. Syntax difference and hoisting difference explained.

JavaScript Tutorial: if-else Statement in JavaScript

This JavaScript tutorial is a step by step guide on JavaScript If Else Statements. Learn how to use If Else in javascript and also JavaScript If Else Statements. if-else Statement in JavaScript. JavaScript's conditional statements: if; if-else; nested-if; if-else-if. These statements allow you to control the flow of your program's execution based upon conditions known only during run time.