widget chat 4
This commit is contained in:
parent
c6b677bbdc
commit
2df85e77a7
@ -1,6 +1,14 @@
|
|||||||
import NextAuth, { NextAuthOptions } from "next-auth";
|
import NextAuth, { NextAuthOptions } from "next-auth";
|
||||||
import KeycloakProvider from "next-auth/providers/keycloak";
|
import KeycloakProvider from "next-auth/providers/keycloak";
|
||||||
|
|
||||||
|
interface RocketChatLoginResponse {
|
||||||
|
status: string;
|
||||||
|
data: {
|
||||||
|
authToken: string;
|
||||||
|
userId: string;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
declare module "next-auth" {
|
declare module "next-auth" {
|
||||||
interface Session {
|
interface Session {
|
||||||
user: {
|
user: {
|
||||||
@ -14,6 +22,8 @@ declare module "next-auth" {
|
|||||||
role: string[];
|
role: string[];
|
||||||
};
|
};
|
||||||
accessToken: string;
|
accessToken: string;
|
||||||
|
rocketChatToken: string;
|
||||||
|
rocketChatUserId: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface JWT {
|
interface JWT {
|
||||||
@ -24,6 +34,22 @@ declare module "next-auth" {
|
|||||||
username: string;
|
username: string;
|
||||||
first_name: string;
|
first_name: string;
|
||||||
last_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.username = (profile as any).preferred_username ?? profile.email?.split('@')[0] ?? '';
|
||||||
token.first_name = (profile as any).given_name ?? '';
|
token.first_name = (profile as any).given_name ?? '';
|
||||||
token.last_name = (profile as any).family_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;
|
return token;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -115,6 +166,8 @@ export const authOptions: NextAuthOptions = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
session.accessToken = token.accessToken;
|
session.accessToken = token.accessToken;
|
||||||
|
session.rocketChatToken = token.rocketChatToken;
|
||||||
|
session.rocketChatUserId = token.rocketChatUserId;
|
||||||
session.user = {
|
session.user = {
|
||||||
...session.user,
|
...session.user,
|
||||||
id: token.sub as string,
|
id: token.sub as string,
|
||||||
|
|||||||
@ -9,13 +9,13 @@ export async function GET() {
|
|||||||
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||||
}
|
}
|
||||||
|
|
||||||
// Use the Rocket.Chat token from environment variables
|
// Get the user's Rocket.Chat token from their session
|
||||||
const rocketChatToken = process.env.ROCKET_CHAT_TOKEN;
|
const rocketChatToken = session.rocketChatToken;
|
||||||
const rocketChatUserId = process.env.ROCKET_CHAT_USER_ID;
|
const rocketChatUserId = session.rocketChatUserId;
|
||||||
|
|
||||||
if (!rocketChatToken || !rocketChatUserId) {
|
if (!rocketChatToken || !rocketChatUserId) {
|
||||||
console.error('Missing Rocket.Chat credentials in environment variables');
|
console.error('Missing Rocket.Chat credentials in user session');
|
||||||
return NextResponse.json({ error: "Server configuration error" }, { status: 500 });
|
return NextResponse.json({ error: "User not authenticated with Rocket.Chat" }, { status: 401 });
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the user's subscriptions (rooms they are in)
|
// 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;
|
last_name: string;
|
||||||
username: string;
|
username: string;
|
||||||
email: string;
|
email: string;
|
||||||
role?: string[];
|
role: string[];
|
||||||
} & DefaultSession["user"];
|
} & DefaultSession["user"];
|
||||||
accessToken: string;
|
accessToken: string;
|
||||||
refreshToken: string;
|
refreshToken: string;
|
||||||
|
rocketChatToken: string;
|
||||||
|
rocketChatUserId: string;
|
||||||
error?: string;
|
error?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface JWT {
|
interface JWT {
|
||||||
accessToken?: string;
|
accessToken: string;
|
||||||
first_name?: string;
|
refreshToken: string;
|
||||||
last_name?: string;
|
accessTokenExpires: number;
|
||||||
username?: string;
|
first_name: string;
|
||||||
role?: string[] | string | null;
|
last_name: string;
|
||||||
|
username: string;
|
||||||
|
role: string[];
|
||||||
|
rocketChatToken: string;
|
||||||
|
rocketChatUserId: string;
|
||||||
error?: string;
|
error?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -30,7 +36,7 @@ declare module "next-auth" {
|
|||||||
last_name: string;
|
last_name: string;
|
||||||
username: string;
|
username: string;
|
||||||
email: string;
|
email: string;
|
||||||
role?: string[] | string | null;
|
role: string[];
|
||||||
}
|
}
|
||||||
|
|
||||||
interface Profile {
|
interface Profile {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user