File size: 10,090 Bytes
50a8ea3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import React, { createContext, useContext, useEffect, useState } from 'react';
import { onAuthStateChanged } from 'firebase/auth';
import {
  auth,
  signInWithGoogle,
  signInWithEmail,
  signUpWithEmail,
  sendUserEmailVerification,
  logOut,
  testConnection,
} from '../firebase';

export interface AppUser {
  uid: string;
  email: string | null;
  displayName: string | null;
  photoURL: string | null;
  emailVerified: boolean;
}

interface AuthContextType {
  user: AppUser | null;
  loading: boolean;
  signIn: () => Promise<AppUser>;
  signInEmail: (email: string, pass: string) => Promise<AppUser>;
  signUpEmail: (email: string, pass: string, name?: string) => Promise<AppUser>;
  sendVerificationEmail: () => Promise<void>;
  reloadUser: () => Promise<void>;
  signOut: () => Promise<void>;
  authError: string | null;
  clearAuthError: () => void;
}

const AuthContext = createContext<AuthContextType>({
  user: null,
  loading: true,
  signIn: async () => {
    throw new Error('AuthContext not initialized');
  },
  signInEmail: async () => {
    throw new Error('AuthContext not initialized');
  },
  signUpEmail: async () => {
    throw new Error('AuthContext not initialized');
  },
  sendVerificationEmail: async () => {
    throw new Error('AuthContext not initialized');
  },
  reloadUser: async () => {},
  signOut: async () => {},
  authError: null,
  clearAuthError: () => {},
});

