diff --git a/app/api/auth/[...nextauth]/route.ts b/app/api/auth/[...nextauth]/route.ts index d6a13b4e..9eb3738e 100644 --- a/app/api/auth/[...nextauth]/route.ts +++ b/app/api/auth/[...nextauth]/route.ts @@ -84,7 +84,7 @@ export const authOptions: NextAuthOptions = { console.log('JWT callback called with:', { token, account, profile }); // Initial sign in - if (account) { + if (account && profile) { // Get user info from Rocket.Chat using the admin token const userInfoResponse = await fetch(`${process.env.ROCKET_CHAT_URL}/api/v1/users.info?username=${token.username}`, { headers: { @@ -172,7 +172,12 @@ export const authOptions: NextAuthOptions = { return token; } - // Return previous token if not expired + // Return previous token if it has Rocket.Chat credentials + if (token.rocketChatToken && token.rocketChatUserId) { + return token; + } + + // Token refresh case if (Date.now() < (token.accessTokenExpires as number)) { return token; }