widget chat 26
This commit is contained in:
parent
b9a6589e14
commit
2dd300f7fc
@ -178,6 +178,97 @@ export const authOptions: NextAuthOptions = {
|
|||||||
return token;
|
return token;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If we have a username but no Rocket.Chat credentials, fetch them
|
||||||
|
if (token.username && (!token.rocketChatToken || !token.rocketChatUserId)) {
|
||||||
|
console.log('Fetching Rocket.Chat credentials for existing user:', token.username);
|
||||||
|
|
||||||
|
// 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: {
|
||||||
|
'X-Auth-Token': process.env.ROCKET_CHAT_ADMIN_TOKEN as string,
|
||||||
|
'X-User-Id': process.env.ROCKET_CHAT_ADMIN_USER_ID as string,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!userInfoResponse.ok) {
|
||||||
|
console.error('Failed to get user info:', {
|
||||||
|
status: userInfoResponse.status,
|
||||||
|
statusText: userInfoResponse.statusText,
|
||||||
|
username: token.username
|
||||||
|
});
|
||||||
|
return token;
|
||||||
|
}
|
||||||
|
|
||||||
|
const userInfo = await userInfoResponse.json();
|
||||||
|
console.log('User info response:', userInfo);
|
||||||
|
|
||||||
|
if (!userInfo.user || !userInfo.user._id) {
|
||||||
|
console.error('Invalid user info response:', userInfo);
|
||||||
|
return token;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set the user's Rocket.Chat ID
|
||||||
|
token.rocketChatUserId = userInfo.user._id;
|
||||||
|
console.log('Set Rocket.Chat user ID:', token.rocketChatUserId);
|
||||||
|
|
||||||
|
// Get personal access tokens for the user
|
||||||
|
const tokensResponse = await fetch(`${process.env.ROCKET_CHAT_URL}/api/v1/users.getPersonalAccessTokens`, {
|
||||||
|
headers: {
|
||||||
|
'X-Auth-Token': process.env.ROCKET_CHAT_ADMIN_TOKEN as string,
|
||||||
|
'X-User-Id': process.env.ROCKET_CHAT_ADMIN_USER_ID as string,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!tokensResponse.ok) {
|
||||||
|
console.error('Failed to get personal access tokens:', {
|
||||||
|
status: tokensResponse.status,
|
||||||
|
statusText: tokensResponse.statusText,
|
||||||
|
username: token.username
|
||||||
|
});
|
||||||
|
return token;
|
||||||
|
}
|
||||||
|
|
||||||
|
const tokensData = await tokensResponse.json();
|
||||||
|
console.log('Personal access tokens response:', tokensData);
|
||||||
|
|
||||||
|
// Find existing token or create a new one
|
||||||
|
let personalAccessToken = tokensData.tokens?.find((t: any) => t.name === 'NextAuth');
|
||||||
|
if (!personalAccessToken) {
|
||||||
|
console.log('No existing token found, creating new one');
|
||||||
|
const createTokenResponse = await fetch(`${process.env.ROCKET_CHAT_URL}/api/v1/users.generatePersonalAccessToken`, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
'X-Auth-Token': process.env.ROCKET_CHAT_ADMIN_TOKEN as string,
|
||||||
|
'X-User-Id': process.env.ROCKET_CHAT_ADMIN_USER_ID as string,
|
||||||
|
},
|
||||||
|
body: JSON.stringify({
|
||||||
|
tokenName: 'NextAuth',
|
||||||
|
bypassTwoFactor: true,
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!createTokenResponse.ok) {
|
||||||
|
console.error('Failed to create personal access token:', {
|
||||||
|
status: createTokenResponse.status,
|
||||||
|
statusText: createTokenResponse.statusText,
|
||||||
|
username: token.username
|
||||||
|
});
|
||||||
|
return token;
|
||||||
|
}
|
||||||
|
|
||||||
|
const createTokenData = await createTokenResponse.json();
|
||||||
|
console.log('Create token response:', createTokenData);
|
||||||
|
personalAccessToken = createTokenData.token;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set the personal access token
|
||||||
|
token.rocketChatToken = personalAccessToken.token;
|
||||||
|
console.log('Set Rocket.Chat token:', token.rocketChatToken);
|
||||||
|
|
||||||
|
return token;
|
||||||
|
}
|
||||||
|
|
||||||
// Return previous token if it has Rocket.Chat credentials
|
// Return previous token if it has Rocket.Chat credentials
|
||||||
if (token.rocketChatToken && token.rocketChatUserId) {
|
if (token.rocketChatToken && token.rocketChatUserId) {
|
||||||
return token;
|
return token;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user