Implement initial server, authentication, and chat functionality with responsive UI

This commit is contained in:
2025-10-28 15:47:14 +01:00
commit c1684be204
8 changed files with 1031 additions and 0 deletions
+41
View File
@@ -0,0 +1,41 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"/>
<title>DeepFake</title>
<link rel="stylesheet" href="style.css"/>
</head>
<body>
<div id="auth" class="card">
<h1>Welcome to DeepFake</h1>
<input id="username" placeholder="Username" autocomplete="username"/>
<input id="password" type="password" placeholder="Password" autocomplete="current-password"/>
<div class="auth-buttons">
<button id="login">Login</button>
<button id="register">Register</button>
</div>
<p id="authMsg"></p>
</div>
<div id="chat" class="card" style="display:none;">
<div class="chat-header">
<div class="model-select">
<label for="model">Model:</label>
<select id="model">
<option value="mistral:latest">Mistral</option>
<option value="femboy:latest">Femboy</option>
</select>
</div>
</div>
<div id="messages"></div>
<div class="input-container">
<input id="msgInput" placeholder="Type your message..."/>
<button id="send"></button>
</div>
<div class="logout-container">
<button id="clearChat">Clear Chat</button>
<button id="logout">Logout</button>
</div>
</div>
<script src="script.js"></script>
</body>
</html>
+233
View File
@@ -0,0 +1,233 @@
const authDiv = document.getElementById("auth");
const chatDiv = document.getElementById("chat");
const authMsg = document.getElementById("authMsg");
const messagesDiv = document.getElementById("messages");
const usernameInput = document.getElementById("username");
const passwordInput = document.getElementById("password");
const loginBtn = document.getElementById("login");
const registerBtn = document.getElementById("register");
const msgInput = document.getElementById("msgInput");
const sendBtn = document.getElementById("send");
let isProcessing = false;
let chatHistory = JSON.parse(localStorage.getItem('chatHistory')) || [];
async function post(url, data) {
try {
const res = await fetch(url, {
method: "POST",
headers: {"Content-Type": "application/json"},
body: JSON.stringify(data)
});
return await res.json();
} catch (err) {
return {error: "Network error — please try again."};
}
}
async function get(url) {
try {
const res = await fetch(url);
return await res.json();
} catch (err) {
return {error: "Network error — please try again."};
}
}
function setAuthMessage(msg, isError = false) {
authMsg.textContent = msg;
authMsg.className = isError ? 'error' : 'success';
}
function addMessage(content, isUser = false, isLoading = false, saveToHistory = true) {
const msgDiv = document.createElement('div');
msgDiv.className = `msg ${isUser ? 'user' : 'bot'}${isLoading ? ' loading' : ''}`;
if (isLoading) {
msgDiv.innerHTML = `
<div class="typing-indicator">
<div class="typing-dot"></div>
<div class="typing-dot"></div>
<div class="typing-dot"></div>
</div>
`;
} else {
msgDiv.textContent = content;
// Save to history (but not loading messages)
if (saveToHistory) {
chatHistory.push({
content: content,
isUser: isUser,
timestamp: Date.now()
});
localStorage.setItem('chatHistory', JSON.stringify(chatHistory));
}
}
messagesDiv.appendChild(msgDiv);
// Smooth scroll to bottom
setTimeout(() => {
messagesDiv.scrollTop = messagesDiv.scrollHeight;
}, 100);
return msgDiv;
}
function loadChatHistory() {
messagesDiv.innerHTML = '';
chatHistory.forEach(msg => {
addMessage(msg.content, msg.isUser, false, false);
});
}
function clearChatHistory() {
chatHistory = [];
localStorage.removeItem('chatHistory');
messagesDiv.innerHTML = '';
}
function setLoadingState(loading) {
isProcessing = loading;
sendBtn.disabled = loading;
msgInput.disabled = loading;
if (loading) {
sendBtn.classList.add('loading');
msgInput.placeholder = "Verarbeite Antwort...";
} else {
sendBtn.classList.remove('loading');
msgInput.placeholder = "Type your message...";
}
}
async function checkSession() {
const res = await get('/api/session');
if (res.user) {
authDiv.style.display = "none";
chatDiv.style.display = "grid";
loadChatHistory();
msgInput.focus();
return true;
}
return false;
}
async function loadServerChat() {
const res = await get("/api/chat/history");
if (res.history && res.history.length) {
messagesDiv.innerHTML = '';
res.history.forEach(msg => {
addMessage(
msg.content,
msg.role === "user",
false,
false
);
});
messagesDiv.scrollTop = messagesDiv.scrollHeight;
}
}
async function handleAuth(endpoint) {
const username = usernameInput.value.trim();
const password = passwordInput.value.trim();
if (!username || !password) {
setAuthMessage("Please enter both username and password.", true);
return;
}
loginBtn.disabled = true;
registerBtn.disabled = true;
setAuthMessage("Processing...", false);
const res = await post(`/api/${endpoint}`, {username, password});
loginBtn.disabled = false;
registerBtn.disabled = false;
if (res.error) {
setAuthMessage(res.error, true);
} else {
if (endpoint === "login") {
setAuthMessage("Login successful!", false);
authDiv.style.display = "none";
chatDiv.style.display = "grid";
await loadServerChat();
msgInput.focus();
} else {
setAuthMessage("Registration successful — you can now log in!", false);
}
}
}
async function sendMessage() {
if (isProcessing) return;
const msg = msgInput.value.trim();
if (!msg) return;
addMessage(`You: ${msg}`, true);
msgInput.value = "";
msgInput.style.height = 'auto';
setLoadingState(true);
const loadingMsg = addMessage('', false, true, false);
try {
const model = document.getElementById("model").value;
const res = await post("/api/chat", { message: msg, model });
if (messagesDiv.contains(loadingMsg)) {
messagesDiv.removeChild(loadingMsg);
}
if (res.reply) {
addMessage(`DeepFake: ${res.reply}`, false);
} else {
addMessage(`[Error: ${res.error}]`, false);
}
} catch (error) {
if (messagesDiv.contains(loadingMsg)) {
messagesDiv.removeChild(loadingMsg);
}
addMessage(`[Error: Network error]`, false);
} finally {
setLoadingState(false);
msgInput.focus();
}
}
loginBtn.onclick = () => handleAuth("login");
registerBtn.onclick = () => handleAuth("register");
passwordInput.addEventListener("keydown", (e) => {
if (e.key === "Enter") handleAuth("login");
});
document.getElementById("logout").onclick = async () => {
await post("/api/logout", {});
chatDiv.style.display = "none";
authDiv.style.display = "grid";
clearChatHistory();
setAuthMessage("Logged out.", false);
usernameInput.value = "";
passwordInput.value = "";
};
sendBtn.onclick = sendMessage;
msgInput.addEventListener("keydown", (e) => {
if (e.key === "Enter" && !e.shiftKey) {
e.preventDefault();
sendMessage();
}
});
document.getElementById("clearChat").onclick = async () => {
await post("/api/chat/clear", {});
clearChatHistory();
};
msgInput.addEventListener('input', function () {
this.style.height = 'auto';
this.style.height = Math.min(this.scrollHeight, 120) + 'px';
});
window.addEventListener('load', () => {
checkSession();
});
+493
View File
@@ -0,0 +1,493 @@
:root {
--bg-dark: #0a0a0f;
--bg-panel: #1a1a2e;
--bg-message-user: #2563eb;
--bg-message-bot: #374151;
--accent: #3b82f6;
--accent-hover: #2563eb;
--text-primary: #f8fafc;
--text-secondary: #94a3b8;
--text-muted: #64748b;
--border: #334155;
--error: #ef4444;
--success: #22c55e;
--shadow: rgba(0, 0, 0, 0.3);
--gradient: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}
html, body {
height: 100%;
min-height: 0;
}
* {
box-sizing: border-box;
}
.card {
background: rgba(26, 26, 46, 0.9);
backdrop-filter: blur(20px);
border: 1px solid rgba(255, 255, 255, 0.1);
border-radius: 24px;
box-shadow: 0 25px 50px -12px var(--shadow),
0 0 0 1px rgba(255, 255, 255, 0.05);
transition: all 0.3s ease;
position: relative;
overflow: hidden;
min-height: 0;
}
.card::before {
content: '';
position: absolute;
top: 0;
left: 0;
right: 0;
height: 1px;
opacity: 0.6;
}
#auth {
padding: 40px 35px;
width: 420px;
max-width: 90vw;
}
#auth:hover {
transform: translateY(-4px);
box-shadow: 0 35px 60px -12px var(--shadow),
0 0 0 1px rgba(255, 255, 255, 0.08);
}
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Inter', sans-serif;
background: linear-gradient(135deg, #0f0f23 0%, #1a1a2e 50%, #16213e 100%) fixed;
color: var(--text-primary);
display: flex;
justify-content: center;
align-items: stretch;
height: 100vh;
overflow: hidden;
margin: 0;
padding: 20px;
min-height: 0;
}
#chat {
display: grid;
grid-template-rows: 1fr auto auto;
row-gap: 15px;
height: 90vh;
width: 90%;
max-width: 95vw;
padding: 25px;
background: rgba(26,26,46,0.9);
border-radius: 24px;
box-shadow: 0 25px 50px -12px var(--shadow);
min-height: 0;
}
#messages {
grid-row: 1;
overflow-y: auto;
overflow-x: hidden;
display: flex;
flex-direction: column;
gap: 16px;
padding: 20px;
margin: 0;
background: rgba(15,15,35,0.6);
border: 1px solid rgba(255,255,255,0.05);
border-radius: 20px;
backdrop-filter: blur(10px);
scroll-behavior: smooth;
min-height: 0;
}
.input-container {
grid-row: 2;
display: flex;
align-items: flex-end;
gap: 12px;
flex-shrink: 0;
min-height: 70px;
}
.logout-container {
grid-row: 3;
text-align: center;
margin-top: 10px;
}
#messages::-webkit-scrollbar {
width: 8px;
}
#messages::-webkit-scrollbar-thumb {
background: var(--border);
border-radius: 4px;
}
#messages::-webkit-scrollbar-thumb:hover {
background: var(--text-muted);
}
#chat h1 {
flex-shrink: 0;
margin-bottom: 20px;
display: flex;
align-items: center;
justify-content: center;
}
h1 {
margin: 0 0 30px;
color: var(--text-primary);
font-size: 2rem;
font-weight: 700;
text-align: center;
background: var(--gradient);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
background-clip: text;
letter-spacing: -0.02em;
}
input, textarea {
width: 100%;
margin: 0 0 20px;
padding: 16px 20px;
border: 2px solid transparent;
background: rgba(15, 15, 35, 0.8);
border-radius: 16px;
color: var(--text-primary);
outline: none;
font-size: 16px;
font-family: inherit;
transition: all 0.3s ease;
backdrop-filter: blur(10px);
resize: none;
}
input::placeholder, textarea::placeholder {
color: var(--text-muted);
}
input:focus, textarea:focus {
border-color: var(--accent);
background: rgba(15, 15, 35, 1);
transform: translateY(-1px);
box-shadow: 0 10px 25px rgba(59, 130, 246, 0.15);
}
button {
background: var(--accent);
color: white;
padding: 14px 24px;
border: none;
border-radius: 12px;
cursor: pointer;
font-weight: 600;
font-size: 15px;
transition: all 0.3s ease;
position: relative;
overflow: hidden;
}
button::before {
content: '';
position: absolute;
top: 0;
left: -100%;
width: 100%;
height: 100%;
background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
transition: left 0.5s ease;
}
button:hover:not(:disabled) {
background: var(--accent-hover);
transform: translateY(-2px);
box-shadow: 0 10px 25px rgba(59, 130, 246, 0.3);
}
button:hover::before {
left: 100%;
}
button:disabled {
opacity: 0.5;
cursor: not-allowed;
transform: none;
}
.msg {
padding: 16px 20px;
border-radius: 18px;
line-height: 1.5;
max-width: 80%;
word-wrap: break-word;
word-break: break-word;
overflow-wrap: break-word;
animation: fadeIn 0.3s ease;
position: relative;
white-space: pre-wrap;
margin: 0;
flex-shrink: 0;
}
@keyframes fadeIn {
from {
opacity: 0;
transform: translateY(10px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
.msg.user {
background: var(--bg-message-user);
color: white;
align-self: flex-end;
border-bottom-right-radius: 4px;
box-shadow: 0 4px 12px rgba(37, 99, 235, 0.2);
}
.msg.bot {
background: var(--bg-message-bot);
color: var(--text-primary);
align-self: flex-start;
border-bottom-left-radius: 4px;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}
.msg.user::before {
position: absolute;
right: -40px;
top: 16px;
font-size: 18px;
}
.msg.bot::before {
position: absolute;
left: -40px;
top: 16px;
font-size: 18px;
}
.msg.loading {
background: var(--bg-message-bot);
color: var(--text-primary);
align-self: flex-start;
border-bottom-left-radius: 4px;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
padding: 10px 14px;
max-width: fit-content;
display: flex;
align-items: center;
justify-content: center;
}
.typing-indicator {
display: flex;
align-items: center;
gap: 6px;
height: 16px;
}
.typing-dot {
width: 6px;
height: 6px;
border-radius: 50%;
background: var(--text-secondary);
animation: typingAnimation 1.2s infinite ease-in-out;
}
.typing-dot:nth-child(1) {
animation-delay: -0.32s;
}
.typing-dot:nth-child(2) {
animation-delay: -0.16s;
}
@keyframes typingAnimation {
0%, 80%, 100% {
transform: scale(0.8);
opacity: 0.5;
}
40% {
transform: scale(1);
opacity: 1;
}
}
#authMsg {
margin-top: 15px;
font-size: 14px;
text-align: center;
min-height: 20px;
padding: 10px;
border-radius: 8px;
transition: all 0.3s ease;
}
#authMsg.error {
color: var(--error);
background: rgba(239, 68, 68, 0.1);
border: 1px solid rgba(239, 68, 68, 0.2);
}
#authMsg.success {
color: var(--success);
background: rgba(34, 197, 94, 0.1);
border: 1px solid rgba(34, 197, 94, 0.2);
}
#msgInput {
flex: 1;
margin: 0;
border-radius: 20px;
padding: 16px 24px;
min-height: 56px;
max-height: 120px;
overflow-y: auto;
}
#send {
background: var(--accent);
border-radius: 50%;
width: 56px;
height: 56px;
display: flex;
align-items: center;
justify-content: center;
padding: 0;
font-size: 18px;
flex-shrink: 0;
}
#send::after {
content: "➤";
font-weight: bold;
}
#send.loading::after {
content: '';
}
#send.loading {
animation: spin 1s linear infinite;
}
@keyframes spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
#logout {
background: linear-gradient(135deg, #6b7280, #4b5563);
border-radius: 12px;
}
.auth-buttons {
display: flex;
gap: 12px;
justify-content: center;
margin-top: 20px;
}
.logout-container {
text-align: center;
margin-top: 15px;
flex-shrink: 0;
}
.chat-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 15px;
}
.model-select {
display: flex;
align-items: center;
gap: 8px;
font-size: 14px;
color: var(--text-secondary);
}
.model-select select {
background: rgba(15, 15, 35, 0.8);
color: var(--text-primary);
border: 1px solid var(--border);
border-radius: 8px;
padding: 6px 10px;
font-size: 14px;
outline: none;
transition: all 0.2s ease;
}
.model-select select:hover,
.model-select select:focus {
border-color: var(--accent);
background: rgba(15, 15, 35, 1);
}
@media (max-width: 768px) {
#chat {
width: 95vw;
height: 95vh;
max-height: 95vh;
padding: 15px;
}
#auth {
width: 95vw;
padding: 30px 20px;
}
.msg {
max-width: 85%;
}
.msg.user::before {
right: -35px;
font-size: 16px;
}
.msg.bot::before {
left: -35px;
font-size: 16px;
}
h1 {
font-size: 1.8rem;
}
}
@media (max-width: 480px) {
.msg {
max-width: 90%;
}
.msg.user::before,
.msg.bot::before {
display: none;
}
#chat {
padding: 10px;
}
#messages {
padding: 15px;
}
}