I keep getting the error above when trying to sign in with Google on Firebase. My code
const provider = new GoogleAuthProvider();
export const auth = getAuth();
export async function signUpGoogle(userType) {
return await signInWithPopup(auth, provider)
.then((result) => {
// This gives you a Google Access Token. You can use it to access the Google API.
const credential = GoogleAuthProvider.credentialFromResult(result);
const token = credential.accessToken;
// The signed-in user info.
const user = result.user;
// ...
// create user in firestore
//init services
// Add a new document in collection "users"
setDoc(collection(db, "users", "/", `${userType}`, '/', 'users'), {
userId: user.uid,
firstName: user.displayName,
lastName: user.displayName,
contactNumber: user.phoneNumber,
county: "",
idNumber: "",
city: "",
zipCode: "",
fullName: "",
email: user.email,
imageAsUrl: {
imageAsUrl: user.photoURL,
},
}).catch((e) => {
console.log(e.toString());
});
sessionStorage.setItem("Auth Token", token);
return true
}).catch((error) => {
// Handle Errors here.
const errorCode = error.code;
const errorMessage = error.message;
// The email of the user's account used.
const email = error.email;
// The AuthCredential type that was used.
const credential = GoogleAuthProvider.credentialFromError(error);
console.log(errorCode, errorMessage);
console.log(error);
// ...
return false
});
}
I have folowed the google documentation about signin with google and javascript
The error that i am getting is
FirebaseError: Expected type 'Zu', but it was: a custom ea object
CodePudding user response:
The setDoc()
takes a DocumentReference as a parameter i.e. you must specify the document ID. You can use addDoc()
instead if you want to generate a random ID. But it might be useful to use user's UID as the document ID itself so try:
setDoc(doc(db, `users/${userType}/users/${user.uid}`), {
userId: user.uid,
firstName: user.displayName,
// ...
}).catch((e) => {
console.log(e.toString());
});