Код: Выделить всё
e.preventDefault(); // Prevent default form submission behavior
signInWithPopup(auth, provider)
.then((result) => {
const user = result.user;
const xhr = new XMLHttpRequest();
xhr.open("GET", `check.php?email=${encodeURIComponent(user.email)}`, true);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
try {
const response = JSON.parse(xhr.responseText);
console.log('Response:', response); // Log the response for debugging
if (response.exists) {
switch (response.role) {
case 'job_seeker':
window.location.href = 'job_seeker_home.php';
break;
case 'employer':
window.location.href = 'employer_home.php';
break;
default:
console.error('Unknown role:', response.role);
// Handle unknown roles here
}
} else {
window.location.href = `login.php?form_type=signup&name=${encodeURIComponent(user.displayName)}&email=${encodeURIComponent(user.email)}`;
}
} catch (error) {
console.error('Error parsing response:', error);
}
} else {
console.error('Request failed with status:', xhr.status);
}
}
};
xhr.onerror = function() {
console.error('Request failed');
};
xhr.send();
})
.catch((error) => {
console.error('Error during sign-in:', error);
});
Подробнее здесь: https://stackoverflow.com/questions/786 ... t-redirect