feat(FE) : implementasi hak akses
This commit is contained in:
No files matched your search
@@ -1,4 +1,5 @@
|
||||
// server/api/auth/keycloak-callback.ts - EXTENDED SESSION FIX
|
||||
import { syncRole } from '~/services/access';
|
||||
|
||||
// Add this at the top of the file (after imports)
|
||||
const SESSION_DURATION = 1 * 60 * 60; // 1 hour in seconds (3600 seconds)
|
||||
@@ -227,24 +228,19 @@ export default defineEventHandler(async (event) => {
|
||||
console.log(' - Host:', event.node.req.headers.host);
|
||||
|
||||
// Auto-sync hakAkses and user data after successful login
|
||||
let responseUserAcess
|
||||
try {
|
||||
console.log('🔄 Starting auto-sync process...');
|
||||
|
||||
// 1. Sync hakAkses (roles) to hakAkses.json
|
||||
const { syncHakAksesFromRoles } = await import('~/server/utils/hakAksesSync');
|
||||
const hakAksesResult = syncHakAksesFromRoles(clientRoles);
|
||||
console.log(`📋 HakAkses sync result:`, hakAksesResult);
|
||||
|
||||
// 2. Sync user data to users.json
|
||||
const { syncUserData } = await import('~/server/utils/userDataSync');
|
||||
const userResult = syncUserData({
|
||||
id: sessionData.user.id,
|
||||
name: sessionData.user.name,
|
||||
email: sessionData.user.email,
|
||||
roles: clientRoles
|
||||
});
|
||||
console.log(`👤 User sync result: ${userResult.action} - ${userResult.message}`);
|
||||
|
||||
responseUserAcess = await syncRole({
|
||||
client_role: clientRoles,
|
||||
email: sessionData.user.email || '',
|
||||
keycloak_id: sessionData.user.id,
|
||||
name: sessionData.user.name || sessionData.user.preferred_username || 'Unknown',
|
||||
}, tokens.id_token);
|
||||
|
||||
console.log("responseUserAcess", responseUserAcess);
|
||||
|
||||
console.log('✅ Auto-sync process completed successfully');
|
||||
} catch (syncError: any) {
|
||||
// Don't fail the login if sync fails, just log it
|
||||
@@ -261,7 +257,13 @@ export default defineEventHandler(async (event) => {
|
||||
|
||||
// Use sendRedirect - it should include the Set-Cookie header
|
||||
// The browser will receive the cookie and include it in the next request
|
||||
return sendRedirect(event, '/dashboard?authenticated=true', 302);
|
||||
|
||||
// check user access before redirecting to dashboard
|
||||
if(responseUserAcess && responseUserAcess.data){
|
||||
return sendRedirect(event, '/dashboard?authenticated=true', 302);
|
||||
}else{
|
||||
return sendRedirect(event, '/auth/access-denied', 302);
|
||||
}
|
||||
|
||||
} catch (error: any) {
|
||||
console.error('❌ === CALLBACK ERROR ===');
|
||||
|
||||
Reference in New Issue
Block a user