clean sidebar

This commit is contained in:
alma 2025-04-18 10:02:58 +02:00
parent 4589aaab2f
commit b9b3db54d8
4 changed files with 99 additions and 37 deletions

View File

@ -15,16 +15,22 @@ declare module "next-auth" {
role: string[];
};
accessToken: string;
refreshToken?: string;
rocketChatToken?: string | null;
rocketChatUserId?: string | null;
error?: string;
}
interface JWT {
accessToken: string;
refreshToken: string;
accessTokenExpires: number;
role: string[];
username: string;
first_name: string;
last_name: string;
accessToken?: string;
refreshToken?: string;
accessTokenExpires?: number;
role?: string[];
username?: string;
first_name?: string;
last_name?: string;
name?: string;
email?: string;
}
}
@ -138,12 +144,36 @@ export const authOptions: NextAuthOptions = {
async session({ session, token }) {
if (session?.user && token.sub) {
session.user.id = token.sub;
session.user.name = token.name ?? null;
session.user.email = token.email ?? null;
session.user.username = token.username ?? '';
session.user.first_name = token.first_name ?? '';
session.user.last_name = token.last_name ?? '';
session.user.role = token.role ?? [];
session.accessToken = token.accessToken ?? '';
session.refreshToken = token.refreshToken ?? '';
session.rocketChatToken = token.rocketChatToken ?? null;
session.rocketChatUserId = token.rocketChatUserId ?? null;
if (token.error) {
session.error = token.error;
}
}
return session;
},
async jwt({ token, user, account }) {
if (user) {
token.sub = user.id;
token.name = user.name;
token.email = user.email;
token.username = user.username;
token.first_name = user.first_name;
token.last_name = user.last_name;
token.role = user.role;
}
if (account) {
token.accessToken = account.access_token;
token.refreshToken = account.refresh_token;
token.accessTokenExpires = account.expires_at ? account.expires_at * 1000 : 0;
}
return token;
}

View File

@ -93,13 +93,30 @@ export async function GET() {
if (!response.ok) {
console.error(`News API error: ${response.status} ${response.statusText}`);
const contentType = response.headers.get('content-type');
if (contentType && !contentType.includes('application/json')) {
console.error('News API returned non-JSON response');
return NextResponse.json(
{ error: 'News API returned invalid response format', status: response.status },
{ status: 502 }
);
}
return NextResponse.json(
{ error: 'Failed to fetch news', status: response.status },
{ status: 502 }
);
}
const articles = await response.json();
let articles;
try {
articles = await response.json();
} catch (error) {
console.error('Failed to parse news API response:', error);
return NextResponse.json(
{ error: 'Failed to parse news API response', details: error instanceof Error ? error.message : 'Unknown error' },
{ status: 502 }
);
}
const formattedNews: NewsItem[] = articles.map((article: any) => ({
id: article.id,

View File

@ -48,6 +48,13 @@ export function Sidebar({ isOpen, onClose }: SidebarProps) {
const router = useRouter();
const pathname = usePathname();
// Debug session data
console.log('Session data:', {
hasSession: !!session,
user: session?.user,
roles: session?.user?.role
});
// Function to check if user has a specific role
const hasRole = (requiredRole: string | string[] | undefined) => {
if (!requiredRole || !session?.user?.role) {
@ -56,26 +63,34 @@ export function Sidebar({ isOpen, onClose }: SidebarProps) {
}
const userRoles = Array.isArray(session.user.role) ? session.user.role : [session.user.role];
console.log('Raw user roles:', userRoles);
// Clean up user roles by removing prefixes and converting to lowercase for comparison
const cleanUserRoles = userRoles.map(role =>
role.replace(/^[\/]/, '').toLowerCase()
);
console.log('Clean user roles:', cleanUserRoles);
// If requiredRole is an array, check if user has any of the roles
if (Array.isArray(requiredRole)) {
const cleanRequiredRoles = requiredRole.map(role => role.toLowerCase());
console.log('Required roles (array):', cleanRequiredRoles);
return cleanRequiredRoles.some(role =>
cleanUserRoles.includes(role.toLowerCase()) || cleanUserRoles.includes('admin')
);
console.log('Checking multiple roles:', {
requiredRoles: requiredRole,
cleanRequiredRoles,
hasAnyRole: cleanRequiredRoles.some(role => cleanUserRoles.includes(role)),
matchingRoles: cleanRequiredRoles.filter(role => cleanUserRoles.includes(role))
});
return cleanRequiredRoles.some(role => cleanUserRoles.includes(role));
}
// For single role requirement
console.log('Required role (single):', requiredRole.toLowerCase());
return cleanUserRoles.includes(requiredRole.toLowerCase()) || cleanUserRoles.includes('admin');
const cleanRequiredRole = requiredRole.toLowerCase();
console.log('Checking single role:', {
requiredRole,
cleanRequiredRole,
hasRole: cleanUserRoles.includes(cleanRequiredRole)
});
return cleanUserRoles.includes(cleanRequiredRole);
};
// Base menu items (available for everyone)

44
types/next-auth.d.ts vendored
View File

@ -10,23 +10,23 @@ declare module "next-auth" {
email: string;
role: string[];
} & DefaultSession["user"];
accessToken: string;
refreshToken: string;
rocketChatToken: string | null;
rocketChatUserId: string | null;
accessToken?: string;
refreshToken?: string;
rocketChatToken?: string | null;
rocketChatUserId?: string | null;
error?: string;
}
interface JWT {
accessToken: string;
refreshToken: string;
accessTokenExpires: number;
first_name: string;
last_name: string;
username: string;
role: string[];
rocketChatToken: string | null;
rocketChatUserId: string | null;
accessToken?: string;
refreshToken?: string;
accessTokenExpires?: number;
first_name?: string;
last_name?: string;
username?: string;
role?: string[];
rocketChatToken?: string | null;
rocketChatUserId?: string | null;
error?: string;
}
@ -53,15 +53,15 @@ declare module "next-auth" {
declare module "next-auth/jwt" {
interface JWT {
accessToken: string;
refreshToken: string;
accessTokenExpires: number;
first_name: string;
last_name: string;
username: string;
role: string[];
rocketChatToken: string;
rocketChatUserId: string;
accessToken?: string;
refreshToken?: string;
accessTokenExpires?: number;
first_name?: string;
last_name?: string;
username?: string;
role?: string[];
rocketChatToken?: string;
rocketChatUserId?: string;
error?: string;
}
}