仅当用户存在时,才向 Google 身份验证提供程序进行身份验证



我正在使用Firebase通过GoogleAuthProvider对我们应用中的用户进行身份验证。但是,如果新用户还不是经过身份验证的用户,我不希望他们登录。

如果用户存在,请登录并console.log('user ' + user.email + ' does exist!');

但是,如果用户不存在。然后不允许身份验证和console.log('user ' + user.email + ' does not exist!')

var googleProvider = new firebase.auth.GoogleAuthProvider();
export const doSignInWithGoogle = () => auth.signInWithPopup(googleProvider);
googleLogin = () => {
auth
.doSignInWithGoogle()
.then(result => {
var user = result.user;
const userRef = db.collection('users').doc(user.uid);
userRef.get().then(docSnapshot => {
if (docSnapshot.exists) {
userRef.onSnapshot(() => {
console.log('user ' + user.email + ' does exist!');
});
} else {
console.log('user ' + user.email + ' does not exist!');
}
});
})
.catch(error => {
this.setState(updateByPropertyName('error', error));
});
};

我认为在 Firestore 中引用用户记录将是一种简单的方法。但是,也许Firebase Auth已经有办法做到这一点。我找不到文档或任何示例。

在上面的代码中,不会记录任何内容,并且用户已创建或登录。

如何阻止新用户注册,同时仍允许当前用户登录?

如果你真的想使用signInWithPopup方法,你有这个选项, 但这不是最好的方法。当您使用 Google 登录时,signInWithPopup方法会返回一个承诺。可以从生成的对象访问isNewUser属性additionalUserInfo。然后删除刚刚创建的用户。

firebase.auth().signInWithPopup(provider).then(
function (result) {
var token = result.credential.accessToken;
var user = result.user;
//this is what you need
var isNewUser = result.additionalUserInfo.isNewUser;
if (isNewUser) {
//delete the created user
result.user.delete();
} else {
// your sign in flow
console.log('user ' + user.email + ' does exist!');
}
}).catch(function (error) {
// Handle Errors here.
});

这是简单的方法,但在创建后删除不是最佳做法。还有另一种选择,

您可以使用signInAndRetrieveDataWithCredential方法。 根据文档,

auth/user-not-found将是 如果使用凭据登录,则引发firebase.auth.EmailAuthProvider.credential并且没有用户 对应于给定的电子邮件。

function googleSignInWithCredentials(id_token) {
// Build Firebase credential with the Google ID token.
var credential = firebase.auth.GoogleAuthProvider.credential(id_token);
// Sign in with credential from the Google user.
firebase.auth().signInAndRetrieveDataWithCredential(credential)
.then(function (userCredential) {
//sign in
console.log(userCredential.additionalUserInfo.username);
}).catch(function (error) {
// Handle Errors here.
var errorCode = error.code;
if (errorCode === 'auth/user-not-found') {
//handle this
} else {
console.error(error);
}
});
}

下面是Firebase GitHub存储库中的一个例子。

具有Firebase安全规则,只能检查密钥是否存在 - 因此在 users 表中搜索不是一个选项:

"emails": {
"example1@gmail.com": true,
"example2@gmail.com": true
}

然后可以使用安全规则检查auth.token.email是否作为密钥存在

{
"rules": {
".read": "root.child('emails').child(auth.token.email).exists(),
".write": false,
}
}

在客户端中,这应该会抛出一个"The read failed: Permission denied error"错误,然后进行相应的处理。 挂接到Firebase注册是不可能的 - 但是虽然他们无法登录,但这有相同的努力(除了必须不时清理用户数据库(; 例如,使用云功能,删除用户,这些用户在emails"表"中没有电子邮件作为密钥。

Firestore安全规则中,可以检查:

  • request.auth.token.email&request.auth.token.email_verified

例如,使用名为emails的集合和名为content的集合:

match /databases/{database}/documents {
function userMatchesId(userId) {
return request.auth != null && request.auth.uid == userId
}
function readAllowed(email) {
return if get(/databases/$(database)/documents/emails/$(request.auth.token.email)).data != null
}
match /users/{userId} {
allow get: if userMatchesId(userId)
}
match /content {
allow get: if readAllowed(request.auth.token.email)
}
}

登录后从 Firebase 收到的对象additionalUserInfo属性isNewUser的位置

。您可以在此处找到参考资料:https://firebase.google.com/docs/reference/js/firebase.auth.html#.AdditionalUserInfo

最新更新