source: frontend/js/loginHandler.js@ 53bad7e

Last change on this file since 53bad7e was c164f8f, checked in by Kristijan <kristijanzafirovski26@…>, 6 days ago

pred-finalna

  • Property mode set to 100644
File size: 4.8 KB
Line 
1document.addEventListener('DOMContentLoaded', function() {
2 const loginForm = document.getElementById('loginForm');
3 const registerForm = document.getElementById('registerForm');
4 const loginResponse = document.getElementById('loginResponse');
5 const registerResponse = document.getElementById('registerResponse');
6
7 checkSession();
8
9 loginForm.addEventListener('submit', function(event) {
10 event.preventDefault();
11
12 const email = loginForm.email.value;
13 const password = loginForm.password.value;
14
15 const data = {
16 email: email,
17 password: password
18 };
19
20 fetch('/account/login', {
21 method: 'POST',
22 headers: {
23 'Content-Type': 'application/json'
24 },
25 body: JSON.stringify(data)
26 })
27 .then(response => response.json())
28 .then(data => {
29 if (data.sessionId) {
30 loginResponse.textContent = data.message;
31 sessionStorage.setItem('loggedIn', 'true');
32 sessionStorage.setItem('user', JSON.stringify(email));
33 sessionStorage.setItem('isAdmin', data.isAdmin);
34 document.cookie = `sessionId=${data.sessionId}; path=/`;
35 showLoggedInState();
36 closeLoginPopup();
37 } else {
38 throw new Error(data.message);
39 }
40 })
41 .catch(error => {
42 showMessage('loginResponse', 'error', error.message);
43 });
44 });
45
46 registerForm.addEventListener('submit', function(event) {
47 event.preventDefault();
48
49 const username = registerForm.username.value;
50 const email = registerForm.email.value;
51 const password = registerForm.password.value;
52
53 const data = {
54 username: username,
55 email: email,
56 password: password
57 };
58
59 fetch('/account/register', {
60 method: 'POST',
61 headers: {
62 'Content-Type': 'application/json'
63 },
64 body: JSON.stringify(data)
65 })
66 .then(response => response.json())
67 .then(data => {
68 if (response.ok) {
69 showMessage('registerResponse', 'success', data.message);
70 closeRegisterPopup();
71 } else {
72 throw new Error(data.message);
73 }
74 })
75 .catch(error => {
76 showMessage('registerResponse', 'error', error.message);
77 });
78 });
79});
80
81function checkSession() {
82 fetch('/account/session', {
83 method: 'GET',
84 credentials: 'include'
85 })
86 .then(response => response.json())
87 .then(data => {
88 if (data.loggedIn) {
89 sessionStorage.setItem('loggedIn', 'true');
90 sessionStorage.setItem('user', JSON.stringify(data.user));
91 sessionStorage.setItem('isAdmin', data.isAdmin);
92 showLoggedInState();
93 } else {
94 sessionStorage.clear();
95 showLoggedOutState();
96 }
97 })
98 .catch(error => {
99 console.error('Error checking session:', error);
100 sessionStorage.clear();
101 showLoggedOutState();
102 });
103}
104
105function logout() {
106 fetch('/account/logout', {
107 method: 'POST',
108 credentials: 'include'
109 })
110 .then(response => {
111 if (response.ok) {
112 sessionStorage.clear();
113 document.cookie = "sessionId=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
114 showLoggedOutState();
115 } else {
116 throw new Error('Logout failed');
117 }
118 })
119 .catch(error => {
120 console.error('Error logging out:', error);
121 });
122}
123
124
125function handleCredentialResponse(response) {
126 const email = JSON.parse(atob(response.credential.split('.')[1])).email;
127 fetch('/account/login', {
128 method: 'POST',
129 headers: {
130 'Content-Type': 'application/json'
131 },
132 body: JSON.stringify({ id_token: response.credential })
133 })
134 .then(response => response.json())
135 .then(data => {
136 if (data.sessionId) {
137 sessionStorage.setItem('loggedIn', 'true');
138 sessionStorage.setItem('user', JSON.stringify(email));
139 sessionStorage.setItem('isAdmin', data.isAdmin);
140 document.cookie = `sessionId=${data.sessionId}; path=/`;
141 showLoggedInState();
142 closeLoginPopup();
143 } else {
144 throw new Error(data.message);
145 }
146 })
147 .catch(error => {
148 console.error(error);
149 showMessage('loginResponse', 'error', 'Google login failed');
150 });
151}
152
153
154
Note: See TracBrowser for help on using the repository browser.