OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Google login doesn't redirect

  • Thread starter Thread starter Cardenas Juniel P
  • Start date Start date
C

Cardenas Juniel P

Guest
I having trouble with my google login using firebase auth. What i want to do is if the user email is already exist in the database it will redirect the user to its respective page and then if not, the user will be redirected to sign up form with the google user info from google pre-filled in the form(it's working). The only problem is that if I try to login with the email that's already in the database it doesn't redirects me to home page.

Code:
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);

    });

});

<p>I having trouble with my google login using firebase auth. What i want to do is if the user email is already exist in the database it will redirect the user to its respective page and then if not, the user will be redirected to sign up form with the google user info from google pre-filled in the form(it's working). The only problem is that if I try to login with the email that's already in the database it doesn't redirects me to home page.</p>
<pre><code>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);

});
</code></pre>
<p>});</p>
 

Latest posts

Top