Skip to content

Commit

Permalink
d
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Schwartz committed Nov 8, 2024
1 parent dd2ceaf commit c5ffd67
Showing 1 changed file with 20 additions and 19 deletions.
39 changes: 20 additions & 19 deletions assets/js/firebaseAuth.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ async function signUp(email, password) {
const recaptchaToken = await generateRecaptchaToken('signup');
console.log('Generated reCAPTCHA token:', recaptchaToken);

// Step 2: Prepare the request body with the recaptchaToken, email, and password
// Step 2: Prepare the request body with the reCAPTCHA token, email, and password
const requestBody = { token: recaptchaToken, email, password };
console.log("Request body for sign-up:", requestBody);

Expand All @@ -99,32 +99,32 @@ async function signUp(email, password) {
const data = await response.json();
console.log("Response from server:", data);

// Step 5: Handle the backend response
// Step 5: Set JWT token in cookie if response is valid
if (data.success === true && typeof data.jwtToken === 'string' && data.jwtToken.trim() !== '') {
console.log("Backend returned a valid JWT token.");

// Set JWT token in cookie
setBackendAuthToken(data.jwtToken);
console.log("Backend JWT token set successfully");

// Step 6: Ensure user is authenticated in Firebase before storing JWT token
await new Promise((resolve) => {
const unsubscribe = onAuthStateChanged(auth, async (user) => {
if (user) {
const userId = user.uid;
unsubscribe(); // Stop listening once we have the user

// Store JWT token in Firestore
await storeJwtInFirestore(userId, data.jwtToken);
console.log("User data stored in Firestore with JWT token:", userId);
resolve();
}
});
});
// Step 6: Send email verification
await auth.currentUser.sendEmailVerification();
alert("A verification email has been sent to your email address. Please verify to complete registration.");

// Step 7: Listen for email verification status
const unsubscribe = onAuthStateChanged(auth, async (user) => {
if (user && user.emailVerified) {
const userId = user.uid;
unsubscribe(); // Stop listening once email is verified

// Store JWT token in Firestore
await storeJwtInFirestore(userId, data.jwtToken);
console.log("User data stored in Firestore with JWT token after email verification:", userId);

transitionModalStep('step1', 'step2');
console.log("Transitioned to step2 successfully");
return;
// Notify user of completion or redirect
alert("Email verified! You’re fully registered.");
}
});
} else {
console.error("Invalid response from server: JWT token not provided.");
throw new Error(data.message || 'reCAPTCHA verification or sign-up failed');
Expand All @@ -135,6 +135,7 @@ async function signUp(email, password) {
}
}


async function signIn(email, password) {
try {
// Sign in the user with Firebase Authentication
Expand Down

0 comments on commit c5ffd67

Please sign in to comment.