File size: 3,351 Bytes
e53c2d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
82
83
84
{% extends "base.html" %}

{% block title %}
    <title>User Registration</title>
{% endblock %}

{% block content %}
    <div class="login-body">
        <h1>Register New User</h1>
        <form id="registrationForm">
            <div class="form-group">
                <label class="login-label" for="email">Email:</label>
                <input class="input-field" type="email" id="email" name="email" required>
                <div id="emailError" class="error"></div>
            </div>
            <div class="form-group">
                <label class="login-label" for="password">Password (8-32 characters):</label>
                <input class="input-field" type="password" id="password" name="password" required minlength="8" maxlength="32">
                <div id="passwordError" class="error"></div>
            </div>
            <button class="login-button" type="submit">Register</button>
        </form>
    </div>
{% endblock %}

{% block body_scripts %}
    <script>
        document.getElementById('registrationForm').addEventListener('submit', async function(e) {
            e.preventDefault();
            
            // Clear previous errors
            document.getElementById('emailError').textContent = '';
            document.getElementById('passwordError').textContent = '';
            
            const email = document.getElementById('email').value;
            const password = document.getElementById('password').value;
            
            try {
                const response = await fetch('/new_user', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({
                        email: email,
                        password: password
                    })
                });
                
                const data = await response.json();
                
                if (!response.ok) {
                    // Handle validation errors from backend
                    if (data.detail) {
                        if (Array.isArray(data.detail)) {
                            data.detail.forEach(error => {
                                if (error.loc && error.loc.includes('email')) {
                                    document.getElementById('emailError').textContent = error.msg;
                                }
                                if (error.loc && error.loc.includes('password')) {
                                    document.getElementById('passwordError').textContent = error.msg;
                                }
                            });
                        } else {
                            // Handle single error message
                            alert(data.detail);
                        }
                    }
                    return;
                }
                
                // Registration successful
                alert('User registered successfully!');
                // Optionally redirect to login page or other page
                // window.location.href = '/login';
                
            } catch (error) {
                console.error('Error:', error);
                alert('An error occurred during registration');
            }
        });
    </script>
{% endblock %}