ChandimaPrabath commited on
Commit
695b887
·
1 Parent(s): b0d2969

session token test 1

Browse files
Files changed (2) hide show
  1. frontend/app/Auth.js +26 -3
  2. frontend/app/layout.js +10 -0
frontend/app/Auth.js CHANGED
@@ -1,4 +1,4 @@
1
- import { useState } from 'react';
2
 
3
  const Auth = ({
4
  username,
@@ -14,7 +14,8 @@ const Auth = ({
14
  isSignup,
15
  setIsSignup,
16
  ws,
17
- status
 
18
  }) => {
19
  const [usernameValid, setUsernameValid] = useState(false);
20
  const [passwordValid, setPasswordValid] = useState(false);
@@ -22,6 +23,14 @@ const Auth = ({
22
  const [showPassword, setShowPassword] = useState(false);
23
  const [showConfirmPassword, setShowConfirmPassword] = useState(false);
24
 
 
 
 
 
 
 
 
 
25
  // Handle login and signup requests
26
  const connectAndAuthenticate = (data) => {
27
  setError('');
@@ -41,6 +50,20 @@ const Auth = ({
41
  }
42
  };
43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
  // Validation for signup form
45
  const validateSignup = () => {
46
  if (!username || !password || !confirmPassword) {
@@ -279,7 +302,7 @@ const switchButtonStyle = {
279
 
280
  const buttonContainerStyle = {
281
  display: 'flex',
282
- justifyContent: 'center',
283
  };
284
 
285
  const conditionsContainerStyle = {
 
1
+ import { useState, useEffect } from 'react';
2
 
3
  const Auth = ({
4
  username,
 
14
  isSignup,
15
  setIsSignup,
16
  ws,
17
+ status,
18
+ setSessionToken, // Added to handle session token
19
  }) => {
20
  const [usernameValid, setUsernameValid] = useState(false);
21
  const [passwordValid, setPasswordValid] = useState(false);
 
23
  const [showPassword, setShowPassword] = useState(false);
24
  const [showConfirmPassword, setShowConfirmPassword] = useState(false);
25
 
26
+ useEffect(() => {
27
+ // Check for existing session token in localStorage or cookie when component loads
28
+ const token = localStorage.getItem('sessionToken');
29
+ if (token) {
30
+ setSessionToken(token);
31
+ }
32
+ }, [setSessionToken]);
33
+
34
  // Handle login and signup requests
35
  const connectAndAuthenticate = (data) => {
36
  setError('');
 
50
  }
51
  };
52
 
53
+ // Handle session token update from the server response
54
+ const handleSessionToken = (response) => {
55
+ const { token, success } = JSON.parse(response);
56
+
57
+ if (success && token) {
58
+ localStorage.setItem('sessionToken', token); // Store token in localStorage
59
+ setSessionToken(token); // Update token state
60
+ setIsLoading(false);
61
+ } else {
62
+ setError('Authentication failed');
63
+ setIsLoading(false);
64
+ }
65
+ };
66
+
67
  // Validation for signup form
68
  const validateSignup = () => {
69
  if (!username || !password || !confirmPassword) {
 
302
 
303
  const buttonContainerStyle = {
304
  display: 'flex',
305
+ justifyContent: 'center',
306
  };
307
 
308
  const conditionsContainerStyle = {
frontend/app/layout.js CHANGED
@@ -18,6 +18,15 @@ export default function RootLayout({ children }) {
18
  const [isLoggedIn, setIsLoggedIn] = useState(false);
19
  const [isSignup, setIsSignup] = useState(false);
20
 
 
 
 
 
 
 
 
 
 
21
  return (
22
  <html lang="en">
23
  <body>
@@ -93,6 +102,7 @@ function WebSocketLayout({
93
  setIsLoggedIn(true);
94
  setRecipientList(data.recipients || []);
95
  localStorage.setItem('me', username);
 
96
  } else if (data.status === 'error') {
97
  setError(data.message);
98
  setIsLoading(false);
 
18
  const [isLoggedIn, setIsLoggedIn] = useState(false);
19
  const [isSignup, setIsSignup] = useState(false);
20
 
21
+ useEffect(() => {
22
+ // Check if there's a valid session token in localStorage
23
+ const sessionToken = localStorage.getItem('sessionToken');
24
+ if (sessionToken) {
25
+ setIsLoggedIn(true);
26
+ setUsername(localStorage.getItem('me'));
27
+ }
28
+ }, []);
29
+
30
  return (
31
  <html lang="en">
32
  <body>
 
102
  setIsLoggedIn(true);
103
  setRecipientList(data.recipients || []);
104
  localStorage.setItem('me', username);
105
+ localStorage.setItem('sessionToken', data.token); // Store session token
106
  } else if (data.status === 'error') {
107
  setError(data.message);
108
  setIsLoading(false);