我有 2 个数组:
[2, 4, -2, 4, 1, 3]
["a", "b", "c", "d", "e", "f"]
Run Code Online (Sandbox Code Playgroud)
我希望它们按数值数组排序:
// output
[-2, 1, 2, 3, 4, 4] // <-sorted by numerical order
["c", "e", "a", "f", "b", "d"] // sorted exactly the same order as the first array
Run Code Online (Sandbox Code Playgroud)
虽然如果 "b" 或 "d" 先出现实际上并不重要(在这个例子中它们都有 4 个)
我在网上发现了很多关于这个的问题,但没有一个对我有用,谁能帮我解决这个问题?
我正在使用护照通过discord oauth2 对我的用户进行身份验证。我希望它们被重定向回原来的页面,而不是主页或仪表板。
我尝试将 URL 存储在会话中,如此处所述,但它不会保留到下一个请求。
我的页面需要身份验证的中间件:
module.exports = (req, res, next) => {
if (req.user) {
next();
}
else {
req.session.returnTo = req.originalUrl;
res.redirect('/auth');
}
};
Run Code Online (Sandbox Code Playgroud)
认证路线:
router.get("/auth", passport.authenticate("discord"));
router.get("/auth/redirect", passport.authenticate("discord", {
failureRedirect: "/auth/forbidden"
}), (req, res) => {
console.log(req.session); // doesnt have returnTo inside anymore ?
res.redirect(req.session.returnTo || '/');
delete req.session.returnTo;
});
Run Code Online (Sandbox Code Playgroud)
console.log 显示用户已成功通过身份验证,但 returnTo 字段不再存在。