Reputation: 925
I'm building a web app using vanilla JS and firebase. I've followed a tutorial which is for user sign up with email and password, and have the code for when a user is created, insert a record into the 'users' database collection, with the user ID and bio value retrieved from the form.
This is:
// sign up the user & add firestore data
auth.createUserWithEmailAndPassword(email, password).then(cred => {
return db.collection('users').doc(cred.user.uid).set({
bio: signupForm['signup-bio'].value
});
}).then(() => {
//do some other stuff
});
});
The issue I am having is that I am trying to implement the same in my google auth, I want to be able to create an entry in the 'users' database collection when a user signs in with google. The code I have currently is:
/**
* Start the auth flow and authorises to Firebase.
* @param{boolean} interactive True if the OAuth flow should request with an interactive mode.
*/
function startAuth(interactive) {
// Request an OAuth token from the Chrome Identity API.
chrome.identity.getAuthToken({interactive: !!interactive}, function(token) {
if (chrome.runtime.lastError && !interactive) {
console.log('It was not possible to get a token programmatically.');
} else if(chrome.runtime.lastError) {
console.error(chrome.runtime.lastError);
} else if (token) {
// Authorize Firebase with the OAuth Access Token.
var credential = firebase.auth.GoogleAuthProvider.credential(null, token);
firebase.auth().signInWithCredential(credential).catch(function(error) {
// The OAuth token might have been invalidated. Lets' remove it from cache.
if (error.code === 'auth/invalid-credential') {
chrome.identity.removeCachedAuthToken({token: token}, function() {
startAuth(interactive);
});
}
});
} else {
console.error('The OAuth Token was null');
}
});
}
Essentially i want to take the functionality from the above block of code and apply the part that creates the document in users collection and use it with the second block. If anyone could help me understand how to return the user ID from the google auth and insert it into a collection 'users' with the document ID the same as the user ID that would be very appreciated.
Thanks, B
Update:
**// Authorize Firebase with the OAuth Access Token.
var credential = firebase.auth.GoogleAuthProvider.credential(null, token);
firebase.auth().signInWithCredential(credential)
.then(cred => {
return db.collection('users').doc(cred.user.uid).set({
bio: 'testing'
});
})
.catch(function(error) {
// The OAuth token might have been invalidated. Lets' remove it from cache.
if (error.code === 'auth/invalid-credential') {
chrome.identity.removeCachedAuthToken({token: token},
function() {
startAuth(interactive);
});
}
});**
Upvotes: 0
Views: 271
Reputation: 83113
Similarly to the createUserWithEmailAndPassword()
method, signInWithCredential()
method returns a Promise that resolves with a UserCredential
.
So you should do as follows:
firebase.auth().signInWithCredential(credential)
.then(cred => {
return db.collection('users').doc(cred.user.uid).set({
bio: signupForm['signup-bio'].value
});
})
.catch(function(error) {
// The OAuth token might have been invalidated. Lets' remove it from cache.
if (error.code === 'auth/invalid-credential') {
chrome.identity.removeCachedAuthToken({token: token}, function() {
startAuth(interactive);
});
}
});
Upvotes: 1