85 lines
1.8 KiB
TypeScript
85 lines
1.8 KiB
TypeScript
import { type ClassValue, clsx } from "clsx"
|
|
import { twMerge } from "tailwind-merge"
|
|
|
|
export function cn(...inputs: ClassValue[]) {
|
|
return twMerge(clsx(inputs))
|
|
}
|
|
|
|
// Session management utilities
|
|
export interface User {
|
|
id: number;
|
|
nim?: string;
|
|
username?: string;
|
|
nip?: string;
|
|
role: string;
|
|
}
|
|
|
|
export interface Session {
|
|
isAuthenticated: boolean;
|
|
user?: User;
|
|
expiresAt?: string;
|
|
issuedAt?: string;
|
|
}
|
|
|
|
// Check if user is authenticated
|
|
export async function checkAuth(): Promise<Session> {
|
|
try {
|
|
const response = await fetch('/api/auth/check', {
|
|
method: 'GET',
|
|
credentials: 'include',
|
|
});
|
|
|
|
if (response.ok) {
|
|
const data = await response.json();
|
|
return data;
|
|
} else {
|
|
return { isAuthenticated: false };
|
|
}
|
|
} catch (error) {
|
|
console.error('Auth check error:', error);
|
|
return { isAuthenticated: false };
|
|
}
|
|
}
|
|
|
|
// Logout user
|
|
export async function logout(): Promise<boolean> {
|
|
try {
|
|
const response = await fetch('/api/auth/logout', {
|
|
method: 'POST',
|
|
credentials: 'include',
|
|
});
|
|
|
|
if (response.ok) {
|
|
// Clear any client-side state
|
|
localStorage.removeItem('sidebarCollapsed');
|
|
return true;
|
|
}
|
|
return false;
|
|
} catch (error) {
|
|
console.error('Logout error:', error);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// Check if session is expired
|
|
export function isSessionExpired(expiresAt?: string): boolean {
|
|
if (!expiresAt) return true;
|
|
|
|
const expirationTime = new Date(expiresAt).getTime();
|
|
const currentTime = Date.now();
|
|
|
|
return currentTime >= expirationTime;
|
|
}
|
|
|
|
// Format date for display
|
|
export function formatDate(dateString: string): string {
|
|
const date = new Date(dateString);
|
|
return date.toLocaleDateString('id-ID', {
|
|
year: 'numeric',
|
|
month: 'long',
|
|
day: 'numeric',
|
|
hour: '2-digit',
|
|
minute: '2-digit'
|
|
});
|
|
}
|