File size: 3,636 Bytes
f72291c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Login - Ollama Manager</title>
    <link rel="stylesheet" href="/static/style.css">
</head>
<body>
    <div class="container">
        <header>
            <h1>Ollama Model Manager</h1>
        </header>
        <div class="auth-box">
            <div class="tabs">
                <button class="tab active" onclick="showTab('login')">Login</button>
                <button class="tab" onclick="showTab('register')">Register</button>
            </div>
            <form id="login-form" class="auth-form" onsubmit="handleLogin(event)">
                <h2>Login</h2>
                <input type="text" name="username" placeholder="Username" required>
                <input type="password" name="password" placeholder="Password" required>
                <button type="submit" class="btn btn-primary">Login</button>
                <p id="login-error" class="error"></p>
            </form>
            <form id="register-form" class="auth-form hidden" onsubmit="handleRegister(event)">
                <h2>Register</h2>
                <input type="text" name="username" placeholder="Username" required>
                <input type="email" name="email" placeholder="Email" required>
                <input type="password" name="password" placeholder="Password" required>
                <button type="submit" class="btn btn-primary">Register</button>
                <p id="register-error" class="error"></p>
            </form>
        </div>
    </div>
    <script>
        function showTab(name) {
            document.querySelectorAll('.tab').forEach(t => t.classList.remove('active'));
            document.querySelectorAll('.auth-form').forEach(f => f.classList.add('hidden'));
            document.querySelector(`.tab[onclick*="${name}"]`).classList.add('active');
            document.getElementById(`${name}-form`).classList.remove('hidden');
        }

        async function handleLogin(e) {
            e.preventDefault();
            const fd = new FormData(e.target);
            const res = await fetch('/api/login', {
                method: 'POST',
                headers: {'Content-Type': 'application/json'},
                body: JSON.stringify({username: fd.get('username'), password: fd.get('password')})
            });
            if (res.ok) {
                const data = await res.json();
                localStorage.setItem('token', data.access_token);
                window.location.href = '/dashboard';
            } else {
                const err = await res.json();
                document.getElementById('login-error').textContent = err.detail || 'Login failed';
            }
        }

        async function handleRegister(e) {
            e.preventDefault();
            const fd = new FormData(e.target);
            const res = await fetch('/api/register', {
                method: 'POST',
                headers: {'Content-Type': 'application/json'},
                body: JSON.stringify({username: fd.get('username'), email: fd.get('email'), password: fd.get('password')})
            });
            if (res.ok) {
                document.getElementById('register-error').textContent = 'Registered! You can now login.';
                document.getElementById('register-error').style.color = 'green';
            } else {
                const err = await res.json();
                document.getElementById('register-error').textContent = err.detail || 'Registration failed';
            }
        }
    </script>
</body>
</html>