如何为实时数据库设置安全规则,但允许新用户注册和创建hashmap ?



我的应用程序需要用户注册Firebase,并在实时数据库中存储一些基本数据。我已经启用了邮件验证。这是注册活动代码:

private void register_user(String email, String userName, String password) {
showLoader ();
FirebaseHelper.createUser (email, password, task -> {
if (task.isSuccessful ()) {
//Send verification email
FirebaseUser user = FirebaseHelper.getUser ();
user.sendEmailVerification ().addOnSuccessListener (new OnSuccessListener<Void> () {
@Override
public void onSuccess(Void aVoid) {
Toast.makeText (RegisterActivity.this, "Verify your email by clicking on the link we have sent you.", Toast.LENGTH_SHORT).show ();
}
}).addOnFailureListener (new OnFailureListener () {
@Override
public void onFailure(@NonNull Exception e) {
Log.d ("TAG", "onFailure: email not sent: " + e.getMessage ());
}
});
// PROCEED WITH NORMAL APP FLOW
UserProfileChangeRequest profileUpdate = new UserProfileChangeRequest.Builder ()
.setDisplayName (userName).build ();
user.updateProfile (profileUpdate);
registerUserToQuickBlox (email, userName, password);

} else {
if (task.getException ().getClass () == FirebaseAuthUserCollisionException.class) {
registerUserToQuickBlox (email, userName, password);
} else {
hideLoader ();
Log.w ("TAG", "createUserWithEmail: failure", task.getException ());
try {
if (task.getException ().getMessage () != null) {
String error = task.getException ().getMessage ();
ToastUtils.longToast (error);
} else if (task.getException ().getLocalizedMessage () != null) {
String error = task.getException ().getLocalizedMessage ();
ToastUtils.longToast (error);
}
} catch (NullPointerException e) {
e.printStackTrace ();
ToastUtils.longToast ("Unknown error occurred");
}
}
}
});
}
private void registerUserToQuickBlox(String email, String userName, String password) {
QBUser qbUser = new QBUser ();
qbUser.setLogin (email);
qbUser.setEmail (email);
qbUser.setFullName (userName);
qbUser.setPassword (password);
Log.d ("QbS", "set QB Login etc. " + qbUser.toString ());
ChatHelper.getInstance ().signUp (qbUser, new QBEntityCallback<QBUser> () {
@Override
public void onSuccess(QBUser qbUser, Bundle bundle) {
Log.d ("QbS", "ChatHelper success. ");
HashMap<String, Object> hashMap = new HashMap<> ();
hashMap.put ("userName", userName);
hashMap.put ("email", email);
hashMap.put ("imgURL", "default");
hashMap.put ("qbId", qbUser.getId ());
hashMap.put ("isPracticing", false);
hashMap.put ("isOnline", true);
hashMap.put ("paymentMade", false);
hashMap.put ("level", "1");
hashMap.put ("userType", "student"); // get input from activity_register
FirebaseHelper.addUser (hashMap, (Void aVoid) -> {
qbUser.setPassword (password);
SharedPrefsHelper.getInstance ().saveQbUser (qbUser);
User user = User.getUserFromQbUser (qbUser);
App.Companion.setUser (user);
//Send user to loginFragment via PracticePhrasesActivity
Intent intent = new Intent (RegisterActivity.this, PracticePhrasesActivity.class);
intent.putExtra ("CONNECT", CONNECT);
startActivity (intent);

});
}

我已经为实时数据库设置了如下规则:

{ "rules": {
"users" : { 
"$uid" : {
".read": "$uid === auth.uid",
".write":"$uid === auth.uid"
}}}}

我面临的问题是,用户注册权限被拒绝的Firebase与上述规则。

任何帮助都会很感激。不用说,我是个新手,一直在自学。

应用程序。Kt有这个方法

companion object {
private lateinit var instance: App
private lateinit var mUser: User
fun getInstance(): App = instance
fun getUser(): User? = mUser

fun setUser(user: User) {
mUser = user
}

问题是我之前没有发布的代码。我已经创建了一个方法'setValidUserName()'来验证是否已经使用了显示名。它试图在用户注册之前这样做,因此权限被拒绝。代码如下

submitButton.setOnClickListener (v -> {
//Get data
display_name = usernameText.getText ().toString ();
setValidUsername ();
String email = emailText.getText ().toString ();
String password = passwordText.getText ().toString ();
String chkPassword = reEnterPassText.getText ().toString ();
if (password.equals (chkPassword) && password.length () > 7 && currentUserName) {
register_user (email, display_name, password);
} else if (password.length () < 8) {
Toast.makeText (RegisterActivity.this, "Password must be at least 8 characters", Toast.LENGTH_SHORT).show ();
} else if (!password.equals (chkPassword)) {
Toast.makeText (RegisterActivity.this, "Passwords do not match", Toast.LENGTH_LONG).show ();
}
});
}
public void setValidUsername() {
DatabaseReference reference = FirebaseDatabase.getInstance ().getReference ();
reference.child ("users").orderByChild ("username").equalTo (display_name).addListenerForSingleValueEvent (new ValueEventListener () {
@Override
public void onDataChange(@NonNull @NotNull DataSnapshot snapshot) {
if (snapshot.exists ()) {
Log.d ("dataSnapshot", "dataSnapShot: " + snapshot.toString ());
ToastUtils.longToast ("The username already exists.nPlease use a different username.");
currentUserName = false;
} else {
currentUserName = true;
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});

}

我现在已经将这个检查移动到注册完成后,如果已经使用了display_name,我现在提示用户更改它。

相关内容

最新更新