1 | import { verificationCheck } from "./authentication-shared.js";
|
---|
2 | function toggleForm(formId, show) {
|
---|
3 | const form = document.getElementById(formId);
|
---|
4 | form.style.display = show ? 'flex' : 'none';
|
---|
5 | document.body.style.overflow = show ? 'hidden' : 'auto';
|
---|
6 | }
|
---|
7 |
|
---|
8 | function updateUIBasedOnRole(role) {
|
---|
9 | if (role === 'ADMIN') {
|
---|
10 | window.location.href = 'admin.html';
|
---|
11 |
|
---|
12 | } else if (role === 'USER') {
|
---|
13 | document.getElementById('adminSection').style.display = 'none';
|
---|
14 | document.getElementById('userSection').style.display = 'block';
|
---|
15 | }
|
---|
16 |
|
---|
17 | }
|
---|
18 |
|
---|
19 | function setupFormHandlers(buttonId, formId, closeBtnId) {
|
---|
20 | document.getElementById(buttonId).addEventListener('click', function() {
|
---|
21 | toggleForm(formId, true);
|
---|
22 | });
|
---|
23 |
|
---|
24 | document.getElementById(closeBtnId).addEventListener('click', function() {
|
---|
25 | toggleForm(formId, false);
|
---|
26 | });
|
---|
27 | window.addEventListener('click', function(event) {
|
---|
28 | if (event.target === document.getElementById(formId)) {
|
---|
29 | toggleForm(formId, false);
|
---|
30 | }
|
---|
31 | });
|
---|
32 | }
|
---|
33 |
|
---|
34 | document.getElementById('signInForm').addEventListener('submit', async function (event) {
|
---|
35 | event.preventDefault();
|
---|
36 | const formData = new FormData(event.target);
|
---|
37 |
|
---|
38 | const data = {};
|
---|
39 | formData.forEach((value, key) => {
|
---|
40 | data[key] = value;
|
---|
41 | });
|
---|
42 |
|
---|
43 | let res = await verificationCheck(data);
|
---|
44 | if (!res) {
|
---|
45 | return;
|
---|
46 | }
|
---|
47 | console.log(data);
|
---|
48 | fetch('/api/users/signIn', {
|
---|
49 | method: 'POST',
|
---|
50 | headers: {
|
---|
51 | 'Content-Type': 'application/json',
|
---|
52 | },
|
---|
53 | body: JSON.stringify(data),
|
---|
54 | })
|
---|
55 | .then(response => {
|
---|
56 | if (!response.ok) {
|
---|
57 | return response.json().then(errorData => {
|
---|
58 | throw new Error(errorData.error || 'Unknown error');
|
---|
59 | });
|
---|
60 | }
|
---|
61 | else{
|
---|
62 | alert("Sign in successfull! Now log in!")
|
---|
63 | }
|
---|
64 | return response.json();
|
---|
65 | })
|
---|
66 | .then(data => {
|
---|
67 | console.log(data.message);
|
---|
68 | })
|
---|
69 | .catch(error => {
|
---|
70 | console.error('Error:', error);
|
---|
71 | });
|
---|
72 | });
|
---|
73 |
|
---|
74 | document.getElementById('loginForm').addEventListener('submit', function(event) {
|
---|
75 | event.preventDefault();
|
---|
76 | const formData = new FormData(event.target);
|
---|
77 | const data = {};
|
---|
78 | formData.forEach((value, key) => {
|
---|
79 | data[key] = value;
|
---|
80 | });
|
---|
81 |
|
---|
82 | fetch('/api/auth/login', {
|
---|
83 | method: 'POST',
|
---|
84 | headers: {
|
---|
85 | 'Content-Type': 'application/json',
|
---|
86 | },
|
---|
87 | body: JSON.stringify(data),
|
---|
88 | })
|
---|
89 | .then(response => {
|
---|
90 | if (!response.ok) {
|
---|
91 | return response.json().then(errorData => {
|
---|
92 | alert(errorData.message || 'Unknown error');
|
---|
93 | throw new Error(errorData.message || 'Unknown error');
|
---|
94 | });
|
---|
95 | }
|
---|
96 | return response.json();
|
---|
97 | })
|
---|
98 | .then(data => {
|
---|
99 | console.log('Parsed data:', data);
|
---|
100 | if (data.success) {
|
---|
101 | updateUIBasedOnRole(data.userRole);
|
---|
102 | }
|
---|
103 | })
|
---|
104 | .catch(error => {
|
---|
105 | console.error('Error:', error);
|
---|
106 | })
|
---|
107 | .finally(() => {
|
---|
108 | toggleForm('loginForm', false);
|
---|
109 | });
|
---|
110 | });
|
---|
111 |
|
---|
112 | setupFormHandlers('loginBtn', 'loginForm', 'closeBtn');
|
---|
113 | setupFormHandlers('signInBtn', 'signInForm', 'closeSignInBtn');
|
---|