-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
MaximilianGT500
committed
Aug 29, 2023
1 parent
9e0926c
commit 0072ca5
Showing
4 changed files
with
88 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
const bcrypt = require('bcrypt'); | ||
const readlineSync = require('readline-sync'); | ||
const jwt = require('jsonwebtoken'); | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const sqlite3 = require('sqlite3').verbose(); | ||
const clc = require('cli-color'); | ||
|
||
require('dotenv').config(); | ||
const { JWT_TOKEN } = process.env; | ||
const db = new sqlite3.Database('./db/datenbank.sqlite'); | ||
|
||
|
||
try { | ||
db.serialize(() => { | ||
const generateToken = (id, username, role) => { | ||
const payload = { | ||
id, | ||
username, | ||
role | ||
}; | ||
const token = jwt.sign(payload, JWT_TOKEN); | ||
return token; | ||
}; | ||
|
||
console.log(clc.whiteBright('\nBitte geben Sie die folgenden Informationen ein, um einen neuen Benutzer zu erstellen oder das Token eines bestehenden Benutzers zu aktualisieren.\nNutze beim Passwort keine Sonderzeichen!\n')); | ||
|
||
const username = readlineSync.question('Benutzername: '); | ||
|
||
const existingUserQuery = 'SELECT * FROM users WHERE username = ?'; | ||
const existingUserValues = [username]; | ||
|
||
db.get(existingUserQuery, existingUserValues, (err, existingUser) => { | ||
if (err) { | ||
console.error(clc.red('\n[ERROR] | » Fehler beim Überprüfen des Benutzernamens:', err)); | ||
return; | ||
} | ||
|
||
if (!existingUser) { | ||
console.log(clc.red('\nBenutzer nicht gefunden.\n')); | ||
return; | ||
} | ||
|
||
const newPassword = readlineSync.question('Neues Passwort (leer lassen, um das Passwort nicht zu ändern): ', { hideEchoBack: true }); | ||
const isAdmin = readlineSync.question('Ist der Benutzer ein Administrator? (Ja/Nein): '); | ||
|
||
const hashedPassword = newPassword ? bcrypt.hashSync(newPassword, 10) : existingUser.password; | ||
const role = isAdmin.toLowerCase() === 'ja' ? 'admin' : 'mitglied'; | ||
const newToken = generateToken(existingUser.id, existingUser.username, role); | ||
|
||
const updateTokenQuery = 'UPDATE users SET password = ?, token = ?, role = ? WHERE id = ?'; | ||
const updateTokenValues = [hashedPassword, newToken, role, existingUser.id]; | ||
|
||
db.run(updateTokenQuery, updateTokenValues, (err) => { | ||
if (err) { | ||
console.error(clc.red('\n[ERROR] | » Fehler beim Aktualisieren des Benutzers:', err)); | ||
return; | ||
} | ||
|
||
console.log(clc.whiteBright('\nBenutzer aktualisiert!\nDas neue Login-Token lautet: ' + newToken + '\n')); | ||
}); | ||
}); | ||
}); | ||
} catch (error) { | ||
console.error(clc.red('[ERROR] | » Fehler:', error)); | ||
} |