weewoo
This commit is contained in:
52
lib/utils/auth.ts
Normal file
52
lib/utils/auth.ts
Normal file
@@ -0,0 +1,52 @@
|
||||
/**
|
||||
* Auth utilities for managing authentication state
|
||||
*/
|
||||
|
||||
/**
|
||||
* Get the authentication token from cookies or localStorage
|
||||
*/
|
||||
export function getAuthToken(): string | null {
|
||||
return document.cookie
|
||||
.split('; ')
|
||||
.find(row => row.startsWith('Authorization='))
|
||||
?.split('=')[1] || localStorage.getItem('Authorization');
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the user is logged in
|
||||
*/
|
||||
export function isLoggedIn(): boolean {
|
||||
return !!getAuthToken();
|
||||
}
|
||||
|
||||
/**
|
||||
* Logout the user by removing auth tokens and redirecting
|
||||
*/
|
||||
export async function logoutUser(): Promise<void> {
|
||||
const token = getAuthToken();
|
||||
|
||||
if (token) {
|
||||
try {
|
||||
await fetch(`/api/auth/logout`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Authorization': `Bearer ${token}`,
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
}).catch(err => {
|
||||
console.warn('Server logout failed:', err);
|
||||
});
|
||||
} catch (error) {
|
||||
console.warn('Error during server logout:', error);
|
||||
}
|
||||
}
|
||||
|
||||
// Remove the auth token from cookies
|
||||
document.cookie = 'Authorization=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT; Secure; SameSite=Strict';
|
||||
|
||||
// Remove from localStorage as backup
|
||||
localStorage.removeItem('Authorization');
|
||||
|
||||
// Redirect to login page
|
||||
window.location.href = '/auth/login';
|
||||
}
|
||||
13
lib/utils/general.ts
Normal file
13
lib/utils/general.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { clsx, type ClassValue } from "clsx"
|
||||
import { twMerge } from "tailwind-merge"
|
||||
|
||||
export function cn(...inputs: ClassValue[]): string {
|
||||
return twMerge(clsx(inputs))
|
||||
}
|
||||
|
||||
export const getGreeting = () => {
|
||||
const hour = new Date().getHours()
|
||||
if (hour < 12) return "Good morning"
|
||||
if (hour < 18) return "Good afternoon"
|
||||
return "Good evening"
|
||||
}
|
||||
50
lib/utils/git.ts
Normal file
50
lib/utils/git.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
// Git utility functions
|
||||
|
||||
interface GitInfo {
|
||||
hash: string;
|
||||
date: string;
|
||||
message: string;
|
||||
}
|
||||
|
||||
// Default git info if file is not found
|
||||
const defaultGitInfo: GitInfo = {
|
||||
hash: 'local-dev',
|
||||
date: new Date().toISOString(),
|
||||
message: 'Local development build',
|
||||
};
|
||||
|
||||
/**
|
||||
* Get git info - safely handles cases where the JSON file isn't available
|
||||
*/
|
||||
export function getGitInfo(): GitInfo {
|
||||
try {
|
||||
// In production builds, the git info should be available
|
||||
// In development, we'll use default values
|
||||
const gitInfo = {
|
||||
hash: process.env.NEXT_PUBLIC_GIT_HASH || 'dev',
|
||||
date: process.env.NEXT_PUBLIC_GIT_DATE || new Date().toISOString(),
|
||||
message: process.env.NEXT_PUBLIC_GIT_MESSAGE || 'Development build',
|
||||
};
|
||||
|
||||
return gitInfo;
|
||||
} catch (error) {
|
||||
console.warn('Could not load git info, using defaults', error);
|
||||
return defaultGitInfo;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a shorter git hash for display
|
||||
*/
|
||||
export function getShortGitHash(): string {
|
||||
const { hash } = getGitInfo();
|
||||
return hash.substring(0, 7);
|
||||
}
|
||||
|
||||
/**
|
||||
* Format git commit date for display
|
||||
*/
|
||||
export function getFormattedGitDate(): string {
|
||||
const { date } = getGitInfo();
|
||||
return new Date(date).toLocaleDateString();
|
||||
}
|
||||
16
lib/utils/index.ts
Normal file
16
lib/utils/index.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
/**
|
||||
* Main utilities index file
|
||||
* Re-exports all utility functions from their respective modules
|
||||
*/
|
||||
|
||||
// Re-export general utils
|
||||
export * from './general';
|
||||
|
||||
// Re-export auth utils
|
||||
export * from './auth';
|
||||
|
||||
// Re-export git utils
|
||||
export * from './git';
|
||||
|
||||
// Re-export style utils
|
||||
export * from './styles';
|
||||
11
lib/utils/styles.ts
Normal file
11
lib/utils/styles.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import { clsx, type ClassValue } from "clsx";
|
||||
import { twMerge } from "tailwind-merge";
|
||||
|
||||
/**
|
||||
* Utility function for merging Tailwind CSS class names with conditional logic.
|
||||
* @param inputs - Class values to merge.
|
||||
* @returns A properly merged class name string.
|
||||
*/
|
||||
export function cn(...inputs: ClassValue[]): string {
|
||||
return twMerge(clsx(inputs));
|
||||
}
|
||||
Reference in New Issue
Block a user