我在Next.js应用程序中使用Next-Auth进行身份验证,我使用凭据提供程序。在登录页面上,用户可以选择记住他们的密码。
选择此选项时,我想增加会话过期时间。如果未选择,则过期时间应设置为24小时。
Login.js代码:
const handleSubmit = async (e) => {
e.preventDefault();
const res = await signIn("credentials", {
redirect: false,
username: username,
password: password,
rememberPassword: checked,
});
if (res?.error) {
console.log(res.error)
} else {
push("/");
}
};
[...nextauth].js code:
export const authOptions = {
providers: [
CredentialsProvider({
name: "credentials",
async authorize(credentials) {
...
//I can access rememberPassword here, for example :
const rememberPassword = credentials.rememberPassword;
},
}),
],
secret: SECRET_KEY,
jwt: {
secret: SECRET_KEY,
encryption: true,
},
session: {
jwt: true,
** maxAge: req.body.rememberPassword ? 15 * 24 * 60 * 60 : 24 * 60 * 60, //I want something like this**
},
};
export default NextAuth(authOptions);
访问会话属性中的rememberPassword
时遇到问题。
1条答案
按热度按时间xdnvmnnf1#
您可以使用高级初始化来动态示例化选项。要设置会话过期时间,您可以这样做: