working leantime widget 42

This commit is contained in:
Alma 2025-04-12 14:45:55 +02:00
parent 88211acab6
commit 2dc3a9603a
2 changed files with 78 additions and 33 deletions

View File

@ -42,7 +42,7 @@ export const authOptions: NextAuthOptions = {
clientSecret: getRequiredEnvVar("KEYCLOAK_CLIENT_SECRET"), clientSecret: getRequiredEnvVar("KEYCLOAK_CLIENT_SECRET"),
issuer: getRequiredEnvVar("KEYCLOAK_ISSUER"), issuer: getRequiredEnvVar("KEYCLOAK_ISSUER"),
profile(profile) { profile(profile) {
console.log("Keycloak profile:", profile); console.log("Keycloak profile received for user:", profile.preferred_username);
return { return {
id: profile.sub, id: profile.sub,
name: profile.name ?? profile.preferred_username, name: profile.name ?? profile.preferred_username,
@ -57,9 +57,8 @@ export const authOptions: NextAuthOptions = {
], ],
callbacks: { callbacks: {
async jwt({ token, account, profile }) { async jwt({ token, account, profile }) {
console.log("JWT callback - token:", token); // Log only non-sensitive information
console.log("JWT callback - account:", account); console.log("JWT callback - processing token for user:", token.sub);
console.log("JWT callback - profile:", profile);
if (account && profile) { if (account && profile) {
token.accessToken = account.access_token; token.accessToken = account.access_token;
@ -82,7 +81,7 @@ export const authOptions: NextAuthOptions = {
const clientId = getRequiredEnvVar("KEYCLOAK_CLIENT_ID"); const clientId = getRequiredEnvVar("KEYCLOAK_CLIENT_ID");
const clientSecret = getRequiredEnvVar("KEYCLOAK_CLIENT_SECRET"); const clientSecret = getRequiredEnvVar("KEYCLOAK_CLIENT_SECRET");
console.log("Attempting to refresh token..."); console.log("Attempting to refresh token for user:", token.sub);
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`,
{ {
@ -100,10 +99,9 @@ export const authOptions: NextAuthOptions = {
); );
const tokens = await response.json(); const tokens = await response.json();
console.log("Token refresh response:", tokens);
if (!response.ok) { if (!response.ok) {
console.error("Token refresh failed:", tokens); console.error("Token refresh failed for user:", token.sub);
throw new Error("RefreshAccessTokenError"); throw new Error("RefreshAccessTokenError");
} }
@ -114,7 +112,7 @@ export const authOptions: NextAuthOptions = {
accessTokenExpires: Date.now() + tokens.expires_in * 1000, accessTokenExpires: Date.now() + tokens.expires_in * 1000,
}; };
} catch (error) { } catch (error) {
console.error("Error refreshing token:", error); console.error("Error refreshing token for user:", token.sub);
// Return token with error flag - this will trigger a redirect to sign-in // Return token with error flag - this will trigger a redirect to sign-in
return { return {
@ -125,11 +123,10 @@ export const authOptions: NextAuthOptions = {
}, },
async session({ session, token }) { async session({ session, token }) {
console.log("Session callback - session:", session); console.log("Session callback - processing session for user:", token.sub);
console.log("Session callback - token:", token);
if (token.error) { if (token.error) {
console.error("Token error detected:", token.error); console.error("Token error detected for user:", token.sub);
// Force sign out if there was a refresh error // Force sign out if there was a refresh error
throw new Error("RefreshAccessTokenError"); throw new Error("RefreshAccessTokenError");
} }
@ -148,7 +145,7 @@ export const authOptions: NextAuthOptions = {
}, },
events: { events: {
async signOut({ token }) { async signOut({ token }) {
console.log("Sign out event - token:", token); console.log("Sign out event - processing logout for user:", token.sub);
if (token.refreshToken) { if (token.refreshToken) {
try { try {
await fetch( await fetch(
@ -166,12 +163,12 @@ export const authOptions: NextAuthOptions = {
} }
); );
} catch (error) { } catch (error) {
console.error("Error during logout:", error); console.error("Error during logout for user:", token.sub);
} }
} }
}, },
}, },
debug: true, // Enable debug logging debug: process.env.NODE_ENV === 'development', // Only enable debug logging in development
}; };
const handler = NextAuth(authOptions); const handler = NextAuth(authOptions);

View File

@ -16,8 +16,13 @@ interface Task {
details: string | null; details: string | null;
} }
interface ProjectTasks {
projectName: string;
tasks: Task[];
}
export function Flow() { export function Flow() {
const [tasks, setTasks] = useState<Task[]>([]); const [projectTasks, setProjectTasks] = useState<ProjectTasks[]>([]);
const [error, setError] = useState<string | null>(null); const [error, setError] = useState<string | null>(null);
const [loading, setLoading] = useState(true); const [loading, setLoading] = useState(true);
const [refreshing, setRefreshing] = useState(false); const [refreshing, setRefreshing] = useState(false);
@ -44,9 +49,35 @@ export function Flow() {
const data = await response.json(); const data = await response.json();
if (data.tasks && Array.isArray(data.tasks)) { if (data.tasks && Array.isArray(data.tasks)) {
setTasks(data.tasks); // Group tasks by project
const groupedTasks = data.tasks.reduce((acc: { [key: string]: Task[] }, task: Task) => {
if (!acc[task.projectName]) {
acc[task.projectName] = [];
}
acc[task.projectName].push(task);
return acc;
}, {});
// Convert to array and sort tasks by due date within each project
const sortedProjectTasks = Object.entries(groupedTasks).map(([projectName, tasks]) => ({
projectName,
tasks: tasks.sort((a, b) => {
if (!a.dueDate) return 1;
if (!b.dueDate) return -1;
return new Date(a.dueDate).getTime() - new Date(b.dueDate).getTime();
}).slice(0, 6) // Limit to 6 tasks per project
}));
// Sort projects by earliest due date
sortedProjectTasks.sort((a, b) => {
const aEarliest = a.tasks[0]?.dueDate ? new Date(a.tasks[0].dueDate).getTime() : Infinity;
const bEarliest = b.tasks[0]?.dueDate ? new Date(b.tasks[0].dueDate).getTime() : Infinity;
return aEarliest - bEarliest;
});
setProjectTasks(sortedProjectTasks);
} else { } else {
setTasks([]); setProjectTasks([]);
} }
setError(null); setError(null);
} catch (err) { } catch (err) {
@ -67,6 +98,16 @@ export function Flow() {
}; };
}, []); }, []);
const formatDate = (dateString: string | null) => {
if (!dateString) return 'No due date';
const date = new Date(dateString);
return date.toLocaleDateString('fr-FR', {
day: 'numeric',
month: 'short',
year: 'numeric'
});
};
return ( return (
<Card className="transition-transform duration-500 ease-in-out transform hover:scale-105"> <Card className="transition-transform duration-500 ease-in-out transform hover:scale-105">
<CardHeader className="flex flex-row items-center justify-between pb-2"> <CardHeader className="flex flex-row items-center justify-between pb-2">
@ -88,26 +129,33 @@ export function Flow() {
</div> </div>
) : error ? ( ) : error ? (
<div className="text-center text-sm text-red-500">{error}</div> <div className="text-center text-sm text-red-500">{error}</div>
) : tasks.length === 0 ? ( ) : projectTasks.length === 0 ? (
<div className="text-center text-sm text-gray-500">No tasks found</div> <div className="text-center text-sm text-gray-500">No tasks found</div>
) : ( ) : (
<div className="space-y-6"> <div className="space-y-6">
{tasks.map((task) => ( {projectTasks.map((project) => (
<div key={task.id} className="space-y-2"> <div key={project.projectName} className="space-y-2">
<h3 className="font-medium text-gray-900">{task.projectName}</h3> <h3 className="font-medium text-gray-900">{project.projectName}</h3>
<div className="space-y-1"> <div className="space-y-1">
<div className="flex items-center justify-between p-2 rounded-lg bg-white shadow-sm"> {project.tasks.map((task) => (
<span className="text-sm font-medium text-gray-700"> <div key={task.id} className="flex items-center justify-between p-2 rounded-lg bg-white shadow-sm">
{task.headline} <span className="text-sm font-medium text-gray-700">
</span> {task.headline}
<span className={`px-2 py-1 rounded-full text-xs font-medium ${ </span>
task.status === 'NEW' ? 'bg-blue-100 text-blue-800' : <div className="flex items-center space-x-2">
task.status === 'INPROGRESS' ? 'bg-yellow-100 text-yellow-800' : <span className={`px-2 py-1 rounded-full text-xs font-medium ${
'bg-green-100 text-green-800' task.status === 'NEW' ? 'bg-blue-100 text-blue-800' :
}`}> task.status === 'INPROGRESS' ? 'bg-yellow-100 text-yellow-800' :
{task.status} 'bg-green-100 text-green-800'
</span> }`}>
</div> {task.status}
</span>
<span className="text-xs text-gray-500">
{formatDate(task.dueDate)}
</span>
</div>
</div>
))}
</div> </div>
</div> </div>
))} ))}