Neah version calendar fix 3 debuger sec chance danger 2

This commit is contained in:
alma 2025-04-17 00:13:38 +02:00
parent f89c25d9a7
commit 7586e17524
5 changed files with 32 additions and 9 deletions

View File

@ -4,3 +4,16 @@ import { authOptions } from "@/lib/auth";
const handler = NextAuth(authOptions); const handler = NextAuth(authOptions);
export { handler as GET, handler as POST }; export { handler as GET, handler as POST };
interface JWT {
accessToken: string;
refreshToken: string;
accessTokenExpires: number;
}
interface Profile {
sub?: string;
email?: string;
name?: string;
roles?: string[];
}

View File

@ -1,6 +1,6 @@
import { NextRequest, NextResponse } from "next/server"; import { NextRequest, NextResponse } from "next/server";
import { authOptions } from "@/lib/auth";
import { getServerSession } from "next-auth/next"; import { getServerSession } from "next-auth/next";
import { authOptions } from "@/app/api/auth/[...nextauth]/route";
import { prisma } from "@/lib/prisma"; import { prisma } from "@/lib/prisma";
/** /**

View File

@ -1,6 +1,6 @@
import { NextRequest, NextResponse } from "next/server"; import { NextRequest, NextResponse } from "next/server";
import { getServerSession } from "next-auth/next"; import { getServerSession } from "next-auth/next";
import { authOptions } from "@/app/api/auth/[...nextauth]/route"; import { authOptions } from "@/lib/auth";
import { prisma } from "@/lib/prisma"; import { prisma } from "@/lib/prisma";
// Cache for Leantime user IDs // Cache for Leantime user IDs

View File

@ -1,5 +1,5 @@
import { getServerSession } from "next-auth/next"; import { getServerSession } from "next-auth/next";
import { authOptions } from "@/app/api/auth/[...nextauth]/route"; import { authOptions } from "@/lib/auth";
import { NextResponse } from "next/server"; import { NextResponse } from "next/server";
// Helper function to get user token using admin credentials // Helper function to get user token using admin credentials

View File

@ -18,7 +18,7 @@ declare module 'next-auth' {
} }
interface Profile { interface Profile {
sub: string; sub: string;
email?: string; email: string;
name?: string; name?: string;
roles?: string[]; roles?: string[];
} }
@ -30,9 +30,9 @@ declare module 'next-auth/jwt' {
email: string; email: string;
name?: string; name?: string;
role: string[]; role: string[];
accessToken?: string; accessToken: string;
refreshToken?: string; refreshToken: string;
accessTokenExpires?: number; accessTokenExpires: number;
error?: string; error?: string;
} }
} }
@ -56,21 +56,31 @@ export const authOptions: NextAuthOptions = {
callbacks: { callbacks: {
async jwt({ token, account, profile }) { async jwt({ token, account, profile }) {
if (account && profile) { if (account && profile) {
if (!profile.sub) {
throw new Error('No user ID (sub) provided by Keycloak');
}
if (!account.access_token || !account.refresh_token || !account.expires_at) {
throw new Error('Missing required token fields from Keycloak');
}
token.id = profile.sub; token.id = profile.sub;
token.email = profile.email || ''; token.email = profile.email || '';
token.name = profile.name; token.name = profile.name;
token.role = profile.roles || ['user']; token.role = profile.roles || ['user'];
token.accessToken = account.access_token; token.accessToken = account.access_token;
token.refreshToken = account.refresh_token; token.refreshToken = account.refresh_token;
token.accessTokenExpires = account.expires_at! * 1000; token.accessTokenExpires = account.expires_at * 1000;
} }
// Return previous token if not expired // Return previous token if not expired
if (Date.now() < (token.accessTokenExpires as number)) { if (token.accessTokenExpires && Date.now() < token.accessTokenExpires) {
return token; return token;
} }
// Token expired, try to refresh // Token expired, try to refresh
if (!token.refreshToken) {
throw new Error('No refresh token available');
}
try { try {
const response = await fetch( const response = await fetch(
`${process.env.KEYCLOAK_BASE_URL}/realms/${process.env.KEYCLOAK_REALM}/protocol/openid-connect/token`, `${process.env.KEYCLOAK_BASE_URL}/realms/${process.env.KEYCLOAK_REALM}/protocol/openid-connect/token`,