Spaces:
Running
Running
File size: 21,149 Bytes
09801ca | 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 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 | /**
* Update Password Page - For users who clicked the reset link from email
* This page is shown after clicking the password reset link
* Supports both native auth flow and custom token-based flow
*/
import { useState, useEffect } from 'react';
import { Link, useNavigate, useSearchParams } from 'react-router-dom';
import { auth } from '../lib/auth-client';
import { motion } from 'framer-motion';
import { useUserStore } from '../store/userStore';
import { Sun, Moon, Lock, CheckCircle, AlertTriangle, Eye, EyeOff } from 'lucide-react';
import { api } from '../services/api';
export default function UpdatePassword() {
const { isDark, toggleTheme } = useUserStore();
const navigate = useNavigate();
const [searchParams] = useSearchParams();
const [password, setPassword] = useState('');
const [confirmPassword, setConfirmPassword] = useState('');
const [loading, setLoading] = useState(false);
const [error, setError] = useState('');
const [success, setSuccess] = useState(false);
const [showPassword, setShowPassword] = useState(false);
const [showConfirmPassword, setShowConfirmPassword] = useState(false);
const [validSession, setValidSession] = useState<boolean | null>(null);
// Custom token-based flow parameters - read directly from URL
const customToken = searchParams.get('token');
const customEmail = searchParams.get('email');
const isCustomFlow = !!(customToken && customEmail);
// Debug logging
useEffect(() => {
console.log('UpdatePassword: URL params check', {
token: customToken ? 'present' : 'missing',
email: customEmail,
isCustomFlow,
fullUrl: window.location.href
});
}, [customToken, customEmail, isCustomFlow]);
// Check if user has a valid recovery session
useEffect(() => {
const checkSession = async () => {
console.log('UpdatePassword: Checking session, isCustomFlow:', isCustomFlow);
// If we have custom token and email from our custom flow, it's valid
if (customToken && customEmail) {
console.log('UpdatePassword: Custom flow detected, setting validSession to true');
setValidSession(true);
return;
}
try {
const { data: { session }, error } = await auth.getSession();
if (error) {
console.error('Session error:', error);
setValidSession(false);
return;
}
// Check if this is a recovery session (from email link)
if (session?.user) {
setValidSession(true);
} else {
// Try to get session from URL hash (recovery flow)
const hashParams = new URLSearchParams(window.location.hash.substring(1));
const accessToken = hashParams.get('access_token');
const type = hashParams.get('type');
if (accessToken && type === 'recovery') {
// Set session from recovery token
const { error: setSessionError } = await auth.setSession({
access_token: accessToken,
refresh_token: hashParams.get('refresh_token') || ''
});
if (setSessionError) {
console.error('Set session error:', setSessionError);
setValidSession(false);
} else {
setValidSession(true);
}
} else {
setValidSession(false);
}
}
} catch (err) {
console.error('Check session error:', err);
setValidSession(false);
}
};
checkSession();
// Listen for auth state changes (handles recovery flow)
const { data: { subscription } } = auth.onAuthStateChange((event, session) => {
if (event === 'PASSWORD_RECOVERY') {
setValidSession(true);
}
});
return () => subscription.unsubscribe();
}, [customToken, customEmail]);
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
setError('');
// Validation
if (password.length < 6) {
setError('Password must be at least 6 characters long');
return;
}
if (password !== confirmPassword) {
setError('Passwords do not match');
return;
}
setLoading(true);
try {
if (isCustomFlow) {
// Use our custom backend endpoint for token-based password reset
const response = await api.post('/api/v1/settings/auth/update-password-with-token', {
token: customToken,
email: customEmail,
new_password: password
});
if (response.data.success) {
setSuccess(true);
// Redirect to login after 3 seconds
setTimeout(() => {
navigate('/login');
}, 3000);
} else {
setError(response.data.message || 'Failed to update password');
}
} else {
// Use native auth flow
const { error } = await auth.updateUser({
password: password
});
if (error) {
setError(error.message);
} else {
setSuccess(true);
// Redirect to login after 3 seconds
setTimeout(() => {
navigate('/login');
}, 3000);
}
}
} catch (err: any) {
setError(err.response?.data?.detail || err.message || 'An error occurred');
} finally {
setLoading(false);
}
};
const renderBackground = () => (
<>
{/* Background Orbs */}
<div className="absolute top-0 left-0 w-full h-full overflow-hidden pointer-events-none">
<div className="absolute top-[-10%] right-[-5%] w-[500px] h-[500px] rounded-full blur-[100px] opacity-20 bg-emerald-500/20" />
<div className="absolute bottom-[-10%] left-[-10%] w-[600px] h-[600px] rounded-full blur-[120px] opacity-10 bg-green-500/20" />
</div>
{/* Theme Toggle */}
<button
onClick={toggleTheme}
className="absolute top-6 right-6 p-3 rounded-full shadow-lg transition-transform hover:scale-110 active:scale-95 z-50 glass-card border border-border"
aria-label="Toggle theme"
>
{isDark ? (
<Sun className="w-6 h-6 text-amber-400" />
) : (
<Moon className="w-6 h-6 text-slate-500" />
)}
</button>
</>
);
// Success state
if (success) {
return (
<div className="min-h-screen flex items-center justify-center p-4 transition-colors duration-300 relative overflow-hidden bg-primary text-primary">
{renderBackground()}
<motion.div
initial={{ opacity: 0, scale: 0.95 }}
animate={{ opacity: 1, scale: 1 }}
className="max-w-md w-full rounded-2xl p-8 text-center glass-card shadow-xl backdrop-blur-xl border border-white/10 relative z-10"
>
<div className="w-20 h-20 bg-emerald-500/10 rounded-full flex items-center justify-center mx-auto mb-6 border border-emerald-500/20">
<CheckCircle className="w-10 h-10 text-emerald-500" />
</div>
<h2 className="text-2xl font-bold mb-3" style={{ color: 'var(--text-primary)' }}>
Password Updated!
</h2>
<p className="mb-8" style={{ color: 'var(--text-muted)' }}>
Your password has been successfully reset. You will be redirected to login shortly.
</p>
<Link
to="/login"
className="inline-block px-8 py-3 font-semibold rounded-lg transition-all hover:opacity-90 shadow-lg shadow-emerald-500/20"
style={{
background: 'linear-gradient(135deg, #22c55e 0%, #16a34a 100%)',
color: '#FFFFFF'
}}
>
Go to Login
</Link>
</motion.div>
</div>
);
}
// Invalid/expired link state
if (validSession === false) {
return (
<div className="min-h-screen flex items-center justify-center p-4 transition-colors duration-300 relative overflow-hidden bg-primary text-primary">
{renderBackground()}
<motion.div
initial={{ opacity: 0, scale: 0.95 }}
animate={{ opacity: 1, scale: 1 }}
className="max-w-md w-full rounded-2xl p-8 text-center glass-card shadow-xl backdrop-blur-xl border border-white/10 relative z-10"
>
<div className="w-20 h-20 bg-amber-500/10 rounded-full flex items-center justify-center mx-auto mb-6 border border-amber-500/20">
<AlertTriangle className="w-10 h-10 text-amber-500" />
</div>
<h2 className="text-2xl font-bold mb-3" style={{ color: 'var(--text-primary)' }}>
Invalid or Expired Link
</h2>
<p className="mb-8" style={{ color: 'var(--text-muted)' }}>
This password reset link is invalid or has expired. Please request a new one.
</p>
<div className="flex flex-col gap-3">
<Link
to="/forgot-password"
className="inline-block px-8 py-3 font-semibold rounded-lg transition-all hover:opacity-90 shadow-lg shadow-emerald-500/20"
style={{
background: 'linear-gradient(135deg, #22c55e 0%, #16a34a 100%)',
color: '#FFFFFF'
}}
>
Request New Link
</Link>
<Link
to="/login"
className="text-sm transition-colors hover:opacity-100 opacity-80 font-medium"
style={{ color: '#22c55e' }}
>
← Back to Login
</Link>
</div>
</motion.div>
</div>
);
}
// Loading/checking session
if (validSession === null) {
return (
<div className="min-h-screen flex items-center justify-center p-4 transition-colors duration-300 relative overflow-hidden bg-primary text-primary">
{renderBackground()}
<div className="text-center">
<div className="w-12 h-12 border-4 border-emerald-500/30 border-t-emerald-500 rounded-full animate-spin mx-auto mb-4" />
<p style={{ color: 'var(--text-muted)' }}>Verifying reset link...</p>
</div>
</div>
);
}
// Main reset form
return (
<div className="min-h-screen flex items-center justify-center p-4 transition-colors duration-300 relative overflow-hidden bg-primary text-primary">
{renderBackground()}
<motion.div
initial={{ opacity: 0, y: 20 }}
animate={{ opacity: 1, y: 0 }}
className="max-w-md w-full relative z-10"
>
{/* Logo/Brand */}
<div className="text-center mb-8">
<Link to="/" className="inline-flex items-center gap-3 group">
<div className="relative">
<div className="absolute inset-0 rounded-full blur-lg opacity-50 group-hover:opacity-100 transition-opacity bg-green-500/30" />
<img src={isDark ? '/datavision-logo-dark.jpg' : '/datavision-logo-light.jpg'} alt="DataVision Logo" className="w-14 h-14 object-contain relative z-10 drop-shadow-lg rounded-full" onError={(e) => { (e.target as HTMLImageElement).src = '/logo.svg'; }} />
</div>
<span className="text-3xl font-bold tracking-tight">
<span style={{ color: 'var(--text-primary)' }}>Data</span>
<span className="text-emerald-500">Vision</span>
</span>
</Link>
<p className="mt-2 text-sm" style={{ color: 'var(--text-muted)' }}>
Create a new password
</p>
</div>
{/* Card */}
<motion.div
initial={{ opacity: 0, scale: 0.95 }}
animate={{ opacity: 1, scale: 1 }}
transition={{ delay: 0.1 }}
className="p-8 md:p-10 rounded-3xl shadow-2xl glass-card backdrop-blur-xl border border-white/10"
>
<div className="w-16 h-16 bg-emerald-500/10 rounded-full flex items-center justify-center mx-auto mb-6 border border-emerald-500/20">
<Lock className="w-8 h-8 text-emerald-500" />
</div>
<h2 className="text-2xl font-bold text-center mb-2" style={{ color: 'var(--text-primary)' }}>
Set New Password
</h2>
<p className="text-center mb-8" style={{ color: 'var(--text-muted)' }}>
Enter your new password below
</p>
{/* Error Message */}
{error && (
<div className="mb-6 p-4 rounded-xl flex items-start gap-3 bg-red-500/10 border border-red-500/20">
<AlertTriangle className="w-5 h-5 text-red-500 flex-shrink-0 mt-0.5" />
<p className="text-sm text-red-500">{error}</p>
</div>
)}
<form onSubmit={handleSubmit}>
<div className="space-y-4">
<div>
<label htmlFor="password" className="block text-sm font-medium mb-2" style={{ color: 'var(--text-muted)' }}>
New Password
</label>
<div className="relative">
<input
id="password"
type={showPassword ? 'text' : 'password'}
value={password}
onChange={(e) => setPassword(e.target.value)}
required
minLength={6}
className="w-full px-5 py-3.5 pr-12 rounded-xl transition-all focus:outline-none focus:ring-2 focus:ring-primary-500/50 glass-input"
placeholder="Min 6 characters"
/>
<button
type="button"
onClick={() => setShowPassword(!showPassword)}
className="absolute right-4 top-1/2 -translate-y-1/2 opacity-60 hover:opacity-100 transition-opacity"
>
{showPassword ? (
<EyeOff className="w-5 h-5" style={{ color: 'var(--text-muted)' }} />
) : (
<Eye className="w-5 h-5" style={{ color: 'var(--text-muted)' }} />
)}
</button>
</div>
</div>
<div>
<label htmlFor="confirmPassword" className="block text-sm font-medium mb-2" style={{ color: 'var(--text-muted)' }}>
Confirm Password
</label>
<div className="relative">
<input
id="confirmPassword"
type={showConfirmPassword ? 'text' : 'password'}
value={confirmPassword}
onChange={(e) => setConfirmPassword(e.target.value)}
required
minLength={6}
className="w-full px-5 py-3.5 pr-12 rounded-xl transition-all focus:outline-none focus:ring-2 focus:ring-primary-500/50 glass-input"
placeholder="Re-enter password"
/>
<button
type="button"
onClick={() => setShowConfirmPassword(!showConfirmPassword)}
className="absolute right-4 top-1/2 -translate-y-1/2 opacity-60 hover:opacity-100 transition-opacity"
>
{showConfirmPassword ? (
<EyeOff className="w-5 h-5" style={{ color: 'var(--text-muted)' }} />
) : (
<Eye className="w-5 h-5" style={{ color: 'var(--text-muted)' }} />
)}
</button>
</div>
</div>
{/* Password match indicator */}
{confirmPassword && (
<div className={`flex items-center gap-2 text-sm ${password === confirmPassword ? 'text-emerald-500' : 'text-amber-500'}`}>
{password === confirmPassword ? (
<>
<CheckCircle className="w-4 h-4" />
Passwords match
</>
) : (
<>
<AlertTriangle className="w-4 h-4" />
Passwords do not match
</>
)}
</div>
)}
<button
type="submit"
disabled={loading || password !== confirmPassword || password.length < 6}
className="w-full py-3.5 rounded-xl font-semibold text-white shadow-lg shadow-green-500/20 transition-all hover:scale-[1.02] active:scale-[0.98] disabled:opacity-70 disabled:cursor-not-allowed disabled:hover:scale-100"
style={{
background: 'linear-gradient(135deg, #22c55e 0%, #16a34a 100%)',
}}
>
{loading ? 'Updating Password...' : 'Update Password'}
</button>
</div>
</form>
<div className="mt-8 text-center pt-6 border-t border-border/50">
<Link to="/login" className="text-sm transition-colors hover:opacity-100 opacity-80 font-medium" style={{ color: '#22c55e' }}>
← Back to Login
</Link>
</div>
</motion.div>
</motion.div>
</div>
);
}
|