所以我正在尝试与node.js和vue.js建立会话。这是我的处理方式:
index.js:
const session = require('express-session');
const app = express();
// middle ware
app.use(bodyParser.json());
app.use(cors());
//middle ware sessions
app.use(session({
secret: 'kittens',
resave: false,
saveUninitialized: true,
cookie: { secure: true }
}));
我的登录功能(长):
// Login
router.post('/login', async (req, res) => {
const currentUser = await userDb();
// Check blank
let emailBody = req.body.email.length;
let passBody = req.body.password.length;
if (!emailBody) {
Object.assign(error, {
'blankEmail': 'Email can not be blank!',
});
}
if (!passBody) {
Object.assign(error, {
'blankPassword': 'Password can not be blank!',
});
}
if (!emailBody || !passBody) {
res.status(201).send(error);
}
// See if member exists
const doesExist = await currentUser.findOne({ 'email': `${req.body.email}` });
if (doesExist) {
// if user exists check password
let passGood = bcrypt.compareSync(req.body.password, doesExist.password);
if (passGood) {
console.log(`${doesExist.username} has logged in`);
req.session.userData = doesExist;
let data = {
redirect: true,
}
req.session.save();
res.status(201).send(data);
} else {
console.log('Email or Password incorrect');
Object.assign(error, {
'wrongCredits': 'Email or Password incorrect',
});
res.status(201).send(error);
}
} else {
Object.assign(error, {
'userNotExist': 'Email or Password incorrect',
});
res.status(201).send(error);
}
});
然后我将成功登录到/api/game
的地址
router.get('/', async (req, res )=> {
console.log(req.session);
});
因此将保存会话,并将其正确保存在登录功能中。但是,我的问题是在那之后,因为它似乎在更改路线甚至刷新后都不希望工作。我的目标是:无论用户在网站上的什么位置,都可以正确传输会话,并保持刷新状态。