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

...


Angular 9 Tutorial: Learn to Build a CRUD Angular App Quickly

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

Brave, Chrome, Firefox, Opera or Edge: Which is Better and Faster?

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

What is new features in Javascript ES2020 ECMAScript 2020

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.

How to Retrieve full Profile of LinkedIn User using Javascript

I am trying to retrieve the full profile (especially job history and educational qualifications) of a linkedin user via the Javascript (Fetch LinkedIn Data Using JavaScript)