D
D
Dmitry2020-08-15 15:43:57
Express.js
Dmitry, 2020-08-15 15:43:57

When requesting from a React application to the server, passport.session() does not occur and the user is subsequently passed to req.user. What am I doing wrong?

I'm writing a small chat application with front and back. When I tested the backend through Postman, everything worked fine for me. But when I tried to send a request inside the React component, deserialization on the server did not occur.

// Application code

const express = require("express");
const app = express();
const passport = require("passport");
const cors = require("cors")
const Router = require("./route/route")
const session = require("express-session");
const FileStor = require("session-file-store")(session);


app.use(cors());
app.use(express.json());
app.use(express.urlencoded());
app.use(session(
    {
    secret:"Zal",
    store:new FileStor,
        cookie:{
        path:"/",
        httpOnly:true,
        maxAge:60*60*1000*24
    },
    resave:false,
    saveUninitialized:false
    }
))

require("./passport/passport");
app.use(passport.initialize());
app.use(passport.session());
app.use(Router)


module.exports = app

//passport.js code
const passport = require("passport");
const LocalStrategy = require("passport-local").Strategy;
const user = require("../model/user")


passport.serializeUser(function(user, done) { 
    console.log("Серелизация");        
    user.id = 0   
    done(null, user.login);
  });
  
passport.deserializeUser(function(login, done) {  
    console.log("Десерелизация",login);      
    user.get(login,(err,res)=>{
        if(err)throw err;        
        done(null,res)
    });

});

passport.use(new LocalStrategy({usernameField: 'login',passwordField: 'password'},
    function(login, password, done) {
      let obj = {
        login,password
      }
      console.log(login,password);
      user.getUser(obj,(err,result)=>{        
          if(err)return done(err);
          if(!result[0])return done(null,false); 
          return done(null,result[0])
      })
    }
  ));

  module.exports = passport


//The React component itself
function Chat({saveMassages}){
    useEffect(()=>{
        fetch("http://localhost:9000/getAllMassage")
        .then(responce=>responce.json())
        .then(arr=>{
            
            saveMassages(arr)
        })
    })
    return(
        <div className="chat">
            <Header></Header>
            <ChatsBar></ChatsBar>
        </div>
    )
}

const mapDispatchToProps={
    saveMassages
}
export default connect(null,mapDispatchToProps)(Chat)


Also tried setting proxy in package.json on client
{
  "proxy":"http://localhost:9000",
  "name": "front",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^4.2.4",
    "@testing-library/react": "^9.5.0",
    "@testing-library/user-event": "^7.2.1",
    "react": "^16.13.1",
    "react-dom": "^16.13.1",
    "react-redux": "^7.2.1",
    "react-router-dom": "^5.2.0",
    "react-scripts": "3.4.2",
    "redux": "^4.0.5"
  },

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2020-08-16
@JaaJ

Found what the problem is. Normal fetch does not pass cockie and therefore had to. add after route {
method: 'GET',
credentials: 'include'
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question