部署到Heroku后,管理员UI登录突然停止工作



我最近使用此处的指南初始化了一个Keystone应用程序。我在代码中唯一接触到的另一件事是在前端添加一个Next.js应用程序,并更改mongodb连接字符串以指向我的mongodb Atlas集群。我在Heroku上部署了这个应用程序,它连接到数据库,一切都很好,但当尝试登录时,单击"登录"后,按钮会显示加载指示器一秒钟,然后再次显示登录页面。

我已经通读了文档,并尝试将

config: {
identityField: 'email',
secretField: 'password'
}

在我的authStrategy定义中,但没有用。真的不知道从这里还能去哪里。

这是我的Keystone定义:

const keystone = new Keystone({
name: PROJECT_NAME,
adapter: new Adapter({
mongoUri: 'mongodb+srv://stratus:WjbsENMyBV80AJ6H@cluster0-qe4bs.azure.mongodb.net/stratus?retryWrites=true&w=majority',
useNewUrlParser: true
}),
});
// Access control functions
const userIsAdmin = ({ authentication: { item: user } }) => Boolean(user && user.isAdmin);
const userOwnsItem = ({ authentication: { item: user } }) => {
if (!user) {
return false;
}
return { id: user.id };
};
const userIsAdminOrOwner = auth => {
const isAdmin = access.userIsAdmin(auth);
const isOwner = access.userOwnsItem(auth);
return isAdmin ? isAdmin : isOwner;
};
const access = { userIsAdmin, userOwnsItem, userIsAdminOrOwner };
keystone.createList('User', {
fields: {
name: { type: Text },
email: {
type: Text,
isUnique: true,
},
isAdmin: { type: Checkbox },
password: {
type: Password,
},
},
// To create an initial user you can temporarily remove access controls
access: {
read: access.userIsAdminOrOwner,
update: access.userIsAdminOrOwner,
create: access.userIsAdmin,
delete: access.userIsAdmin,
auth: true,
},
});
const authStrategy = keystone.createAuthStrategy({
type: PasswordAuthStrategy,
list: 'User',
config: {
identityField: 'email',
secretField: 'password'
}
});
module.exports = {
keystone,
apps: [
new GraphQLApp(),
// To create an initial user you can temporarily remove the authStrategy below
new AdminUIApp({ enableDefaultRoute: true, authStrategy }),
new NextApp({ dir: 'client'})
],
};

我在我的Heroku日志中没有看到任何错误。非常感谢您的帮助!

原来它是内存中的会话存储,根据文档,它不会扩展到单个进程。在生产中,这需要在Keystone构造函数中通过传递文档中所示的"session store": <Session Store>的键值对来设置

正如Daniel所指出的,内存中的会话存储不可扩展。为了解决这个问题,我不得不将我的mongoStore连接到会话存储。

这是Keystone v5

const session = require("express-session");
const MongoStore = require("connect-mongo")(session);
const keystone = new Keystone({
name: PROJECT_NAME,
adapter: new Adapter({
mongoUri: process.env.MONGO_URL
}),
onConnect: initialiseData,
cookieSecret: process.env.SESSION_KEY,
secureCookies: false,  <-- needed on non-SSL servers
sessionStore: new MongoStore({
url: process.env.MONGO_URL
})
});

连接mongohttps://www.npmjs.com/package/connect-mongo

快速会话https://www.npmjs.com/package/express-session

在我部署时(2021年3月(,在实施Kenny的建议后,需要通过设置每个Keystone doc 的cookie.secure属性将安全cookie设置为false

const keystone = new Keystone({
/* ...config */
cookie: {
secure: false // if SSL not enabled!
maxAge: 1000 * 60 * 60 * 24 * 30, 
sameSite: false,
},
});

最新更新