widget chat 4
This commit is contained in:
parent
c6b677bbdc
commit
2df85e77a7
@ -1,6 +1,14 @@
|
||||
import NextAuth, { NextAuthOptions } from "next-auth";
|
||||
import KeycloakProvider from "next-auth/providers/keycloak";
|
||||
|
||||
interface RocketChatLoginResponse {
|
||||
status: string;
|
||||
data: {
|
||||
authToken: string;
|
||||
userId: string;
|
||||
};
|
||||
}
|
||||
|
||||
declare module "next-auth" {
|
||||
interface Session {
|
||||
user: {
|
||||
@ -14,6 +22,8 @@ declare module "next-auth" {
|
||||
role: string[];
|
||||
};
|
||||
accessToken: string;
|
||||
rocketChatToken: string;
|
||||
rocketChatUserId: string;
|
||||
}
|
||||
|
||||
interface JWT {
|
||||
@ -24,6 +34,22 @@ declare module "next-auth" {
|
||||
username: string;
|
||||
first_name: string;
|
||||
last_name: string;
|
||||
rocketChatToken: string;
|
||||
rocketChatUserId: string;
|
||||
}
|
||||
}
|
||||
|
||||
declare module "next-auth/jwt" {
|
||||
interface JWT {
|
||||
accessToken: string;
|
||||
refreshToken: string;
|
||||
accessTokenExpires: number;
|
||||
role: string[];
|
||||
username: string;
|
||||
first_name: string;
|
||||
last_name: string;
|
||||
rocketChatToken: string;
|
||||
rocketChatUserId: string;
|
||||
}
|
||||
}
|
||||
|
||||
@ -68,6 +94,31 @@ export const authOptions: NextAuthOptions = {
|
||||
token.username = (profile as any).preferred_username ?? profile.email?.split('@')[0] ?? '';
|
||||
token.first_name = (profile as any).given_name ?? '';
|
||||
token.last_name = (profile as any).family_name ?? '';
|
||||
|
||||
// Get Rocket.Chat token for the user using their Keycloak password
|
||||
try {
|
||||
const rocketChatResponse = await fetch('https://parole.slm-lab.net/api/v1/login', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
user: token.username,
|
||||
password: account.access_token, // Use the Keycloak access token as password
|
||||
}),
|
||||
});
|
||||
|
||||
if (rocketChatResponse.ok) {
|
||||
const rocketChatData = await rocketChatResponse.json() as RocketChatLoginResponse;
|
||||
if (rocketChatData.data) {
|
||||
token.rocketChatToken = rocketChatData.data.authToken;
|
||||
token.rocketChatUserId = rocketChatData.data.userId;
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error getting Rocket.Chat token:', error);
|
||||
}
|
||||
|
||||
return token;
|
||||
}
|
||||
|
||||
@ -115,6 +166,8 @@ export const authOptions: NextAuthOptions = {
|
||||
}
|
||||
|
||||
session.accessToken = token.accessToken;
|
||||
session.rocketChatToken = token.rocketChatToken;
|
||||
session.rocketChatUserId = token.rocketChatUserId;
|
||||
session.user = {
|
||||
...session.user,
|
||||
id: token.sub as string,
|
||||
|
||||
@ -9,13 +9,13 @@ export async function GET() {
|
||||
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
|
||||
// Use the Rocket.Chat token from environment variables
|
||||
const rocketChatToken = process.env.ROCKET_CHAT_TOKEN;
|
||||
const rocketChatUserId = process.env.ROCKET_CHAT_USER_ID;
|
||||
// Get the user's Rocket.Chat token from their session
|
||||
const rocketChatToken = session.rocketChatToken;
|
||||
const rocketChatUserId = session.rocketChatUserId;
|
||||
|
||||
if (!rocketChatToken || !rocketChatUserId) {
|
||||
console.error('Missing Rocket.Chat credentials in environment variables');
|
||||
return NextResponse.json({ error: "Server configuration error" }, { status: 500 });
|
||||
console.error('Missing Rocket.Chat credentials in user session');
|
||||
return NextResponse.json({ error: "User not authenticated with Rocket.Chat" }, { status: 401 });
|
||||
}
|
||||
|
||||
// Get the user's subscriptions (rooms they are in)
|
||||
|
||||
20
types/next-auth.d.ts
vendored
20
types/next-auth.d.ts
vendored
@ -8,19 +8,25 @@ declare module "next-auth" {
|
||||
last_name: string;
|
||||
username: string;
|
||||
email: string;
|
||||
role?: string[];
|
||||
role: string[];
|
||||
} & DefaultSession["user"];
|
||||
accessToken: string;
|
||||
refreshToken: string;
|
||||
rocketChatToken: string;
|
||||
rocketChatUserId: string;
|
||||
error?: string;
|
||||
}
|
||||
|
||||
interface JWT {
|
||||
accessToken?: string;
|
||||
first_name?: string;
|
||||
last_name?: string;
|
||||
username?: string;
|
||||
role?: string[] | string | null;
|
||||
accessToken: string;
|
||||
refreshToken: string;
|
||||
accessTokenExpires: number;
|
||||
first_name: string;
|
||||
last_name: string;
|
||||
username: string;
|
||||
role: string[];
|
||||
rocketChatToken: string;
|
||||
rocketChatUserId: string;
|
||||
error?: string;
|
||||
}
|
||||
|
||||
@ -30,7 +36,7 @@ declare module "next-auth" {
|
||||
last_name: string;
|
||||
username: string;
|
||||
email: string;
|
||||
role?: string[] | string | null;
|
||||
role: string[];
|
||||
}
|
||||
|
||||
interface Profile {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user