Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
354 lines (296 loc) · 12.3 KB
/
Copy pathscript.js
File metadata and controls
354 lines (296 loc) · 12.3 KB
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
// Global Supabase client creation function (resilient)
const createClient = () => {
try {
// Return cached instance if available
if (window._supabaseClient) return window._supabaseClient;
// Determine config from window or built-in fallback
const fallbackUrl = 'https://ckyncyqsakqevzeqgwgv.supabase.co';
const fallbackKey = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImNreW5jeXFzYWtxZXZ6ZXFnd2d2Iiwicm9sZSI6ImFub24iLCJpYXQiOjE3NTU3NzE2MTUsImV4cCI6MjA3MTM0NzYxNX0.t32UWdoe6w9b-OYfxb_FcTWTwvGJP-uIndGATxWOowQ';
const cfg = (window && window.APP_CONFIG) ? window.APP_CONFIG : { SUPABASE_URL: fallbackUrl, SUPABASE_KEY: fallbackKey };
if (!window?.supabase) {
console.error('Supabase library not loaded yet (CDN). If on a restricted network, ensure CDN access.');
return null;
}
window._supabaseClient = window.supabase.createClient(cfg.SUPABASE_URL, cfg.SUPABASE_KEY);
return window._supabaseClient;
} catch (err) {
console.error('Failed to create Supabase client:', err);
return null;
}
};
// Parent signup -> send email confirmation via Supabase and redirect to login page
document.addEventListener('DOMContentLoaded', () => {
const signupForm = document.getElementById('parent-signup-form');
const loginForm = document.getElementById('parent-login-form');
// Handle parent signup form
if (signupForm) {
signupForm.addEventListener('submit', async (e) => {
e.preventDefault();
const submitBtn = signupForm.querySelector('.submit-btn');
const firstName = document.getElementById('firstName').value.trim();
const surname = document.getElementById('surname').value.trim();
const countryCode = document.getElementById('countryCode').value;
const mobile = document.getElementById('mobile').value.trim();
const defaultCurrency = document.getElementById('defaultCurrency').value;
const province = document.getElementById('province').value;
const gender = document.getElementById('gender').value;
const email = document.getElementById('email').value.trim();
const password = document.getElementById('password').value;
const confirm = document.getElementById('confirmPassword').value;
const accepted = document.getElementById('terms').checked;
if (!accepted) {
alert('Please accept the Terms & Conditions to continue.');
return;
}
if (password !== confirm) {
alert('Passwords do not match. Please confirm your password.');
return;
}
const supabase = createClient();
if (!supabase) {
alert('Supabase client not available. Please refresh the page.');
return;
}
try {
if (submitBtn) { submitBtn.disabled = true; submitBtn.textContent = 'Creating...'; }
// Add debug logging
console.log('Attempting signup with:', { email, defaultCurrency });
// Sign up and let Supabase send a confirmation email
const { data, error } = await supabase.auth.signUp({
email,
password,
options: {
emailRedirectTo: `${window.location.origin}/parentLogin.html`,
data: {
first_name: firstName,
surname: surname,
province: province,
gender: gender,
phone_country_code: countryCode,
phone_number: mobile,
default_currency: defaultCurrency
}
}
});
if (error) throw error;
console.log('Signup response:', data);
// Check if email confirmation is required
if (data.user && !data.user.email_confirmed_at) {
alert('Account created! Please check your email (including spam folder) for a confirmation link. After confirming, you can sign in.');
} else {
alert('Account created successfully! You can now sign in.');
}
window.location.href = 'parentLogin.html';
} catch (err) {
console.error('Signup error:', err);
if (err.message && err.message.includes('fetch')) {
alert('Network error: Please check your internet connection and try again.');
} else {
alert(err.message || 'Could not create your account. Please try again.');
}
} finally {
if (submitBtn) { submitBtn.disabled = false; submitBtn.textContent = 'Create Account'; }
}
});
}
// Handle parent login form
if (loginForm) {
loginForm.addEventListener('submit', async (e) => {
e.preventDefault();
const submitBtn = loginForm.querySelector('.submit-btn');
const email = document.getElementById('email').value.trim();
const password = document.getElementById('password').value;
const rememberMe = document.getElementById('rememberMe').checked;
if (!email || !password) {
alert('Please enter both email and password.');
return;
}
const supabase = createClient();
if (!supabase) {
alert('Supabase client not available. Please refresh the page.');
return;
}
try {
if (submitBtn) { submitBtn.disabled = true; submitBtn.textContent = 'Signing In...'; }
const { data, error } = await supabase.auth.signInWithPassword({
email,
password
});
console.log('Login attempt result:', { data, error });
if (error) {
console.error('Login error details:', error);
// Check if it's really an email confirmation issue
if (/Email not confirmed/i.test(error.message) || error.message.includes('email_not_confirmed')) {
// Try to resend confirmation with proper redirect
try {
const { error: resendError } = await supabase.auth.resend({
type: 'signup',
email: email,
options: {
emailRedirectTo: `${window.location.origin}/parentLogin.html`
}
});
if (resendError) {
console.error('Resend error:', resendError);
alert('Email confirmation issue detected. Please check your email and click the confirmation link. If you need help, contact support.');
} else {
alert('A fresh confirmation email has been sent. Please check your inbox and click the new confirmation link to activate your account.');
}
} catch (resendErr) {
console.error('Resend failed:', resendErr);
alert('Email confirmation required. Please check your inbox for the confirmation link.');
}
} else if (/Invalid login credentials/i.test(error.message)) {
alert('Invalid email or password. Please try again.');
} else {
alert('Login failed: ' + error.message);
}
return;
}
// Additional check for email confirmation
if (data.user && !data.user.email_confirmed_at) {
alert('Your email address needs to be confirmed. Please check your inbox for the confirmation link.');
return;
}
if (data.user) {
// Handle user profile creation/update
await handleUserProfile(data.user);
if (rememberMe) {
localStorage.setItem('rememberMe', 'true');
} else {
localStorage.removeItem('rememberMe');
}
window.location.href = 'parentDashboard.html';
}
} catch (err) {
alert('An unexpected error occurred. Please try again.');
console.error('Login error:', err);
} finally {
if (submitBtn) { submitBtn.disabled = false; submitBtn.textContent = 'Sign In'; }
}
});
}
});
// Google OAuth sign-in
async function signInWithGoogle() {
const supabase = createClient();
if (!supabase) {
alert('Supabase client not available. Please refresh the page.');
return;
}
try {
const { data, error } = await supabase.auth.signInWithOAuth({
provider: 'google',
options: {
redirectTo: window.location.origin + '/parentDashboard.html'
}
});
if (error) throw error;
// Redirect handled by provider
} catch (err) {
alert('Google sign-in failed: ' + (err.message || 'Unknown error'));
}
}
// Function to handle user profile creation/update after authentication
async function handleUserProfile(user) {
const supabase = createClient();
if (!supabase) return null;
try {
console.log('handleUserProfile called with user metadata:', user.user_metadata);
const { data: existingProfile, error: fetchError } = await supabase
.from('profiles')
.select('*')
.eq('id', user.id)
.single();
if (fetchError && fetchError.code !== 'PGRST116') {
console.error('Error fetching profile:', fetchError);
}
let profileData = {};
if (user.app_metadata?.provider === 'google') {
const fullName = user.user_metadata?.full_name || '';
const firstName = user.user_metadata?.first_name || '';
const surname = user.user_metadata?.last_name || '';
profileData = {
id: user.id,
email: user.email,
first_name: firstName || fullName.split(' ')[0] || '',
surname: surname || fullName.split(' ').slice(1).join(' ') || '',
default_currency: user.user_metadata?.default_currency || 'USD',
updated_at: new Date().toISOString()
};
} else {
// Try to use metadata set during signUp
const md = user.user_metadata || {};
profileData = {
id: user.id,
email: user.email,
first_name: md.first_name || md.firstName || '',
surname: md.surname || md.last_name || md.lastName || '',
province: md.province || null,
gender: md.gender || null,
phone_country_code: md.phone_country_code || null,
phone_number: md.phone_number || null,
default_currency: md.default_currency || 'USD',
updated_at: new Date().toISOString()
};
}
// Always ensure currency is saved to profile from user metadata
if (user.user_metadata?.default_currency) {
profileData.default_currency = user.user_metadata.default_currency;
console.log('Overriding currency with user metadata:', user.user_metadata.default_currency);
}
console.log('About to save profile with data:', profileData);
const { error: upsertError } = await supabase
.from('profiles')
.upsert(profileData, { onConflict: 'id' });
if (upsertError) {
console.error('Error upserting profile:', upsertError);
} else {
console.log('Profile saved successfully with currency:', profileData.default_currency);
}
return profileData;
} catch (err) {
console.error('Error handling user profile:', err);
return null;
}
}
// Multi-currency functionality
function updateCurrencyFromPhone() {
const countryCodeSelect = document.getElementById('countryCode');
const currencySelect = document.getElementById('defaultCurrency');
const currencyNote = document.getElementById('currencyNote');
if (!countryCodeSelect || !currencySelect) return;
const selectedOption = countryCodeSelect.options[countryCodeSelect.selectedIndex];
const suggestedCurrency = selectedOption.getAttribute('data-currency');
if (suggestedCurrency) {
currencySelect.value = suggestedCurrency;
currencyNote.textContent = `Currency auto-selected as ${suggestedCurrency} based on your phone number. You can change this if needed.`;
}
}
// Initialize currency selection on page load
document.addEventListener('DOMContentLoaded', () => {
// Set initial currency based on default country code
setTimeout(() => {
updateCurrencyFromPhone();
}, 100);
// Scroll animation for Get in Touch section
const observerOptions = {
threshold: 0.1,
rootMargin: '0px 0px -50px 0px'
};
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
entry.target.classList.add('slide-in');
}
});
}, observerOptions);
const getInTouchImage = document.querySelector('.get-in-touch-image');
const getInTouchContent = document.querySelector('.get-in-touch-content');
if (getInTouchImage) {
observer.observe(getInTouchImage);
}
if (getInTouchContent) {
observer.observe(getInTouchContent);
}
});