All files / src/pages/marketing MarketingLoginPage.tsx

95.08% Statements 58/61
88% Branches 44/50
100% Functions 7/7
95.08% Lines 58/61

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                              1x 506x 506x 506x 506x   506x 506x   506x 15x 15x   15x         12x 1x         1x 1x     11x 11x 11x 11x 12x   12x 12x       12x 12x 12x 12x   12x 12x 12x   12x 1x 1x     10x 1x 1x     9x 1x 1x     8x 1x 1x     7x         7x   7x 2x 5x 5x     7x   7x 7x 7x 7x           3x           506x           506x                                                                         1518x                                                                                                                                                   247x                                                     185x                                                                                                                
/**
 * Marketing Login Page
 * Modern login page that matches the marketing site theme
 */
 
import React, { useState } from 'react';
import { useTranslation } from 'react-i18next';
import { useLogin } from '../../hooks/useAuth';
import { useNavigate, Link } from 'react-router-dom';
import SmoothScheduleLogo from '../../components/SmoothScheduleLogo';
import OAuthButtons from '../../components/OAuthButtons';
import LanguageSelector from '../../components/LanguageSelector';
import { DevQuickLogin } from '../../components/DevQuickLogin';
import { AlertCircle, Loader2, Mail, Lock, ArrowRight, CheckCircle2, Calendar, Zap, Shield } from 'lucide-react';
 
