gpt4 book ai didi

javascript - 如何通过 Google 身份验证在 Firebase Firestore 数据库中创建记录

转载 作者:行者123 更新时间:2023-12-02 21:02:45 25 4
gpt4 key购买 nike

我正在使用 vanilla JS 和 firebase 构建一个 Web 应用程序。我遵循了一个教程,该教程用于用户使用电子邮件和密码注册,并具有创建用户时的代码,将记录插入到“用户”数据库集合中,并从表单中检索用户 ID 和生物值.

这是:

// 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
});
});

我遇到的问题是我正在尝试在我的谷歌身份验证中实现相同的功能,我希望当用户使用谷歌登录时能够在“用户”数据库集合中创建一个条目。我目前拥有的代码是:

 /**
* 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');
}
});

}

本质上,我想从上面的代码块中获取功能,并应用在用户集合中创建文档的部分,并将其与第二个 block 一起使用。如果有人可以帮助我了解如何从 google auth 返回用户 ID 并将其插入到文档 ID 与用户 ID 相同的集合“用户”中,我将非常感激。

谢谢,

更新:

**// 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);
});
}
});**

最佳答案

类似于 createUserWithEmailAndPassword()方法,signInWithCredential()方法返回一个 Promise,该 Promise 解析为 UserCredential .

所以你应该这样做:

  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);
});
}
});

关于javascript - 如何通过 Google 身份验证在 Firebase Firestore 数据库中创建记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61303247/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com