[英]example use of custom user claims of firebase documentation doesn't work
I've tried to follow this guide to create three roles that I need on client side to show different UIs 我尝试按照本指南创建客户端需要的三个角色,以显示不同的UI
https://firebase.google.com/docs/auth/admin/custom-claims https://firebase.google.com/docs/auth/admin/custom-claims
at first I initialize firebase on my public void run, at the main .java 首先,我在主.java的公共void运行中初始化firebase
FileInputStream serviceAccount = new FileInputStream("MyRoute\\serviceAccountKey.json");
FirebaseOptions options = new FirebaseOptions.Builder()
.setCredentials(GoogleCredentials.fromStream(serviceAccount))
.setDatabaseUrl("https://<DATABASE_NAME>.firebaseio.com/")
.build();
FirebaseApp.initializeApp(options);
then I use custom user claims on my User controller the same way as in the guide UserController.java 然后我在用户控制器上使用自定义用户声明,方法与指南UserController.java中的方法相同
@RequestMapping(value = "/student", method = RequestMethod.POST)
private User createStudent(@RequestBody UserDTO userDTO) {
//Create the new User
User user = new User();
user.setUserType(UserType.STUDENT);
user.setEmail(userDTO.getEmail());
user.setFirstName(userDTO.getFirstName());
user.setLastName(userDTO.getLastName());
//verify the user and create custom role
String idToken = userDTO.getIdToken();
Task<FirebaseToken> decoded = FirebaseAuth.getInstance().verifyIdToken(idToken);
Map<String, Object> claims = new HashMap<>();
claims.put("student", true);
String uid = decoded.getUid();
FirebaseAuth.getInstance().setCustomUserClaims(uid, claims);
return userService.save(user);
With two errors first of all I can't get uid with decoded.getUid()
like documentation says because it forces me to use Task instead FirebaseToken 首先有两个错误,我无法像文档中所述的那样使用
decoded.getUid()
获取uid,因为它迫使我使用Task而不是FirebaseToken
and secondly I can't use FirebaseAuth.getInstance().setCustomUserClaims(uid, claims);
其次,我不能使用
FirebaseAuth.getInstance().setCustomUserClaims(uid, claims);
because it says The method setCustomUserClaims(String, Map) is undefined for the type FirebaseAuth 因为它说对于类型FirebaseAuth未定义方法setCustomUserClaims(String,Map)
if I change setCustomUserClaims
to setCustomClaims
as eclipse suggests me I get the following error The method setCustomClaims(String, Map) from the type FirebaseAuth is not visible 如果我将
setCustomUserClaims
更改为setCustomClaims
因为日食提示我,我将收到以下错误:类型FirebaseAuth的setCustomClaims(String,Map)方法不可见
Is my first time with firebase so I'm at loss why this doesn't work 这是我第一次使用Firebase,所以我茫然为什么这不起作用
You are using an old version of the SDK. 您正在使用旧版本的SDK。
Task
API was removed in the 6.0.0 release. Task
API在6.0.0版本中已删除。 Blocking methods like setCustomUserClaims()
were added in the same release. 在同一版本中添加了诸如
setCustomUserClaims()
类的阻塞方法。 If you want to keep using the old version then following should work: 如果您想继续使用旧版本,则应执行以下操作:
ApiFuture<Void> result = FirebaseAuth.getInstance().setCustomUserClaimsAsync(uid, claims);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.