From d368e07e972e926bf9c65c7a597ba3d06f6dc65e Mon Sep 17 00:00:00 2001 From: Alma Date: Sat, 12 Apr 2025 10:10:28 +0200 Subject: [PATCH] correction flow 4 --- .env | 2 + app/api/auth/[...nextauth]/route.ts | 59 ++++++++++++++++++++--------- 2 files changed, 44 insertions(+), 17 deletions(-) diff --git a/.env b/.env index ec3b8509..bd2d62de 100644 --- a/.env +++ b/.env @@ -42,3 +42,5 @@ NEXT_PUBLIC_IFRAME_AI_ASSISTANT_URL=https://alma.slm-lab.net ROCKET_CHAT_TOKEN=w91TYgkH-Z67Oz72usYdkW5TZLLRwnre7qyAhp7aHJB ROCKET_CHAT_USER_ID=Tpuww59PJKsrGNQJB +LEANTIME_API_KEY=lt_lsdShQdoYHaPUWuL07XZR1Rf3GeySsIs_UDlll3VJPk5EwAuILpMC4BwzJ9MZFRrb +LEANTIME_API_URL=https://agilite.slm-lab.net \ No newline at end of file diff --git a/app/api/auth/[...nextauth]/route.ts b/app/api/auth/[...nextauth]/route.ts index 181f1e72..1289bb40 100644 --- a/app/api/auth/[...nextauth]/route.ts +++ b/app/api/auth/[...nextauth]/route.ts @@ -83,31 +83,44 @@ export const authOptions: NextAuthOptions = { token.first_name = profile.given_name || ''; token.last_name = profile.family_name || ''; token.role = profile.groups || []; - token.accessToken = account.access_token; - token.refreshToken = account.refresh_token; + token.accessToken = account.access_token || ''; + token.refreshToken = account.refresh_token || ''; token.accessTokenExpires = account.expires_at ? account.expires_at * 1000 : Date.now() + 60 * 60 * 1000; // Create or update Leantime user try { - const leantimeResponse = await fetch(`${process.env.LEANTIME_URL}/api/users`, { + const leantimeResponse = await fetch(`${process.env.LEANTIME_API_URL}/api/jsonrpc`, { method: 'POST', headers: { 'Content-Type': 'application/json', - 'apiKey': process.env.LEANTIME_API_KEY!, + 'x-api-key': process.env.LEANTIME_API_KEY!, }, body: JSON.stringify({ - firstname: token.first_name, - lastname: token.last_name, - username: token.username, - email: profile.email, - role: token.role.includes('admin') ? 'admin' : 'user', + method: 'leantime.rpc.Users.Users.addUser', + jsonrpc: '2.0', + id: 1, + params: { + values: { + firstname: token.first_name, + lastname: token.last_name, + username: token.username, + email: profile.email, + role: token.role.includes('admin') ? 'admin' : 'user', + password: Math.random().toString(36).slice(-8), // Generate a random password + status: 'a', // Active status + source: 'keycloak', + } + } }), }); - if (!leantimeResponse.ok) { - console.error('Failed to create/update Leantime user:', await leantimeResponse.text()); + const responseData = await leantimeResponse.json(); + console.log('Leantime response:', responseData); + + if (!leantimeResponse.ok || !responseData.result) { + console.error('Failed to create/update Leantime user:', responseData); } else { - console.log('Successfully created/updated Leantime user'); + console.log('Successfully created/updated Leantime user with ID:', responseData.result); } } catch (error) { console.error('Error creating/updating Leantime user:', error); @@ -194,15 +207,27 @@ export const authOptions: NextAuthOptions = { // Delete user from Leantime if (token.username) { - const leantimeResponse = await fetch(`${process.env.LEANTIME_URL}/api/users/${token.username}`, { - method: 'DELETE', + const leantimeResponse = await fetch(`${process.env.LEANTIME_API_URL}/api/jsonrpc`, { + method: 'POST', headers: { - 'apiKey': process.env.LEANTIME_API_KEY!, + 'Content-Type': 'application/json', + 'x-api-key': process.env.LEANTIME_API_KEY!, }, + body: JSON.stringify({ + method: 'leantime.rpc.Users.Users.deleteUser', + jsonrpc: '2.0', + id: 1, + params: { + id: token.username + } + }), }); - if (!leantimeResponse.ok) { - console.error('Failed to delete Leantime user:', await leantimeResponse.text()); + const responseData = await leantimeResponse.json(); + console.log('Leantime delete response:', responseData); + + if (!leantimeResponse.ok || !responseData.result) { + console.error('Failed to delete Leantime user:', responseData); } else { console.log('Successfully deleted Leantime user'); }