Клиент не может получить куки от получения ответа - PullRequest
0 голосов
/ 17 февраля 2019

У меня есть сервер node.js, он должен установить токен в куки клиента.Он устанавливает токен в Postman, но не в моем приложении.

Я попытался добавить учетные данные "same-origin" и "include" в методе fetch в Vue.Это не помогло.

Включить интерфейсную часть vue.js

checkLogin(context) {
    const token = document.cookie;
    console.log("Cookie: " + token); //returns "Cookie: "
    fetch(PROXY_URL + BASE_URL + "checkLogin", {
      method: "POST"
    })
      .then(r => r.json())
  .then(d => {
    console.log(d);
    return d;
  })
  .then(d => {
    context.commit("setIsLoggedIn", d.auth);
  });
},
login(context, data) {
fetch(PROXY_URL + BASE_URL + "login", {
  method: "POST",
  body: JSON.stringify(data),
  headers: {
    Accept: "application/json",
    "Content-Type": "application/json"
  }
})
  .then(r => console.log(r))
}

Включить компонентную часть node.js

//check login
app.post('/checkLogin',(req,res ) => {
    passport.authenticate('jwt', { session: false }, (err, 
decryptToken, jwtError) => {
        if(jwtError != void(0) || err != void(0)) {
            res.send({auth: false});
        }else{
            res.send({auth: true });
        };
        req.user = decryptToken;

    })(req, res);
});


//login
app.post('/login',async (req, res) => {
    try{
    let user = await db.findUser(req.body);
    if(user != void(0) && bcrypt.compareSync(req.body.password, user.password)){

        const token = createToken({id: user._id, username: user.username});

        res.cookie('token', token, {
            httpOnly: false
        });

        res.status(200).send("You are logged");

    } else
        res.status(400).send({message: "User not exist or password not correct"});

    }catch(e){
        console.error("Error: login: ,", e);
        res.status(500).send({message: "some error"});
    }
});

Не установлен токенв печенье, но так и должно быть.

...