简体   繁体   中英

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

https://firebase.google.com/docs/auth/admin/custom-claims

at first I initialize firebase on my public void run, at the main .java

    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

@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

and secondly I can't use FirebaseAuth.getInstance().setCustomUserClaims(uid, claims); because it says The method setCustomUserClaims(String, Map) is undefined for the type FirebaseAuth

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

Is my first time with firebase so I'm at loss why this doesn't work

You are using an old version of the SDK. Task API was removed in the 6.0.0 release. Blocking methods like setCustomUserClaims() were added in the same release. If you want to keep using the old version then following should work:

ApiFuture<Void> result = FirebaseAuth.getInstance().setCustomUserClaimsAsync(uid, claims);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM