我正在尝试使用身份验证检查创建动态子域。子域按预期工作,但由于我添加了身份验证,它只是被忽略了,至少在大多数情况下。isAuthenticated 总是返回 false,所以理论上它应该总是重定向到登录。但事实并非如此。日志也非常随机地打印(在我看来是什么)。
有人可以帮我弄这个吗?
subdomains.forEach(subdomain => {
const subDir = `${subdomainDir}/${subdomain}/`;
let subExpress = express();
subExpress.use(express.static(__dirname + subDir));
subExpress.get('*', async (req, res) => {
let isAuthenticated = false;
try {
isAuthenticated = await login.isAuthenticated(req.get('authorization'))
} catch (error) {
console.log(error);
}
console.log("subdomain", subdomain)
console.log("isAuthenticated", isAuthenticated, "subdomain === login", subdomain === "login");
if (isAuthenticated || subdomain === "login") {
res.sendFile(`/${subDir}index.html`, {
root: '.'
});
} else {
res.redirect(301, `https://login.${domain}/`);
}
});
app.use(vhost(`${subdomain}.${domain}`, subExpress));
});
let unusedSub = express();
unusedSub.get('*', (req, res) => {
console.log("unusedSub");
res.redirect(`https://dashboard.${domain}/`);
});
app.use(vhost(`*.${domain}`, unusedSub));
app.listen(port, () => console.log(`Listening on port ${port}.`));