注册新用户时,我会立即为用户访问控制设置一些值。
问题在于,我设置的数据仅在您创建身份验证的用户会话后注销后才显示在下一个会话中。
所以我的问题是,在创建帐户然后定义帐户后,我无法获得索偿。在Firebase身份验证中创建新用户后,我需要此数据。
auth.service.ts
package com.example.tastease;
import android.content.SharedPreferences;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.util.ArrayList;
public class CheckAvailability extends AppCompatActivity {
ArrayList<String> selectedItems = new ArrayList<>();
ArrayList<String> items = new ArrayList<>();
private String UserID;
public static final String MY_PREFS_NAME = "MyPrefsFile";
private DatabaseReference database;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_check_availability);
SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
UserID = prefs.getString("userid", "No ID defined");
ListView list1 = findViewById(R.id.check_list);
list1.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
database = FirebaseDatabase.getInstance().getReference();
DatabaseReference useref = database.child(UserID).child("Item Details");
final DatabaseReference checkref = database.child(UserID).child("Availability");
final ArrayAdapter<String> adapter = new ArrayAdapter<>(this,R.layout.rowlayout,R.id.txt_lan,items);
list1.setAdapter(adapter);
list1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String selectedItem = ((TextView)view).getText().toString();
if(selectedItems.contains(selectedItem)){
selectedItems.remove(selectedItem);
database.child(UserID).child("Availability").child(selectedItem).setValue("0");
}else {
selectedItems.add(selectedItem);
database.child(UserID).child("Availability").child(selectedItem).setValue("1");
}
}
});
checkref.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
if(ds.getValue().equals("1")){
items.add(ds.getKey());
adapter.notifyDataSetChanged();
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
public void getselectedItems(View view) {
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
tools:context=".CheckAvailability">
<ListView
android:id="@+id/check_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"></ListView>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn2"
android:layout_gravity="center"
android:text="Save Products"
android:onClick="getselectedItems"/>
</LinearLayout>
云功能
public user: any;
public firestoreCollection: string;
public admin: boolean;
constructor(private _angularFireauth: AngularFireAuth) {
this.getUser().subscribe((user: firebase.User) => {
this.user = user;
if (this.user) {
// You must logout() and then login() to get the claims.
user.getIdTokenResult().then(idTokenResult => {
this.firestoreCollection = idTokenResult.claims.firestoreCollection;
this.admin = idTokenResult.claims.admin;
});
}
});
}
public getUser(): Observable<firebase.User> {
return this._angularFireauth.authState;
}
public async createAccount(user: any): Promise<void> {
const auth = await this._angularFireauth.auth
.createUserWithEmailAndPassword(user.email, user.password);
const userObject = {
displayName: user.nome.trim(),
photoURL: 'assets/images/avatars/profile.jpg'
};
await auth.user.updateProfile(userObject);
const setUserControlAccess = firebase.functions()
.httpsCallable('setUserControlAccess');
await setUserControlAccess({
admin: true, assinante: true, profissional: true,
firestoreCollection: auth.user.uid,
email: auth.user.email
});
// I tried to re-authenticate to see if I could get claims.
await this.reauthenticateUser(user.email, user.password);
}
答案 0 :(得分:0)
如果您立即需要用户角色,请考虑使用不同于自定义声明的机制。通常,将此信息存储在数据库中可确保立即获得该信息并可以更好地扩展。自定义声明通常更适合用于波动性较小的事情,例如识别应用程序管理员。有关更多信息,请参见视频Five tips to secure your app。
也就是说,您可以强制reload the user's profile获取最新的令牌更改(包括声明)。如果这样不起作用,则用户必须注销然后再次登录。