File size: 2,886 Bytes
ff6910b
573b1c9
 
 
 
 
 
 
 
 
 
 
ff6910b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
573b1c9
ff6910b
573b1c9
 
 
 
 
 
 
 
 
 
 
 
 
ff6910b
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

// Handle login form submission
document.addEventListener('DOMContentLoaded', () => {
    const loginForm = document.getElementById('login-form');
    if (loginForm) {
        loginForm.addEventListener('submit', (e) => {
            e.preventDefault();
            
            const email = document.getElementById('email').value;
            const password = document.getElementById('password').value;
            
            if (email && password) {
                const users = JSON.parse(localStorage.getItem('users') || '[]');
                const user = users.find(u => u.email === email && u.password === password);
                
                if (user) {
                    localStorage.setItem('authToken', 'dummy-token');
                    localStorage.setItem('currentUser', JSON.stringify(user));
                    window.location.href = 'gallery.html';
                } else {
                    alert('Email ou mot de passe incorrect');
                }
            } else {
                alert('Veuillez remplir tous les champs');
            }
        });
    }
    
    // Handle registration form
    const registerForm = document.getElementById('register-form');
    if (registerForm) {
        registerForm.addEventListener('submit', (e) => {
            e.preventDefault();
            
            const firstname = document.getElementById('firstname').value;
            const lastname = document.getElementById('lastname').value;
            const email = document.getElementById('email').value;
            const password = document.getElementById('password').value;
            
            if (firstname && lastname && email && password) {
                const users = JSON.parse(localStorage.getItem('users') || '[]');
                
                // Check if email already exists
                if (users.some(u => u.email === email)) {
                    alert('Cet email est déjà utilisé');
                    return;
                }
                
                const newUser = {
                    id: Date.now(),
                    firstname,
                    lastname,
                    email,
                    password
                };
                
                users.push(newUser);
                localStorage.setItem('users', JSON.stringify(users));
                localStorage.setItem('authToken', 'dummy-token');
                localStorage.setItem('currentUser', JSON.stringify(newUser));
                window.location.href = 'gallery.html';
            } else {
                alert('Veuillez remplir tous les champs');
            }
        });
    }
    
    // Check authentication on gallery page
    if (window.location.pathname.includes('gallery.html')) {
        if (!localStorage.getItem('authToken')) {
            window.location.href = 'index.html';
        }
    }
});