Mohit0199 commited on
Commit
cbde832
Β·
verified Β·
1 Parent(s): 884299a

when i am registering or login it is giving error

Browse files

Unexpected token '<', "<!DOCTYPE "... is not valid JSON
check this out and solve it properly

Files changed (2) hide show
  1. auth-api.js +16 -0
  2. index.html +33 -15
auth-api.js CHANGED
@@ -103,6 +103,22 @@ router.post('/login', [
103
  res.status(500).json({ message: 'Server error' });
104
  }
105
  });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
106
 
107
  // Protected route example
108
  router.get('/profile', async (req, res) => {
 
103
  res.status(500).json({ message: 'Server error' });
104
  }
105
  });
106
+ // Token verification endpoint
107
+ router.get('/verify', async (req, res) => {
108
+ try {
109
+ // Get token from header
110
+ const token = req.header('Authorization')?.replace('Bearer ', '');
111
+ if (!token) {
112
+ return res.json({ valid: false });
113
+ }
114
+
115
+ // Verify token
116
+ jwt.verify(token, JWT_SECRET);
117
+ res.json({ valid: true });
118
+ } catch (error) {
119
+ res.json({ valid: false });
120
+ }
121
+ });
122
 
123
  // Protected route example
124
  router.get('/profile', async (req, res) => {
index.html CHANGED
@@ -293,10 +293,9 @@ const settingsBtn = document.getElementById('settingsBtn');
293
  authError.classList.add('hidden');
294
  authError.textContent = '';
295
  }
296
-
297
  async function loginUser(email, password, username = null) {
298
  try {
299
- const response = await fetch('/api/auth/login', {
300
  method: 'POST',
301
  headers: {
302
  'Content-Type': 'application/json'
@@ -308,12 +307,20 @@ const settingsBtn = document.getElementById('settingsBtn');
308
  })
309
  });
310
 
311
- const data = await response.json();
312
-
313
  if (!response.ok) {
314
- throw new Error(data.message || 'Login failed');
 
 
 
 
 
 
 
 
315
  }
316
 
 
 
317
  // Store user session
318
  localStorage.setItem('chatRouterUser', data.user.username);
319
  localStorage.setItem('chatRouterToken', data.token);
@@ -326,12 +333,11 @@ const settingsBtn = document.getElementById('settingsBtn');
326
  authModal.classList.add('hidden');
327
 
328
  } catch (error) {
329
- authError.textContent = error.message;
330
  authError.classList.remove('hidden');
331
  }
332
  }
333
-
334
- async function registerUser(username, email, password) {
335
  try {
336
  const response = await fetch('/api/auth/register', {
337
  method: 'POST',
@@ -375,17 +381,29 @@ const settingsBtn = document.getElementById('settingsBtn');
375
  updateAuthUI();
376
  addMessage('system', 'Please log in to start chatting.');
377
  }
378
-
379
- function checkAuthStatus() {
380
  const token = localStorage.getItem('chatRouterToken');
381
  if (!token) return false;
382
 
383
- // Here you would verify the token with the backend
384
- // For now we'll just check if it exists
385
- return true;
386
- }
 
 
 
387
 
388
- // Chat management
 
 
 
 
 
 
 
 
 
 
389
  let currentChatId = null;
390
 
391
  function generateChatId() {
 
293
  authError.classList.add('hidden');
294
  authError.textContent = '';
295
  }
 
296
  async function loginUser(email, password, username = null) {
297
  try {
298
+ const response = await fetch('https://chatrouter-api.example.com/api/auth/login', {
299
  method: 'POST',
300
  headers: {
301
  'Content-Type': 'application/json'
 
307
  })
308
  });
309
 
 
 
310
  if (!response.ok) {
311
+ const errorText = await response.text();
312
+ try {
313
+ // Try parsing as JSON first
314
+ const errorData = JSON.parse(errorText);
315
+ throw new Error(errorData.message || 'Login failed');
316
+ } catch (e) {
317
+ // If not JSON, show raw error
318
+ throw new Error(errorText || 'Login failed');
319
+ }
320
  }
321
 
322
+ const data = await response.json();
323
+
324
  // Store user session
325
  localStorage.setItem('chatRouterUser', data.user.username);
326
  localStorage.setItem('chatRouterToken', data.token);
 
333
  authModal.classList.add('hidden');
334
 
335
  } catch (error) {
336
+ authError.textContent = error.message.replace('Error: ', '').replace('error: ', '');
337
  authError.classList.remove('hidden');
338
  }
339
  }
340
+ async function registerUser(username, email, password) {
 
341
  try {
342
  const response = await fetch('/api/auth/register', {
343
  method: 'POST',
 
381
  updateAuthUI();
382
  addMessage('system', 'Please log in to start chatting.');
383
  }
384
+ async function checkAuthStatus() {
 
385
  const token = localStorage.getItem('chatRouterToken');
386
  if (!token) return false;
387
 
388
+ try {
389
+ const response = await fetch('https://chatrouter-api.example.com/api/auth/verify', {
390
+ method: 'GET',
391
+ headers: {
392
+ 'Authorization': `Bearer ${token}`
393
+ }
394
+ });
395
 
396
+ if (response.ok) {
397
+ const data = await response.json();
398
+ return data.valid;
399
+ }
400
+ return false;
401
+ } catch (error) {
402
+ console.error('Auth verification failed:', error);
403
+ return false;
404
+ }
405
+ }
406
+ // Chat management
407
  let currentChatId = null;
408
 
409
  function generateChatId() {