6

I cant understand how i can set cookie (connect sid) in REACT from my back-end.

I have a Passport Local Strategy

Here is my server.js

app.use(cookieParser());
app.use(session({
  resave: true,
  saveUninitialized: false,
  secret: 'bla bla bla',
  cookie: { httpOnly: false }
}));
app.use(passport.initialize());
app.use(passport.session());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));

Here is my route.js

app.post('/login', (req, res, next) => {
    passport.authenticate('local-login', (err, user, info) => {
        if (err) { return res.send(err); }
        if (!user) { return res.send(info); }
        req.logIn(user,(err) => {
            if (err) { return next(err); }
            return res.send({status: true, user});
        });
    })(req, res, next);
});

app.post('/test', isLoggedIn, (req, res) => {
    res.send('Logged User')
});

And here is my react file

componentWillMount() {
        axios({
            method: 'post',
            url: 'http://localhost:8080/test',
        }).then(({data}) => {
            console.log(data);

So when i use postman everything works fine i can log in and my is LoggedIn function is working, same if i use my browser with my route.

The problem is when i use React .. i can login and i receive exactly what i want but nothing is stored in my cookie and i have no idea how to do it..

I m kind of new to React so if anyone has a clue..

Thanks

kikiwie
  • 390
  • 1
  • 3
  • 12

0 Answers0