admin管理员组文章数量:1023838
I'm trying to update a user session via NextAuth.js v5 using JWT
strategy.
My end goal is switch to another active company in server side. As I use their unstable_update
function and it actually triggers my JWT callback.
async jwt({ token, user, trigger, session }) {
if (user) {
const userId = token.sub as string;
const user = await prisma.user.findUnique({
where: { id: +userId },
});
token.role = user?.role as Role;
token.username = user?.username as string;
token.firstName = user?.firstName as string;
token.lastName = user?.lastName as string;
token.activeCompanyId = user?.activeCompanyId;
}
if (trigger === 'update') {
console.log('Update triggered, session:');
console.log(session);
token.activeCompanyId = session.user.activeCompanyId;
}
return token;
},
async session({ token, session }: { token: JWT; session: Session }) {
console.log('Token in session callback');
console.log(token);
const userId = token.sub as string;
session.user.userId = +userId;
session.user.role = token.role as Role;
session.user.firstName = token.firstName;
session.user.lastName = token.lastName;
session.user.username = token.username;
session.user.activeCompanyId = token.activeCompanyId;
return session;
},
}
After it was triggered my session was updated, but with the other call of session it was nullified, and the final result in session.user.activeCompanyId
is null
.
Am I doing it correctly?
I'm trying to update a user session via NextAuth.js v5 using JWT
strategy.
My end goal is switch to another active company in server side. As I use their unstable_update
function and it actually triggers my JWT callback.
async jwt({ token, user, trigger, session }) {
if (user) {
const userId = token.sub as string;
const user = await prisma.user.findUnique({
where: { id: +userId },
});
token.role = user?.role as Role;
token.username = user?.username as string;
token.firstName = user?.firstName as string;
token.lastName = user?.lastName as string;
token.activeCompanyId = user?.activeCompanyId;
}
if (trigger === 'update') {
console.log('Update triggered, session:');
console.log(session);
token.activeCompanyId = session.user.activeCompanyId;
}
return token;
},
async session({ token, session }: { token: JWT; session: Session }) {
console.log('Token in session callback');
console.log(token);
const userId = token.sub as string;
session.user.userId = +userId;
session.user.role = token.role as Role;
session.user.firstName = token.firstName;
session.user.lastName = token.lastName;
session.user.username = token.username;
session.user.activeCompanyId = token.activeCompanyId;
return session;
},
}
After it was triggered my session was updated, but with the other call of session it was nullified, and the final result in session.user.activeCompanyId
is null
.
Am I doing it correctly?
1 Answer
Reset to default 0As the documentation says: The arguments user, account, profile and isNewUser are only passed the first time this callback is called on a new session, after the user signs in. In subsequent calls, only token will be available.
So, you should only assign token.activeCompanyId
if session != null
.
I'ld expect that the session
argument received in the session
callback still has the user.activeCompanyId
value, so I don't think you need to parse it from token
at all.
I'm trying to update a user session via NextAuth.js v5 using JWT
strategy.
My end goal is switch to another active company in server side. As I use their unstable_update
function and it actually triggers my JWT callback.
async jwt({ token, user, trigger, session }) {
if (user) {
const userId = token.sub as string;
const user = await prisma.user.findUnique({
where: { id: +userId },
});
token.role = user?.role as Role;
token.username = user?.username as string;
token.firstName = user?.firstName as string;
token.lastName = user?.lastName as string;
token.activeCompanyId = user?.activeCompanyId;
}
if (trigger === 'update') {
console.log('Update triggered, session:');
console.log(session);
token.activeCompanyId = session.user.activeCompanyId;
}
return token;
},
async session({ token, session }: { token: JWT; session: Session }) {
console.log('Token in session callback');
console.log(token);
const userId = token.sub as string;
session.user.userId = +userId;
session.user.role = token.role as Role;
session.user.firstName = token.firstName;
session.user.lastName = token.lastName;
session.user.username = token.username;
session.user.activeCompanyId = token.activeCompanyId;
return session;
},
}
After it was triggered my session was updated, but with the other call of session it was nullified, and the final result in session.user.activeCompanyId
is null
.
Am I doing it correctly?
I'm trying to update a user session via NextAuth.js v5 using JWT
strategy.
My end goal is switch to another active company in server side. As I use their unstable_update
function and it actually triggers my JWT callback.
async jwt({ token, user, trigger, session }) {
if (user) {
const userId = token.sub as string;
const user = await prisma.user.findUnique({
where: { id: +userId },
});
token.role = user?.role as Role;
token.username = user?.username as string;
token.firstName = user?.firstName as string;
token.lastName = user?.lastName as string;
token.activeCompanyId = user?.activeCompanyId;
}
if (trigger === 'update') {
console.log('Update triggered, session:');
console.log(session);
token.activeCompanyId = session.user.activeCompanyId;
}
return token;
},
async session({ token, session }: { token: JWT; session: Session }) {
console.log('Token in session callback');
console.log(token);
const userId = token.sub as string;
session.user.userId = +userId;
session.user.role = token.role as Role;
session.user.firstName = token.firstName;
session.user.lastName = token.lastName;
session.user.username = token.username;
session.user.activeCompanyId = token.activeCompanyId;
return session;
},
}
After it was triggered my session was updated, but with the other call of session it was nullified, and the final result in session.user.activeCompanyId
is null
.
Am I doing it correctly?
1 Answer
Reset to default 0As the documentation says: The arguments user, account, profile and isNewUser are only passed the first time this callback is called on a new session, after the user signs in. In subsequent calls, only token will be available.
So, you should only assign token.activeCompanyId
if session != null
.
I'ld expect that the session
argument received in the session
callback still has the user.activeCompanyId
value, so I don't think you need to parse it from token
at all.
本文标签: javascriptNEXT Auth v5 Server side session updateStack Overflow
版权声明:本文标题:javascript - NEXT Auth v5 Server side session update - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745555397a2155843.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论