export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => {
  const [user, setUser] = useState<AppUser | null>(null);
  const [loading, setLoading] = useState(true);
  const [authError, setAuthError] = useState<string | null>(null);

  useEffect(() => {
    testConnection().catch(() => {});

    const unsubscribe = onAuthStateChanged(auth, (firebaseUser) => {
      if (firebaseUser) {
        const appUser: AppUser = {
          uid: firebaseUser.uid,
          email: firebaseUser.email,
          displayName: firebaseUser.displayName || firebaseUser.email?.split('@')[0] || 'Citizen',
          photoURL: firebaseUser.photoURL,
          emailVerified: firebaseUser.emailVerified,
        };
        setUser(appUser);
      } else {
        setUser(null);
      }
      setLoading(false);
    });

    return () => unsubscribe();
  }, []);

  const handleSignInGoogle = async (): Promise<AppUser> => {
    setAuthError(null);
    try {
      const loggedUser = await signInWithGoogle();
      const appUser: AppUser = {
        uid: loggedUser.uid,
        email: loggedUser.email,
        displayName: loggedUser.displayName || 'Citizen',
        photoURL: loggedUser.photoURL,
        emailVerified: loggedUser.emailVerified,
      };
      setUser(appUser);
      return appUser;
    } catch (err: any) {
      console.error('Google sign-in error:', err);
      let message = 'Google sign-in could not be completed.';

      if (err?.code === 'auth/unauthorized-domain') {
        message =
          'This domain is not yet authorized for Google Sign-In in Firebase. Please use Email Login below, or add this domain in Firebase Console > Authentication > Settings > Authorized Domains.';
      } else if (err?.code === 'auth/popup-blocked') {
        message = 'Google sign-in popup was blocked by your browser. Please allow popups or use Email Login.';
      } else if (err?.code === 'auth/cancelled-popup-request' || err?.code === 'auth/popup-closed-by-user') {
        message = 'The Google sign-in window was closed before completing.';
      } else if (err?.message) {
        message = err.message;
      }
      setAuthError(message);
      throw new Error(message);
    }
  };

  const handleSignInEmail = async (email: string, pass: string): Promise<AppUser> => {
    setAuthError(null);
    try {
      const loggedUser = await signInWithEmail(email, pass);

      // Check if email is verified
      if (!loggedUser.emailVerified) {
        // Send a fresh verification email
        try {
          await sendUserEmailVerification(loggedUser);
        } catch (vErr) {
          console.warn('Could not auto-resend verification on login attempt:', vErr);
        }
        await logOut();
        setUser(null);
        const unverifiedError: any = new Error(
          'Email verification required. We have sent a verification link to your email. Please click the link to verify your account before logging in.'
        );
        unverifiedError.code = 'auth/unverified-email';
        throw unverifiedError;
      }

      const appUser: AppUser = {
        uid: loggedUser.uid,
        email: loggedUser.email,
        displayName: loggedUser.displayName || email.split('@')[0],
        photoURL: loggedUser.photoURL,
        emailVerified: loggedUser.emailVerified,
      };
      setUser(appUser);
      return appUser;
    } catch (err: any) {
      console.error('Email sign-in error:', err);
      let message = 'Could not sign in with this email and password.';
      if (err?.code === 'auth/unverified-email') {
        message = err.message;
      } else if (err?.code === 'auth/operation-not-allowed') {
        message =
          'Email/Password provider is not enabled in Firebase Console. Go to Firebase Console > Authentication > Sign-in method > Enable "Email/Password", or click "Continue with Google".';
      } else if (
        err?.code === 'auth/user-not-found' ||
        err?.code === 'auth/wrong-password' ||
        err?.code === 'auth/invalid-credential'
      ) {
        message = 'Invalid email or password. If you do not have an account yet, click "Create Account".';
      } else if (err?.code === 'auth/invalid-email') {
        message = 'Please enter a valid email address.';
      } else if (err?.message) {
        message = err.message;
      }
      setAuthError(message);
      throw new Error(message);
    }
  };

  const handleSignUpEmail = async (email: string, pass: string, name?: string): Promise<AppUser> => {
    setAuthError(null);
    try {
      const loggedUser = await signUpWithEmail(email, pass, name);

      // Send email verification link
      try {
        await sendUserEmailVerification(loggedUser);
      } catch (verifErr: any) {
        console.warn('Auto verification email notice:', verifErr);
      }

      // Log out unverified user to prevent unverified session
      await logOut();
      setUser(null);

      const verificationNeeded: any = new Error(
        `Account created for ${email}! A verification link has been sent to your email inbox. Please verify your email before signing in.`
      );
      verificationNeeded.code = 'auth/verification-sent';
      throw verificationNeeded;
    } catch (err: any) {
      console.error('Email sign-up error:', err);
      let message = 'Could not create account.';
      if (err?.code === 'auth/verification-sent') {
        message = err.message;
      } else if (err?.code === 'auth/operation-not-allowed') {
        message =
          'Email/Password registration is not enabled in Firebase Console. Go to Firebase Console > Authentication > Sign-in method > Enable "Email/Password", or click "Continue with Google".';
      } else if (err?.code === 'auth/email-already-in-use') {
        message = 'An account with this email already exists. Please sign in instead.';
      } else if (err?.code === 'auth/weak-password') {
        message = 'Password should be at least 6 characters.';
      } else if (err?.code === 'auth/invalid-email') {
        message = 'Please enter a valid email address.';
      } else if (err?.message) {
        message = err.message;
      }
      setAuthError(message);
      throw new Error(message);
    }
  };

  const handleSendVerificationEmail = async () => {
    setAuthError(null);
    if (!auth.currentUser) {
      const msg = 'You must be signed in to send a verification email.';
      setAuthError(msg);
      throw new Error(msg);
    }
    try {
      await sendUserEmailVerification(auth.currentUser);
    } catch (err: any) {
      console.error('Send verification email error:', err);
      let message = 'Failed to send verification email.';
      if (err?.code === 'auth/operation-not-allowed') {
        message =
          'Email verification is not enabled in Firebase Console. Enable "Email/Password" provider in Firebase Console > Authentication > Sign-in method.';
      } else if (err?.code === 'auth/too-many-requests') {
        message = 'Too many requests. Please wait a few moments before trying to resend the verification email.';
      } else if (err?.message) {
        message = err.message;
      }
      setAuthError(message);
      throw new Error(message);
    }
  };

  const handleReloadUser = async () => {
    if (auth.currentUser) {
      await auth.currentUser.reload();
      const updatedUser = auth.currentUser;
      setUser({
        uid: updatedUser.uid,
        email: updatedUser.email,
        displayName: updatedUser.displayName || updatedUser.email?.split('@')[0] || 'Citizen',
        photoURL: updatedUser.photoURL,
        emailVerified: updatedUser.emailVerified,
      });
    }
  };

  const handleSignOut = async () => {
    setAuthError(null);
    try {
      await logOut();
      setUser(null);
    } catch (err: any) {
      console.error('Sign-out failed:', err);
      setUser(null);
    }
  };

  return (
    <AuthContext.Provider

      value={{

        user,

        loading,

        signIn: handleSignInGoogle,

        signInEmail: handleSignInEmail,

        signUpEmail: handleSignUpEmail,

        sendVerificationEmail: handleSendVerificationEmail,

        reloadUser: handleReloadUser,

        signOut: handleSignOut,

        authError,

        clearAuthError: () => setAuthError(null),

      }}

    >

      {children}

    </AuthContext.Provider>
  );
};

export const useAuth = () => useContext(AuthContext);