Spaces:
Sleeping
Sleeping
File size: 13,170 Bytes
7afbbc5 5649a75 92f01f5 b780a2e 7afbbc5 5649a75 7afbbc5 5649a75 2ea6bf5 5649a75 2ea6bf5 5649a75 2ea6bf5 5649a75 2ea6bf5 5649a75 2ea6bf5 5649a75 7afbbc5 5649a75 7afbbc5 5649a75 2ea6bf5 5649a75 7afbbc5 5649a75 2ea6bf5 5649a75 7afbbc5 5649a75 7afbbc5 5649a75 7afbbc5 5649a75 7afbbc5 5649a75 7afbbc5 5649a75 7afbbc5 5649a75 7afbbc5 3acdbcc 5649a75 3acdbcc 5649a75 b780a2e 5649a75 b780a2e 5649a75 b780a2e 5649a75 b780a2e 5649a75 b780a2e 5649a75 b780a2e 5649a75 b780a2e 5649a75 7afbbc5 5649a75 b780a2e 5649a75 b780a2e 5649a75 b780a2e 5649a75 7afbbc5 5649a75 b780a2e 5649a75 b780a2e 5649a75 b780a2e 5649a75 b780a2e 5649a75 7afbbc5 5649a75 7afbbc5 5649a75 7afbbc5 5649a75 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 |
'use client';
import './NexusAuth.css';
import { useState, useEffect } from 'react';
import NexusAuthApi from '@lib/Nexus_Auth_API';
import SplashScreen from '@components/SplashScreen';
import { useToast } from '@lib/ToastContext';
import { CheckCircleIcon } from '@heroicons/react/20/solid';
const SignupForm = ({ onSignup }) => {
const [username, setUsername] = useState('');
const [password, setPassword] = useState('');
const [confirmPassword, setConfirmPassword] = useState('');
const [email, setEmail] = useState('');
const [usernameAvailable, setUsernameAvailable] = useState(null); // null for initial state
const [doesUsernameContainInvalidChars, setDoesUsernameContainInvalidChars] = useState(false);
const [doesUsernameExceedMinLength, setDoesUsernameExceedMinLength] = useState(false);
const [passwordValid, setPasswordValid] = useState(false); // Initially invalid
const [formValid, setFormValid] = useState(false);
const [debounceTimeout, setDebounceTimeout] = useState(null); // Store timeout ID
const minUsernameLength = 3;
const validatePassword = (password) => {
return password.length >= 8;
};
const handleUsernameChange = (e) => {
const newUsername = e.target.value;
setUsername(newUsername);
// Reset username availability while typing
setUsernameAvailable(null);
// Clear any existing debounce timeout
if (debounceTimeout) {
clearTimeout(debounceTimeout);
}
// Check for invalid characters
const invalidChars = /[^a-zA-Z0-9_]/g;
if (invalidChars.test(newUsername)) {
setDoesUsernameContainInvalidChars(true);
setTimeout(() => {
setDoesUsernameContainInvalidChars(false);
}, 2000); // Show error for 2 seconds
}
// Basic sanitization to prevent SQL injection
const sanitizedUsername = newUsername.replace(invalidChars, '');
if (sanitizedUsername.length < minUsernameLength) {
setDoesUsernameExceedMinLength(false);
return;
} else {
setDoesUsernameExceedMinLength(true);}
if (sanitizedUsername.trim().length > 0) {
// Set a new timeout to check availability
const newTimeout = setTimeout(async () => {
try {
const response = await NexusAuthApi.isUsernameAvailable(sanitizedUsername);
setUsernameAvailable(response?.is_available === true);
} catch (error) {
console.error('Error checking username availability:', error);
setUsernameAvailable(null); // Fallback state
}
}, 1000); // 1-second debounce delay
setDebounceTimeout(newTimeout);
} else {
setUsernameAvailable(null); // Reset availability check when input is empty
}
// Set sanitized username
setUsername(sanitizedUsername);
};
const handlePasswordChange = (e) => {
const newPassword = e.target.value;
setPassword(newPassword);
setPasswordValid(validatePassword(newPassword));
};
const handleConfirmPasswordChange = (e) => {
setConfirmPassword(e.target.value);
};
const handleSubmit = (e) => {
e.preventDefault();
// Set email to null if it's empty
const emailValue = email.trim() === '' ? null : email;
if (password === confirmPassword && passwordValid) {
onSignup({ username, password, email: emailValue });
}
};
useEffect(() => {
setFormValid(
usernameAvailable === true &&
password === confirmPassword &&
passwordValid &&
username.length >= minUsernameLength &&
!doesUsernameContainInvalidChars
);
}, [username, password, confirmPassword, usernameAvailable, passwordValid]);
return (
<form onSubmit={handleSubmit} className="nexus-auth-form">
<h2>Signup</h2>
<div className="form-group">
<label>Username:</label>
<input
type="text"
value={username}
onChange={handleUsernameChange}
required
className={usernameAvailable === false ? 'error' : ''} />
{usernameAvailable === true && username.length > 0 && (
<CheckCircleIcon className="h-5 w-5 text-green-500" />
)}
{doesUsernameExceedMinLength === false && (
<p className="error-message text-red-500">Username must have more than {minUsernameLength} characters.</p>
)}
{doesUsernameContainInvalidChars === true && (
<p className="error-message text-red-500">Username cannot contain invalid characters.</p>
)}
{usernameAvailable === false && (
<p className="error-message text-red-500">Username is already taken</p>
)}
{usernameAvailable === null && username.length > 0 && (
<p className="typing-message text-green-500">Checking username availability...</p>
)}
</div>
<div className="form-group">
<label>Password:</label>
<input
type="password"
value={password}
onChange={handlePasswordChange}
required
className={passwordValid ? '' : 'error'} />
{passwordValid && (
<CheckCircleIcon className="h-5 w-5 text-green-500" />
)}
{!passwordValid && (
<p className="error-message text-yellow-500">Password must be at least 8 characters long</p>
)}
</div>
<div className="form-group">
<label>Confirm Password:</label>
<input
type="password"
value={confirmPassword}
onChange={handleConfirmPasswordChange}
required
className={password === confirmPassword ? '' : 'error'} />
{password === confirmPassword && confirmPassword.length > 0 && (
<CheckCircleIcon className="h-5 w-5 text-green-500" />
)}
{password !== confirmPassword && confirmPassword.length > 0 && (
<p className="error-message text-red-500">Passwords do not match</p>
)}
</div>
<div className="form-group">
<label>Email (optional):</label>
<input
type="email"
value={email}
onChange={(e) => setEmail(e.target.value)} />
</div>
<button type="submit" className="submit-button" disabled={!formValid}>
Signup
</button>
</form>
);
};
const LoginForm = ({ onLogin }) => {
const [username, setUsername] = useState('');
const [password, setPassword] = useState('');
const handleSubmit = (e) => {
e.preventDefault();
onLogin({ username, password });
};
return (
<form onSubmit={handleSubmit} className="nexus-auth-form">
<h2>Login</h2>
<div className="form-group">
<label>Username:</label>
<input
type="text"
value={username}
onChange={(e) => setUsername(e.target.value)}
required />
</div>
<div className="form-group">
<label>Password:</label>
<input
type="password"
value={password}
onChange={(e) => setPassword(e.target.value)}
required />
</div>
<button type="submit" className="submit-button">
Login
</button>
</form>
);
};
export const NexusAuthWrapper = ({ children }) => {
const [isLoggedIn, setIsLoggedIn] = useState(false);
const [isSignup, setIsSignup] = useState(false);
const [isLoading, setIsLoading] = useState(true);
const toast = useToast();
useEffect(() => {
const validateUserSession = async () => {
const storedUsername = localStorage.getItem("me");
const storedToken = localStorage.getItem("s_tkn");
const storedUserID = localStorage.getItem("u_id");
if (storedUsername && storedToken && storedUserID) {
try {
// Validate the token with the NexusAuthApi
const response = await NexusAuthApi.validateToken(storedUserID, storedToken);
if (response.data && response.data.user_id) {
// Token is valid; response contains user details
console.log("User is already logged in.");
toast.info("Welcome back, " + response.data.username + "!");
setIsLoggedIn(true);
// Optionally, update localStorage with new details if needed
localStorage.setItem("me", response.data.username);
localStorage.setItem("s_tkn", response.data.access_token);
localStorage.setItem("u_id", response.data.user_id);
localStorage.setItem("a_l", response.data.access_level);
} else if (response.status === 401) {
// Token is invalid; clear local storage
console.error("Token validation failed with status 401:", response.data);
clearLocalStorage();
} else {
// Handle other errors (e.g., network issues)
console.error("Token validation failed due to an unexpected error:", response.data);
toast.error("Unable to validate token. Please check your connection.");
}
} catch (error) {
// Handle other errors (e.g., network issues)
console.error("Token validation failed due to an unexpected error:", error);
toast.error("Unable to validate token. Please check your connection.");
}
}
setIsLoading(false);
};
const clearLocalStorage = () => {
localStorage.removeItem("me");
localStorage.removeItem("s_tkn");
localStorage.removeItem("u_id");
localStorage.removeItem("a_l");
setIsLoggedIn(false);
toast.error("Session expired. Please login again.");
};
validateUserSession();
}, []);
const handleSignup = async (data) => {
setIsLoading(true);
try {
const response = await NexusAuthApi.signup(data.username, data.password, data.email);
console.log("Signup successful:", response);
setIsLoading(false);
toast.success('Signup successful. Please login to continue');
setIsSignup(false);
} catch (error) {
setIsLoading(false);
console.error("Signup failed:", error);
toast.error("Signup failed");
}
};
const handleLogin = async (data) => {
setIsLoading(true);
try {
const response = await NexusAuthApi.login(data.username, data.password);
console.log("Login successful:", response);
toast.success('Login successful.');
// Save username and token to localStorage
localStorage.setItem("me", response.username);
localStorage.setItem("s_tkn", response.access_token);
localStorage.setItem("u_id", response.user_id);
localStorage.setItem("a_l", response.access_level);
setIsLoggedIn(true);
setIsLoading(false);
} catch (error) {
setIsLoading(false);
console.error("Login failed:", error);
toast.error("Login failed");
}
};
if (isLoading) {
return <SplashScreen />;
}
return (
<div>
{isLoggedIn ? (
children
) : (
<div className="nexus-auth-signup-login">
<h1>Nexus Accounts</h1>
<button onClick={() => setIsSignup(!isSignup)}>
{isSignup ? "Already have an Account? Login" : "Don't have an Account? Signup"}
</button>
{isSignup ? (
<SignupForm onSignup={handleSignup} />
) : (
<LoginForm onLogin={handleLogin} />
)}
</div>
)}
</div>
);
};
|