[JAVA] Firebase Authentication Note

Server side

Initialization

Guide

FileInputStream refreshToken = new FileInputStream("path/to/refreshToken.json");

FirebaseOptions options = new FirebaseOptions.Builder()
    .setCredentials(GoogleCredentials.fromStream(refreshToken))
    .setDatabaseUrl("https://<DATABASE_NAME>.firebaseio.com/")
    .build();

FirebaseApp.initializeApp(options);

How to get tokens

"Generate new private key" from the setting screen of the console of firebase> service account

Use

Guide

// idToken comes from the client app (shown above)
FirebaseToken decodedToken = FirebaseAuth.getInstance().verifyIdToken(idToken);
String uid = decodedToken.getUid();

Client side

Guide Document auth.signinwithemailandpassword

Login

firebase.auth().signInWithEmailAndPassword(email, password).catch(function(error) {
  // Handle Errors here.
  var errorCode = error.code;
  var errorMessage = error.message;
  // ...
}).then((userCredencial: UserCredential | void) => {
  // userCredencial.Use User as user
};

Request with token

Document User.getidtoken

firebase.auth().signInWithEmailAndPassword(email, password).catch(function (error) {
        // Handle Errors here.
    }).then((userCredencial: UserCredential | void) => {
        // https://firebase.google.com/docs/reference/js/firebase.auth.html?hl=ja#usercredential
        if (userCredencial != null) {
              userCredencial.user.getIdToken(true).then((idToken: string) => {
                    //Process of HTTP communication by including idToken in request header
              }
        }
    });

Recommended Posts

Firebase Authentication Note
Note
Note
Android Firebase Authentication (Facebook authentication) take-home memo
Firebase MLKit
[Note] Dockerfile
StringBuilder Note