clean sidebar
This commit is contained in:
parent
4589aaab2f
commit
b9b3db54d8
@ -15,16 +15,22 @@ declare module "next-auth" {
|
|||||||
role: string[];
|
role: string[];
|
||||||
};
|
};
|
||||||
accessToken: string;
|
accessToken: string;
|
||||||
|
refreshToken?: string;
|
||||||
|
rocketChatToken?: string | null;
|
||||||
|
rocketChatUserId?: string | null;
|
||||||
|
error?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface JWT {
|
interface JWT {
|
||||||
accessToken: string;
|
accessToken?: string;
|
||||||
refreshToken: string;
|
refreshToken?: string;
|
||||||
accessTokenExpires: number;
|
accessTokenExpires?: number;
|
||||||
role: string[];
|
role?: string[];
|
||||||
username: string;
|
username?: string;
|
||||||
first_name: string;
|
first_name?: string;
|
||||||
last_name: string;
|
last_name?: string;
|
||||||
|
name?: string;
|
||||||
|
email?: string;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -138,12 +144,36 @@ export const authOptions: NextAuthOptions = {
|
|||||||
async session({ session, token }) {
|
async session({ session, token }) {
|
||||||
if (session?.user && token.sub) {
|
if (session?.user && token.sub) {
|
||||||
session.user.id = 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;
|
return session;
|
||||||
},
|
},
|
||||||
async jwt({ token, user, account }) {
|
async jwt({ token, user, account }) {
|
||||||
if (user) {
|
if (user) {
|
||||||
token.sub = user.id;
|
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;
|
return token;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -93,13 +93,30 @@ export async function GET() {
|
|||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
console.error(`News API error: ${response.status} ${response.statusText}`);
|
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(
|
return NextResponse.json(
|
||||||
{ error: 'Failed to fetch news', status: response.status },
|
{ error: 'Failed to fetch news', status: response.status },
|
||||||
{ status: 502 }
|
{ 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) => ({
|
const formattedNews: NewsItem[] = articles.map((article: any) => ({
|
||||||
id: article.id,
|
id: article.id,
|
||||||
|
|||||||
@ -48,6 +48,13 @@ export function Sidebar({ isOpen, onClose }: SidebarProps) {
|
|||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
const pathname = usePathname();
|
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
|
// Function to check if user has a specific role
|
||||||
const hasRole = (requiredRole: string | string[] | undefined) => {
|
const hasRole = (requiredRole: string | string[] | undefined) => {
|
||||||
if (!requiredRole || !session?.user?.role) {
|
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];
|
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
|
// Clean up user roles by removing prefixes and converting to lowercase for comparison
|
||||||
const cleanUserRoles = userRoles.map(role =>
|
const cleanUserRoles = userRoles.map(role =>
|
||||||
role.replace(/^[\/]/, '').toLowerCase()
|
role.replace(/^[\/]/, '').toLowerCase()
|
||||||
);
|
);
|
||||||
|
|
||||||
console.log('Clean user roles:', cleanUserRoles);
|
console.log('Clean user roles:', cleanUserRoles);
|
||||||
|
|
||||||
// If requiredRole is an array, check if user has any of the roles
|
// If requiredRole is an array, check if user has any of the roles
|
||||||
if (Array.isArray(requiredRole)) {
|
if (Array.isArray(requiredRole)) {
|
||||||
const cleanRequiredRoles = requiredRole.map(role => role.toLowerCase());
|
const cleanRequiredRoles = requiredRole.map(role => role.toLowerCase());
|
||||||
console.log('Required roles (array):', cleanRequiredRoles);
|
console.log('Checking multiple roles:', {
|
||||||
return cleanRequiredRoles.some(role =>
|
requiredRoles: requiredRole,
|
||||||
cleanUserRoles.includes(role.toLowerCase()) || cleanUserRoles.includes('admin')
|
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
|
// For single role requirement
|
||||||
console.log('Required role (single):', requiredRole.toLowerCase());
|
const cleanRequiredRole = requiredRole.toLowerCase();
|
||||||
return cleanUserRoles.includes(requiredRole.toLowerCase()) || cleanUserRoles.includes('admin');
|
console.log('Checking single role:', {
|
||||||
|
requiredRole,
|
||||||
|
cleanRequiredRole,
|
||||||
|
hasRole: cleanUserRoles.includes(cleanRequiredRole)
|
||||||
|
});
|
||||||
|
return cleanUserRoles.includes(cleanRequiredRole);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Base menu items (available for everyone)
|
// Base menu items (available for everyone)
|
||||||
|
|||||||
44
types/next-auth.d.ts
vendored
44
types/next-auth.d.ts
vendored
@ -10,23 +10,23 @@ declare module "next-auth" {
|
|||||||
email: string;
|
email: string;
|
||||||
role: string[];
|
role: string[];
|
||||||
} & DefaultSession["user"];
|
} & DefaultSession["user"];
|
||||||
accessToken: string;
|
accessToken?: string;
|
||||||
refreshToken: string;
|
refreshToken?: string;
|
||||||
rocketChatToken: string | null;
|
rocketChatToken?: string | null;
|
||||||
rocketChatUserId: string | null;
|
rocketChatUserId?: string | null;
|
||||||
error?: string;
|
error?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface JWT {
|
interface JWT {
|
||||||
accessToken: string;
|
accessToken?: string;
|
||||||
refreshToken: string;
|
refreshToken?: string;
|
||||||
accessTokenExpires: number;
|
accessTokenExpires?: number;
|
||||||
first_name: string;
|
first_name?: string;
|
||||||
last_name: string;
|
last_name?: string;
|
||||||
username: string;
|
username?: string;
|
||||||
role: string[];
|
role?: string[];
|
||||||
rocketChatToken: string | null;
|
rocketChatToken?: string | null;
|
||||||
rocketChatUserId: string | null;
|
rocketChatUserId?: string | null;
|
||||||
error?: string;
|
error?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -53,15 +53,15 @@ declare module "next-auth" {
|
|||||||
|
|
||||||
declare module "next-auth/jwt" {
|
declare module "next-auth/jwt" {
|
||||||
interface JWT {
|
interface JWT {
|
||||||
accessToken: string;
|
accessToken?: string;
|
||||||
refreshToken: string;
|
refreshToken?: string;
|
||||||
accessTokenExpires: number;
|
accessTokenExpires?: number;
|
||||||
first_name: string;
|
first_name?: string;
|
||||||
last_name: string;
|
last_name?: string;
|
||||||
username: string;
|
username?: string;
|
||||||
role: string[];
|
role?: string[];
|
||||||
rocketChatToken: string;
|
rocketChatToken?: string;
|
||||||
rocketChatUserId: string;
|
rocketChatUserId?: string;
|
||||||
error?: string;
|
error?: string;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user