import React, { useState } from 'react'; import { IonButton, IonContent, IonHeader, IonInput, IonItem, IonLabel, IonPage, IonText, IonTitle, IonToolbar, useIonViewWillEnter, } from '@ionic/react'; import { useHistory } from 'react-router-dom'; import { supabase } from '../supabase'; import { setStatusBarStyle, Style } from '../utils/statusBar'; const Auth: React.FC = () => { const history = useHistory(); const [mode, setMode] = useState<'login' | 'signup'>('login'); const [email, setEmail] = useState(''); const [password, setPassword] = useState(''); const [message, setMessage] = useState(''); const [errorMessage, setErrorMessage] = useState(''); const [isSubmitting, setIsSubmitting] = useState(false); useIonViewWillEnter(() => { setStatusBarStyle(Style.Light); }); const showError = (msg: string) => { setErrorMessage(msg); window.setTimeout(() => setErrorMessage(''), 4000); }; const showMessage = (msg: string) => { setMessage(msg); window.setTimeout(() => setMessage(''), 4000); }; const normaliseAuthError = (msg: string) => { const lowered = msg.toLowerCase(); if (lowered.includes('email not confirmed')) { return 'Please confirm your email before signing in. Check your inbox for the verification email.'; } if (lowered.includes('invalid login credentials')) { return 'Incorrect email or password. Please try again.'; } if (lowered.includes('user already registered')) { return 'An account with this email already exists. Please sign in instead.'; } return msg; }; const handleResendConfirmation = async () => { if (!email.trim()) { showError( 'Enter your email address first so we can resend the confirmation email.' ); return; } setIsSubmitting(true); const { error } = await supabase.auth.resend({ type: 'signup', email: email.trim(), }); setIsSubmitting(false); if (error) { showError(normaliseAuthError(error.message)); return; } showMessage('Confirmation email sent. Please check your inbox.'); }; const handleSubmit = async () => { if (!email.trim() || password.trim().length < 6) { showError('Enter a valid email and password with at least 6 characters.'); return; } setIsSubmitting(true); if (mode === 'login') { const { error } = await supabase.auth.signInWithPassword({ email: email.trim(), password, }); setIsSubmitting(false); if (error) { showError(normaliseAuthError(error.message)); return; } history.replace('/home'); return; } const { data, error } = await supabase.auth.signUp({ email: email.trim(), password, }); setIsSubmitting(false); if (error) { showError(normaliseAuthError(error.message)); return; } if (data.session) { history.replace('/home'); return; } showMessage('Account created! Check your email for a verification link.'); }; return ( {mode === 'login' ? 'Welcome back' : 'Create account'}

{mode === 'login' ? 'Sign in to continue' : 'Create your shopping account'}

Save your orders, addresses, and track deliveries securely.

{errorMessage && (

{errorMessage}

)} {message && (

{message}

)}
Email setEmail(e.detail.value ?? '')} /> Password setPassword(e.detail.value ?? '')} /> {mode === 'login' ? 'Sign in' : 'Create account'} {mode === 'login' && ( Resend confirmation email )} setMode(mode === 'login' ? 'signup' : 'login')} > {mode === 'login' ? 'Need an account? Sign up' : 'Already have an account? Sign in'}
); }; export default Auth;