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