const MarketingLoginPage: React.FC = () => {
  const { t } = useTranslation();
  const [email, setEmail] = useState('');
  const [password, setPassword] = useState('');
  const [error, setError] = useState('');
 
  const navigate = useNavigate();
  const loginMutation = useLogin();
 
  const handleSubmit = async (e: React.FormEvent) => {
    e.preventDefault();
    setError('');
 
    loginMutation.mutate(
      { email, password },
      {
        onSuccess: (data) => {
          // Check if MFA is required
          if (data.mfa_required) {
            sessionStorage.setItem('mfa_challenge', JSON.stringify({
              user_id: data.user_id,
              mfa_methods: data.mfa_methods,
              phone_last_4: data.phone_last_4,
            }));
            navigate('/mfa-verify');
            return;
          }
 
          const user = data.user!;
          const currentHostname = window.location.hostname;
          const currentPort = window.location.port;
          const portStr = currentPort ? `:${currentPort}` : '';
          const protocol = window.location.protocol;
 
          const hostnameParts = currentHostname.split('.');
          const baseDomain = hostnameParts.length >= 2
            ? hostnameParts.slice(-2).join('.')
            : currentHostname;
 
          const isRootDomain = currentHostname === baseDomain || currentHostname === 'localhost';
          const isPlatformDomain = currentHostname === `platform.${baseDomain}`;
          const currentSubdomain = hostnameParts[0];
          const isBusinessSubdomain = !isRootDomain && !isPlatformDomain && currentSubdomain !== 'api';
 
          const isPlatformUser = ['superuser', 'platform_manager', 'platform_support'].includes(user.role);
          const isBusinessUser = ['owner', 'manager', 'staff', 'resource'].includes(user.role);
          const isCustomer = user.role === 'customer';
 
          if (isPlatformUser && isBusinessSubdomain) {
            setError(t('auth.invalidCredentials'));
            return;
          }
 
          if (isBusinessUser && isBusinessSubdomain && user.business_subdomain !== currentSubdomain) {
            setError(t('auth.invalidCredentials'));
            return;
          }
 
          if (isCustomer && isRootDomain) {
            setError(t('auth.invalidCredentials'));
            return;
          }
 
          if (isCustomer && isPlatformDomain) {
            setError(t('auth.invalidCredentials'));
            return;
          }
 
          Iif (isCustomer && isBusinessSubdomain && user.business_subdomain !== currentSubdomain) {
            setError(t('auth.invalidCredentials'));
            return;
          }
 
          let targetSubdomain: string | null = null;
 
          if (isPlatformUser && !isPlatformDomain) {
            targetSubdomain = 'platform';
          E} else if (isBusinessUser && user.business_subdomain && !isBusinessSubdomain) {
            targetSubdomain = user.business_subdomain;
          }
 
          const needsRedirect = targetSubdomain !== null;
 
          Eif (needsRedirect) {
            const targetHostname = `${targetSubdomain}.${baseDomain}`;
            window.location.href = `${protocol}//${targetHostname}${portStr}/?access_token=${data.access}&refresh_token=${data.refresh}`;
            return;
          }
 
          navigate('/');
        },
        onError: (err: any) => {
          setError(err.response?.data?.error || t('auth.invalidCredentials'));
        },
      }
    );
  };
 
  const features = [
    { icon: Calendar, text: t('auth.login.features.scheduling') },
    { icon: Zap, text: t('auth.login.features.automation') },
    { icon: Shield, text: t('auth.login.features.security') },
  ];
 
  return (
    <div className="min-h-screen bg-white dark:bg-gray-900">
      {/* Background Elements */}
      <div className="absolute inset-0 overflow-hidden pointer-events-none">
        <div className="absolute top-0 right-0 w-[800px] h-[800px] bg-brand-500/5 rounded-full blur-3xl translate-x-1/2 -translate-y-1/2" />
        <div className="absolute bottom-0 left-0 w-[600px] h-[600px] bg-purple-500/5 rounded-full blur-3xl -translate-x-1/2 translate-y-1/2" />
      </div>
 
      <div className="relative min-h-screen flex">
        {/* Left Side - Branding & Features */}
        <div className="hidden lg:flex lg:w-1/2 xl:w-[55%] flex-col justify-between p-12 bg-gray-50 dark:bg-gray-800/50">
          <div>
            <Link to="/" className="inline-flex items-center gap-3 group">
              <SmoothScheduleLogo className="w-10 h-10 text-brand-600 dark:text-brand-400" />
              <span className="text-xl font-bold text-gray-900 dark:text-white group-hover:text-brand-600 dark:group-hover:text-brand-400 transition-colors">
                SmoothSchedule
              </span>
            </Link>
          </div>
 
          <div className="max-w-lg">
            <div className="inline-flex items-center gap-2 px-3 py-1 rounded-full bg-brand-50 dark:bg-brand-900/30 border border-brand-100 dark:border-brand-800 mb-6">
              <span className="flex h-2 w-2 rounded-full bg-brand-600 dark:bg-brand-400 animate-pulse" />
              <span className="text-sm font-medium text-brand-700 dark:text-brand-300">
                {t('auth.login.platformBadge')}
              </span>
            </div>
 
            <h1 className="text-4xl xl:text-5xl font-bold text-gray-900 dark:text-white mb-6 leading-tight">
              {t('auth.login.heroTitle')}
            </h1>
            <p className="text-lg text-gray-600 dark:text-gray-400 mb-10">
              {t('auth.login.heroSubtitle')}
            </p>
 
            <div className="space-y-4">
              {features.map((feature, idx) => (
                <div key={idx} className="flex items-center gap-4 p-4 bg-white dark:bg-gray-800 rounded-xl border border-gray-100 dark:border-gray-700 shadow-sm">
                  <div className="p-2 bg-brand-50 dark:bg-brand-900/30 rounded-lg">
                    <feature.icon className="w-5 h-5 text-brand-600 dark:text-brand-400" />
                  </div>
                  <span className="text-gray-700 dark:text-gray-300 font-medium">{feature.text}</span>
                </div>
              ))}
            </div>
          </div>
 
          <div className="flex items-center gap-6 text-sm text-gray-500 dark:text-gray-400">
            <span>© {new Date().getFullYear()} SmoothSchedule</span>
            <Link to="/privacy" className="hover:text-brand-600 dark:hover:text-brand-400 transition-colors">
              {t('auth.login.privacy')}
            </Link>
            <Link to="/terms" className="hover:text-brand-600 dark:hover:text-brand-400 transition-colors">
              {t('auth.login.terms')}
            </Link>
          </div>
        </div>
 
        {/* Right Side - Login Form */}
        <div className="flex-1 flex flex-col justify-center py-12 px-6 sm:px-12 lg:px-16 xl:px-24">
          <div className="mx-auto w-full max-w-md">
            {/* Mobile Logo */}
            <div className="lg:hidden text-center mb-10">
              <Link to="/" className="inline-flex items-center gap-3">
                <SmoothScheduleLogo className="w-12 h-12 text-brand-600" />
                <span className="text-2xl font-bold text-gray-900 dark:text-white">SmoothSchedule</span>
              </Link>
            </div>
 
            <div className="text-center lg:text-left mb-8">
              <h2 className="text-3xl font-bold text-gray-900 dark:text-white mb-2">
                {t('auth.login.title')}
              </h2>
              <p className="text-gray-600 dark:text-gray-400">
                {t('auth.login.subtitle')}{' '}
                <Link to="/signup" className="text-brand-600 dark:text-brand-400 hover:underline font-medium">
                  {t('auth.login.createAccount')}
                </Link>
              </p>
            </div>
 
            {error && (
              <div className="mb-6 rounded-xl bg-red-50 dark:bg-red-900/20 p-4 border border-red-100 dark:border-red-800/50 animate-in fade-in slide-in-from-top-2">
                <div className="flex gap-3">
                  <AlertCircle className="h-5 w-5 text-red-500 dark:text-red-400 flex-shrink-0 mt-0.5" />
                  <div>
                    <p className="text-sm font-medium text-red-800 dark:text-red-200">{t('auth.authError')}</p>
                    <p className="text-sm text-red-700 dark:text-red-300 mt-1">{error}</p>
                  </div>
                </div>
              </div>
            )}
 
            <form onSubmit={handleSubmit} className="space-y-5">
              <div>
                <label htmlFor="email" className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                  {t('auth.email')}
                </label>
                <div className="relative">
                  <div className="absolute inset-y-0 left-0 pl-4 flex items-center pointer-events-none">
                    <Mail className="h-5 w-5 text-gray-400" />
                  </div>
                  <input
                    id="email"
                    name="email"
                    type="email"
                    autoComplete="email"
                    required
                    className="block w-full pl-12 pr-4 py-3.5 border border-gray-200 dark:border-gray-700 rounded-xl bg-white dark:bg-gray-800 text-gray-900 dark:text-white placeholder-gray-400 focus:ring-2 focus:ring-brand-500 focus:border-transparent transition-all"
                    placeholder={t('auth.enterEmail')}
                    value={email}
                    onChange={(e) => setEmail(e.target.value)}
                  />
                </div>
              </div>
 
              <div>
                <div className="flex items-center justify-between mb-2">
                  <label htmlFor="password" className="block text-sm font-medium text-gray-700 dark:text-gray-300">
                    {t('auth.password')}
                  </label>
                  <Link to="/forgot-password" className="text-sm text-brand-600 dark:text-brand-400 hover:underline">
                    {t('auth.forgotPassword')}
                  </Link>
                </div>
                <div className="relative">
                  <div className="absolute inset-y-0 left-0 pl-4 flex items-center pointer-events-none">
                    <Lock className="h-5 w-5 text-gray-400" />
                  </div>
                  <input
                    id="password"
                    name="password"
                    type="password"
                    autoComplete="current-password"
                    required
                    className="block w-full pl-12 pr-4 py-3.5 border border-gray-200 dark:border-gray-700 rounded-xl bg-white dark:bg-gray-800 text-gray-900 dark:text-white placeholder-gray-400 focus:ring-2 focus:ring-brand-500 focus:border-transparent transition-all"
                    placeholder="••••••••"
                    value={password}
                    onChange={(e) => setPassword(e.target.value)}
                  />
                </div>
              </div>
 
              <button
                type="submit"
                disabled={loginMutation.isPending}
                className="w-full flex items-center justify-center gap-2 py-3.5 px-4 bg-brand-600 hover:bg-brand-700 text-white font-medium rounded-xl shadow-lg shadow-brand-600/25 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-brand-500 disabled:opacity-70 disabled:cursor-not-allowed transition-all transform active:scale-[0.98]"
              >
                {loginMutation.isPending ? (
                  <>
                    <Loader2 className="animate-spin h-5 w-5" />
                    {t('auth.signingIn')}
                  </>
                ) : (
                  <>
                    {t('auth.signIn')}
                    <ArrowRight className="h-5 w-5" />
                  </>
                )}
              </button>
            </form>
 
            {/* OAuth */}
            <div className="mt-8">
              <div className="relative">
                <div className="absolute inset-0 flex items-center">
                  <div className="w-full border-t border-gray-200 dark:border-gray-700" />
                </div>
                <div className="relative flex justify-center text-sm">
                  <span className="px-4 bg-white dark:bg-gray-900 text-gray-500 dark:text-gray-400">
                    {t('auth.orContinueWith')}
                  </span>
                </div>
              </div>
 
              <div className="mt-6">
                <OAuthButtons disabled={loginMutation.isPending} />
              </div>
            </div>
 
            {/* Language Selector */}
            <div className="mt-8 flex justify-center">
              <LanguageSelector />
            </div>
 
            <DevQuickLogin embedded />
          </div>
        </div>
      </div>
    </div>
  );
};
 
export default MarketingLoginPage;