update user login baru dan hakakses

This commit is contained in:
Fanrouver
2025-12-18 15:11:41 +07:00
parent dfcd59481c
commit c95da96017
20 changed files with 2564 additions and 404 deletions

No files matched your search

+63 -28
View File
@@ -19,6 +19,7 @@ const initDb = () => {
const dbPath = getDbPath();
const db = new Database(dbPath);
// Create table if not exists
db.exec(`
CREATE TABLE IF NOT EXISTS users (
id TEXT PRIMARY KEY,
@@ -39,35 +40,44 @@ const initDb = () => {
)
`);
// Migration: Add new columns if they don't exist (for existing databases)
// Migration: Check and add missing columns one by one
try {
db.exec(`
ALTER TABLE users ADD COLUMN realmRoles TEXT DEFAULT '[]';
ALTER TABLE users ADD COLUMN accountRoles TEXT DEFAULT '[]';
ALTER TABLE users ADD COLUMN resourceRoles TEXT DEFAULT '[]';
ALTER TABLE users ADD COLUMN lastLogin INTEGER;
`);
} catch (e: any) {
// Columns might already exist, ignore error
if (!e.message?.includes('duplicate column')) {
console.warn('Migration note:', e.message);
}
}
// Migration: Rename keterangan to lastLogin if exists
try {
// Check if keterangan column exists
const tableInfo = db.prepare("PRAGMA table_info(users)").all() as any[];
const hasKeterangan = tableInfo.some(col => col.name === 'keterangan');
const hasLastLogin = tableInfo.some(col => col.name === 'lastLogin');
const columnNames = tableInfo.map(col => col.name);
if (hasKeterangan && !hasLastLogin) {
// SQLite doesn't support ALTER COLUMN, so we need to recreate the table
// For now, we'll just add lastLogin and leave keterangan (it will be ignored)
console.log('Migration: Adding lastLogin column');
// Add missing columns one by one
if (!columnNames.includes('realmRoles')) {
db.exec(`ALTER TABLE users ADD COLUMN realmRoles TEXT DEFAULT '[]'`);
console.log('✅ Added column: realmRoles');
}
if (!columnNames.includes('accountRoles')) {
db.exec(`ALTER TABLE users ADD COLUMN accountRoles TEXT DEFAULT '[]'`);
console.log('✅ Added column: accountRoles');
}
if (!columnNames.includes('resourceRoles')) {
db.exec(`ALTER TABLE users ADD COLUMN resourceRoles TEXT DEFAULT '[]'`);
console.log('✅ Added column: resourceRoles');
}
if (!columnNames.includes('lastLogin')) {
db.exec(`ALTER TABLE users ADD COLUMN lastLogin INTEGER`);
console.log('✅ Added column: lastLogin');
}
if (!columnNames.includes('given_name')) {
db.exec(`ALTER TABLE users ADD COLUMN given_name TEXT`);
console.log('✅ Added column: given_name');
}
if (!columnNames.includes('family_name')) {
db.exec(`ALTER TABLE users ADD COLUMN family_name TEXT`);
console.log('✅ Added column: family_name');
}
} catch (e: any) {
console.warn('Migration check note:', e.message);
console.error('Migration error:', e.message);
// Don't throw, continue with existing schema
}
return db;
@@ -207,7 +217,15 @@ export const syncUserFromTokens = (
existingUser.family_name !== (idTokenPayload.family_name || null) ||
(existingUser.tipeUser === '' && tipeUser !== ''); // Only update if empty
if (needsUpdate) {
// Always update lastLogin when loginTime is provided (user is logging in)
// Check if lastLogin needs updating (if loginTime provided or new timestamp is newer)
const existingLastLogin = existingUser.lastLogin || 0;
const needsLastLoginUpdate = loginTime !== undefined
? true // Always update on login when loginTime is provided
: (lastLoginTimestamp > existingLastLogin); // Only update if newer when not a login event
// Update if any data changed OR if lastLogin needs updating
if (needsUpdate || needsLastLoginUpdate) {
// Update user data
// Only update tipeUser if it's currently empty (preserve manual edits)
const updateTipeUser = existingUser.tipeUser === '' ? tipeUser : existingUser.tipeUser;
@@ -244,7 +262,11 @@ export const syncUserFromTokens = (
userId
);
console.log("✅ User data updated:", userId);
if (needsLastLoginUpdate && !needsUpdate) {
console.log("✅ User lastLogin updated:", userId, "new timestamp:", lastLoginTimestamp);
} else {
console.log("✅ User data updated:", userId);
}
db.close();
return {
success: true,
@@ -262,6 +284,14 @@ export const syncUserFromTokens = (
}
} else {
// New user - insert
console.log(" Inserting new user:", {
userId,
namaLengkap,
namaUser,
email,
lastLoginTimestamp
});
db.prepare(`
INSERT INTO users (
id, namaLengkap, namaUser, email, roles, realmRoles, accountRoles, resourceRoles, groups,
@@ -283,12 +313,17 @@ export const syncUserFromTokens = (
lastLoginTimestamp // lastLogin - from session createdAt
);
console.log("✅ New user saved:", userId);
console.log("✅ New user saved to database:", {
userId,
namaUser,
namaLengkap,
lastLogin: lastLoginTimestamp ? new Date(lastLoginTimestamp * 1000).toISOString() : 'null'
});
db.close();
return {
success: true,
action: 'created',
message: 'New user saved successfully'
message: `New user ${namaUser} saved successfully`
};
}
} catch (error: any) {