weiter die Sache mit Auth

This commit is contained in:
2026-03-01 07:52:31 +00:00
parent 0678fdcaa7
commit 1ccd66b307
16 changed files with 693 additions and 5 deletions

50
lib/auth.ts Normal file
View File

@@ -0,0 +1,50 @@
/**
* Reusable authentication library
* Configure users via environment variables in .env:
* AUTH_USERS=user1:$2a$10$hash1,user2:$2a$10$hash2
*
* Use scripts/generate-password.js to generate password hashes
*/
export interface User {
username: string;
password: string;
}
/**
* Parse users from environment variable
* Format: username:password,username2:password2
*/
export function getUsers(): User[] {
const usersString = process.env.AUTH_USERS || '';
if (!usersString) {
console.warn('AUTH_USERS not configured in .env');
return [];
}
return usersString
.split(',')
.map((userPair) => {
const [username, password] = userPair.trim().split(':');
return { username: username?.trim(), password: password?.trim() };
})
.filter((user) => user.username && user.password);
}
/**
* Verify user credentials
*/
export function verifyCredentials(username: string, password: string): boolean {
const users = getUsers();
const user = users.find(u => u.username === username);
if (!user) {
return false;
}
return user.password === password;
}
/**
* Check if authentication is enabled
*/
export function isAuthEnabled(): boolean {
return !!process.env.AUTH_USERS;
}