问题
I have no front end. I want users to use my API e.g (postman) with a basic auth header (email:password) and then using this header I can get the users data from firebase.
however I find no method to authenticate the actual user?
<dependency>
<groupId>com.google.firebase</groupId>
<artifactId>firebase-admin</artifactId>
<version>6.11.0</version>
</dependency>
I have correctly connected to DB using
FileInputStream serviceAccount =
new FileInputStream("path/to/serviceAccountKey.json");
FirebaseOptions options = new FirebaseOptions.Builder()
.setCredentials(GoogleCredentials.fromStream(serviceAccount))
.setDatabaseUrl("https://DBNAME.firebaseio.com")
.build();
FirebaseApp.initializeApp(options);
I have tried to look into the methods:
FirebaseAuth.getInstance().
FirebaseDatabase.getInstance().
but I see no options to sign in the user from my server...
seems very easy from nodejs server... e.g
firebase.auth().signInWithEmailAndPassword(email, password)
does this method exist in java/ how can I do this in java for server and no client??
回答1:
The way Firebase Auth generally works is like this. The client app should be signing in to get an ID token, and passing along that token to the backend for each call. The backend should verify the token received from the client with each request using the Firebase Admin SDK.
Typically, client apps will use one of the client SDKs for mobile and web apps. It will manage all the details of authenticating the user, getting a token, and refreshing the token every hour.
If you can't use one of the client SDKs, you will essentially have to write your own, or search the internet to see if someone else has already done it. If you write your own code, you will have to invoke the Firebase Auth REST APIs directly to get and refresh that token. You don't really have any alternatives to this with Firebase Auth - that's the way it was designed.
来源:https://stackoverflow.com/questions/59202816/java-firebase-admin-cannot-log-user-in-and-get-token-no-client