#auth-astro id with google
2 messages · Page 1 of 1 (latest)
To retrieve the user's ID after they've authenticated with Google as an OAuth provider, you need to ensure that you're correctly handling the sign-in process and retrieving the user's credentials.
In the Astro documentation, there's an example of how to handle Google OAuth sign-in. After the user signs in with Google, a POST request is sent to the /api/auth/signin endpoint with the ID token generated by the OAuth provider. The endpoint verifies the ID token and creates a new session cookie for the user.
Here's the relevant code snippet:
const googleSignin = document.querySelector("#google") as HTMLButtonElement;
googleSignin.addEventListener("click", async () => {
const provider = new GoogleAuthProvider();
const userCredential = await signInWithPopup(auth, provider);
const idToken = await userCredential.user.getIdToken();
const res = await fetch("/api/auth/signin", {
headers: {
Authorization: `Bearer ${idToken}`,
},
});
if (res.redirected) {
window.location.assign(res.url);
}
});
In this code, userCredential.user should contain the user's information after they've signed in. If session.user.id is undefined, it's possible that the user's ID isn't being correctly stored in the session, or the session isn't being correctly retrieved.
Please ensure that you're correctly handling the sign-in process and storing the user's credentials in the session. If you're using a library like auth-astro, you might need to refer to its specific documentation or community resources for more details on how to handle user sessions.