frontend: create user auth
This commit is contained in:
152
frontend/src/components/InitialSetup.tsx
Normal file
152
frontend/src/components/InitialSetup.tsx
Normal file
@@ -0,0 +1,152 @@
|
||||
import { useState, useEffect } from 'react';
|
||||
import { useCookies } from 'react-cookie';
|
||||
import { GUESTS } from '../constants/constants';
|
||||
import useFetchUser from '../utils/fetchUser'; // Import your custom hook
|
||||
import { useNotification } from '../NotificationContext';
|
||||
|
||||
const InitialSetup = () => {
|
||||
const [cookie, setCookie] = useCookies();
|
||||
const [selectedName, setSelectedName] = useState<string | undefined>(cookie.userName);
|
||||
const [token, setToken] = useState<string | undefined>(cookie.token)
|
||||
const [isSubmitted, setIsSubmitted] = useState(false);
|
||||
const [password, setPassword] = useState('');
|
||||
const [isPasswordSet, setIsPasswordSet] = useState(false); // To track if password is set
|
||||
const [isSignIn, setIsSignIn] = useState(false); // To track if it should prompt for sign-in
|
||||
|
||||
const { userSet, passwordCreate, signUser, validToken } = useFetchUser(); // Destructure functions from the hook
|
||||
const notify = useNotification();
|
||||
|
||||
useEffect(() => {
|
||||
document.body.style.overflow = 'hidden';
|
||||
|
||||
return () => {
|
||||
document.body.style.overflow = 'unset';
|
||||
};
|
||||
}, []);
|
||||
|
||||
const handleSelect = (event: React.ChangeEvent<HTMLSelectElement>) => {
|
||||
const name = event.target.value;
|
||||
setSelectedName(name);
|
||||
checkUserPassword(name);
|
||||
};
|
||||
|
||||
const checkUserPassword = async (name: string) => {
|
||||
const passwordStatus = await userSet(name);
|
||||
setIsPasswordSet(passwordStatus);
|
||||
setIsSignIn(!passwordStatus); // If password is not set, show sign-up
|
||||
};
|
||||
|
||||
const handlePasswordCreate = async () => {
|
||||
const message= await passwordCreate(selectedName!, password);
|
||||
if (message !== '') {
|
||||
notify(message, 'error')
|
||||
return
|
||||
}
|
||||
setIsSubmitted(true);
|
||||
};
|
||||
|
||||
const handleSignIn = async () => {
|
||||
const signedIn = await signUser(selectedName!, password); // Implement your sign-in logic here
|
||||
if (!signedIn) {
|
||||
notify('Не удалось войти. Может пароль не тот?', 'error')
|
||||
return
|
||||
}
|
||||
setIsSubmitted(true);
|
||||
};
|
||||
|
||||
if (isSubmitted) {
|
||||
console.log('Selected', selectedName);
|
||||
return null; // or you can redirect to another component or page
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
const validateToken = async () => {
|
||||
const isTokenValid = await validToken(token);
|
||||
setIsSubmitted(isTokenValid);
|
||||
};
|
||||
|
||||
validateToken();
|
||||
}, [token]);
|
||||
|
||||
return (
|
||||
<div style={styles.container}>
|
||||
<h2 style={styles.title}>Выбери себя</h2>
|
||||
<select style={styles.dropdown} onChange={handleSelect} value={selectedName || ''}>
|
||||
<option value="" disabled>Select your name</option>
|
||||
{GUESTS.map((name) => (
|
||||
<option key={name} value={name}>
|
||||
{name}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
{(selectedName !== undefined) && (
|
||||
<>
|
||||
<h3>{isPasswordSet ? 'Войдите' : 'Создайте свой пароль'}</h3>
|
||||
<input
|
||||
type="password"
|
||||
placeholder="Ввести пароль"
|
||||
value={password}
|
||||
onChange={(e) => setPassword(e.target.value)}
|
||||
style={styles.input}
|
||||
/>
|
||||
{isPasswordSet ? (
|
||||
<>
|
||||
<button onClick={handleSignIn}>
|
||||
Войти
|
||||
</button>
|
||||
</>
|
||||
) : (<>
|
||||
<button
|
||||
onClick={handlePasswordCreate}
|
||||
disabled={!password} // Disables the button if no password is entered
|
||||
>
|
||||
Создать пароль
|
||||
</button>
|
||||
</>)}
|
||||
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
const styles = {
|
||||
container: {
|
||||
position: 'fixed' as 'fixed',
|
||||
top: 0,
|
||||
left: 0,
|
||||
width: '100vw',
|
||||
height: '100vh',
|
||||
backgroundColor: 'rgba(0, 0, 0, 1)',
|
||||
display: 'flex',
|
||||
flexDirection: 'column' as 'column',
|
||||
justifyContent: 'center',
|
||||
alignItems: 'center',
|
||||
color: '#fff',
|
||||
zIndex: 1000,
|
||||
overflow: 'hidden',
|
||||
},
|
||||
title: {
|
||||
marginBottom: '20px',
|
||||
},
|
||||
dropdown: {
|
||||
padding: '10px',
|
||||
fontSize: '16px',
|
||||
cursor: 'pointer',
|
||||
border: 'none',
|
||||
borderRadius: '5px',
|
||||
outline: 'none',
|
||||
marginBottom: '10px',
|
||||
},
|
||||
input: {
|
||||
padding: '10px',
|
||||
fontSize: '16px',
|
||||
border: 'none',
|
||||
borderRadius: '5px',
|
||||
outline: 'none',
|
||||
marginBottom: '10px',
|
||||
width: '200px', // Set a width
|
||||
}
|
||||
}
|
||||
|
||||
export default InitialSetup;
|
||||
Reference in New Issue
Block a user