如何解决使用用户名注册Firebase Cliend Side +安全规则还是后端?
我已经使用用户名,电子邮件和密码实施了Firebase SignUp。基本上我在做什么:
1-使用电子邮件和密码创建用户(如果其他用户未使用用户名和电子邮件)
2-将用户名添加到用户
赞:
firebase
.createUserWithEmailAndPassword(email,password)
.then((currentUser) => {
// Get the username from the input
const username = this.usernameInput.current.getText();
// Create a user with this username and the current user uid in the db
firebase.createUserWithUsername(username,currentUser.user.uid); // <----------
})
.catch((err) => {
// ...
});
我的createUserWithUsername函数基本上是这样做的:
createUserWithUsername = (username,userId) => {
/*
Create a document in the usernames collection
which uid (of the document itself,not a field) is the given username.
*/
// Pass username to lowercase
username = username.toLowerCase();
// Initial user's data
const data = {
email: this.auth.currentUser.email,username,};
return this.db
.collection("usernames")
.doc(username)
.set({ userId })
.then(() => {
this.db.collection("users").doc(userId).set(data);
})
.catch((err) => {
console.log(err);
throw err;
});
/*
Pd: As firestore automatically removes empty documents,we have to
assign them a field. The user's id is a good option,because it will help us to
update usernames faster,acting like a 'Foreign Key' in a NoSql DB.
*/
};
我的问题是?将此代码留在客户端是错误的吗?可能是安全问题吗?我需要将其移至云功能/后端吗?
这是我的用户名集合的firestore安全规则:
match /usernames/{username} {
function isUsernameAvailable() {
return !exists(/databases/$(database)/documents/usernames/$(username));
}
allow read: if true;
allow write,update: if isSignedIn() && isUsernameAvailable();
// TODO - Allow delete?
}
我真的很感谢任何指导。谢谢。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。