Mist, jetzt vielleicht
This commit is contained in:
36
lib/auth.ts
Normal file
36
lib/auth.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
/**
|
||||
* Authentifizierungsbibliothek
|
||||
* Benutzer via Umgebungsvariable konfigurieren:
|
||||
* AUTH_USERS=user1:passwort1,user2:passwort2
|
||||
*/
|
||||
|
||||
export interface User {
|
||||
username: string;
|
||||
password: string;
|
||||
}
|
||||
|
||||
export function getUsers(): User[] {
|
||||
const usersString = process.env.AUTH_USERS || '';
|
||||
if (!usersString) {
|
||||
console.warn('AUTH_USERS nicht in .env konfiguriert');
|
||||
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);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
export function isAuthEnabled(): boolean {
|
||||
return !!process.env.AUTH_USERS;
|
||||
}
|
||||
Reference in New Issue
Block a user