*更新
它实际上仅存在于修改发生的单个路径中。请求会话在整个应用程序中都是持久的......
我尝试添加这个,但没有雪茄:
app.use(cors({
credentials: true,
}));
app.options('*', cors());
app.use(function(request, response, next) {
response.header("Access-Control-Allow-Origin", "*");
response.header("Access-Control-Allow-Headers", "X-Requested-With");
next();
});
所以我使用快速js(节点api),我有一个设计模式,我为每个路由调用一个特定的函数,我将请求对象传递给该函数,如下所示:
const express = require('express');
const client_sessions = require('client-sessions');
const brain = require('./brain'); // custom module
const chamber = require('./chamber');
const app = express();
app.use(client_sessions({
cookieName: 'session',
secret: chamber.app_secret,
duration: 30 * 60 * 1000,
activeDuration: 5 * 60 * 1000,
cookie: {
httpOnly: false,
secure: false,
ephemeral: false
}
}));
app.put('/signup', function(request, response) {
brain.sign_up(request)
.then(obj => { return response.json(obj) })
.catch(obj => { return response.json(obj) });
});
在大脑模块中,注册功能如下所示:
function sign_up(request) {
return new Promise((resolve, reject) => {
var { username, email, password } = request.body
if(!username || !email || !password) {
return reject({error: true, message: "username, email and/or password not provided..."})
}
if(typeof(username) !== 'string' || typeof(email) !== 'string' || typeof(password) !== 'string') {
return reject({error: true, message: "username, email and password must be of type string..."})
}
var check_username = models.Users.findOne({ where: { username } });
var check_email = models.Users.findOne({ where: { email } });
Promise.all([check_username, check_email])
.then(values => {
// console.log("signup checks: ", values);
var username_result = values[0] ? values[0].get({plain: true}) : false;
var email_result = values[1] ? values[1].get({plain: true}) : false;
if(username_result) { return reject({error: true, message: "username is taken..."}); }
if(email_result) { return reject({error: true, message: "email already in use..."}); }
models.Users.create({ username, email, password: bcrypt.hashSync(password) })
.then(user_data => {
var { id, email, username, date_created, uniqueValue, bio, link, icon } = user_data.dataValues;
var user = { id, email, username, date_created, uniqueValue, bio, link, icon };
var session_id = chamber.uniqueValue();
request.session.id = session_id;
request.session.user = user;
console.log("session --- ", request.session);
return resolve({ user, session_id, message: "Signed Up!", online: true });
})
.catch(error => {
console.log("error: ", error);
return reject({error: true, message: "error processing data..."});
})
})
.catch(error => {
console.log("error: ", error);
return reject({error: true, message: "error fetching data..."});
})
});
}
问题是会话在请求之间不存在。我做了一些麻烦,当我直接在app路由回调函数中修改request.session时,它似乎工作,但当我将它传递给另一个函数并在那里修改它时,它似乎没有。我应该能够做到这一点。为什么不工作?是因为一切都是以编程方式完成的,而不是通过客户端(浏览器)完成的?
答案 0 :(得分:0)
经过大量的阅读和研究,开发人员经常要做的事情,我找到了问题和解决方案。我发送给api的请求没有包含凭据,因此快递应用程序将创建一个全新的会话而不是使用当前的会话。对于在javascript中使用fetch API的用户,fetch()默认情况下不包含凭据。要进行ajax调用,请确保第二个参数(object)具有此键值对---凭据:" include"
这样的事情:
let params = {
method: "GET",
credentials: "include"
}
fetch($vault.api_domain + "/signout", params)
.then(resp => resp.json())
.then(json => {
self.session = json;
return resolve(json);
})
.catch(error => {
console.log("error:", error);
return reject({error: true, message: "error fetching..."});
});