widget chat 6
This commit is contained in:
parent
baae184106
commit
20caa97d71
@ -97,25 +97,50 @@ export const authOptions: NextAuthOptions = {
|
|||||||
|
|
||||||
// Get Rocket.Chat token for the user using their Keycloak password
|
// Get Rocket.Chat token for the user using their Keycloak password
|
||||||
try {
|
try {
|
||||||
|
console.log('Attempting to get personal access tokens for user:', token.username);
|
||||||
|
|
||||||
|
// First, let's verify the admin token is working
|
||||||
|
const verifyTokenResponse = await fetch('https://parole.slm-lab.net/api/v1/me', {
|
||||||
|
headers: {
|
||||||
|
'X-Auth-Token': process.env.ROCKET_CHAT_TOKEN!,
|
||||||
|
'X-User-Id': process.env.ROCKET_CHAT_USER_ID!,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log('Admin token verification:', {
|
||||||
|
status: verifyTokenResponse.status,
|
||||||
|
ok: verifyTokenResponse.ok,
|
||||||
|
response: await verifyTokenResponse.text().catch(() => 'Could not get response text')
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!verifyTokenResponse.ok) {
|
||||||
|
console.error('Admin token verification failed');
|
||||||
|
return token;
|
||||||
|
}
|
||||||
|
|
||||||
// Get user's personal access tokens using admin credentials
|
// Get user's personal access tokens using admin credentials
|
||||||
const tokensResponse = await fetch('https://parole.slm-lab.net/api/v1/users.getPersonalAccessTokens', {
|
const tokensResponse = await fetch('https://parole.slm-lab.net/api/v1/users.getPersonalAccessTokens', {
|
||||||
|
method: 'GET',
|
||||||
headers: {
|
headers: {
|
||||||
'X-Auth-Token': process.env.ROCKET_CHAT_TOKEN!,
|
'X-Auth-Token': process.env.ROCKET_CHAT_TOKEN!,
|
||||||
'X-User-Id': process.env.ROCKET_CHAT_USER_ID!,
|
'X-User-Id': process.env.ROCKET_CHAT_USER_ID!,
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
},
|
},
|
||||||
body: JSON.stringify({
|
});
|
||||||
username: token.username,
|
|
||||||
}),
|
console.log('Personal access tokens response:', {
|
||||||
|
status: tokensResponse.status,
|
||||||
|
ok: tokensResponse.ok,
|
||||||
|
response: await tokensResponse.text().catch(() => 'Could not get response text')
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!tokensResponse.ok) {
|
if (!tokensResponse.ok) {
|
||||||
console.error('Failed to get personal access tokens:', await tokensResponse.text());
|
console.error('Failed to get personal access tokens');
|
||||||
return token;
|
return token;
|
||||||
}
|
}
|
||||||
|
|
||||||
const tokensData = await tokensResponse.json();
|
const tokensData = await tokensResponse.json();
|
||||||
console.log('Personal access tokens response:', tokensData);
|
console.log('Parsed tokens data:', tokensData);
|
||||||
|
|
||||||
// Find or create a token for this user
|
// Find or create a token for this user
|
||||||
const tokenName = `keycloak-${token.username}`;
|
const tokenName = `keycloak-${token.username}`;
|
||||||
@ -126,12 +151,14 @@ export const authOptions: NextAuthOptions = {
|
|||||||
// Use existing token
|
// Use existing token
|
||||||
const existingToken = tokensData.tokens.find((t: any) => t.name === tokenName);
|
const existingToken = tokensData.tokens.find((t: any) => t.name === tokenName);
|
||||||
if (existingToken) {
|
if (existingToken) {
|
||||||
|
console.log('Found existing token:', existingToken);
|
||||||
personalToken = existingToken.lastTokenPart;
|
personalToken = existingToken.lastTokenPart;
|
||||||
rocketChatUserId = tokensData.userId;
|
rocketChatUserId = tokensData.userId;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!personalToken) {
|
if (!personalToken) {
|
||||||
|
console.log('Creating new personal access token');
|
||||||
// Create new token
|
// Create new token
|
||||||
const createTokenResponse = await fetch('https://parole.slm-lab.net/api/v1/users.generatePersonalAccessToken', {
|
const createTokenResponse = await fetch('https://parole.slm-lab.net/api/v1/users.generatePersonalAccessToken', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
@ -141,25 +168,34 @@ export const authOptions: NextAuthOptions = {
|
|||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
},
|
},
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
username: token.username,
|
|
||||||
tokenName,
|
tokenName,
|
||||||
bypassTwoFactor: true,
|
bypassTwoFactor: true,
|
||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
console.log('Create token response:', {
|
||||||
|
status: createTokenResponse.status,
|
||||||
|
ok: createTokenResponse.ok,
|
||||||
|
response: await createTokenResponse.text().catch(() => 'Could not get response text')
|
||||||
|
});
|
||||||
|
|
||||||
if (createTokenResponse.ok) {
|
if (createTokenResponse.ok) {
|
||||||
const createTokenData = await createTokenResponse.json();
|
const createTokenData = await createTokenResponse.json();
|
||||||
|
console.log('Created token data:', createTokenData);
|
||||||
personalToken = createTokenData.token;
|
personalToken = createTokenData.token;
|
||||||
rocketChatUserId = createTokenData.userId;
|
rocketChatUserId = createTokenData.userId;
|
||||||
} else {
|
} else {
|
||||||
console.error('Failed to create personal access token:', await createTokenResponse.text());
|
console.error('Failed to create personal access token');
|
||||||
return token;
|
return token;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (personalToken && rocketChatUserId) {
|
if (personalToken && rocketChatUserId) {
|
||||||
|
console.log('Setting Rocket.Chat credentials in token');
|
||||||
token.rocketChatToken = personalToken;
|
token.rocketChatToken = personalToken;
|
||||||
token.rocketChatUserId = rocketChatUserId;
|
token.rocketChatUserId = rocketChatUserId;
|
||||||
|
} else {
|
||||||
|
console.error('Failed to get Rocket.Chat credentials');
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user