Firebase service account to generate authentication token for client-side use with Google Apps Script

前端 未结 1 1557
别跟我提以往
别跟我提以往 2020-12-16 08:29

I am having difficulty using the FirebaseApp (a 3rd party API) to generate an authentication token that can be passed to a sidebar and used by the client to login and access

相关标签:
1条回答
  • 2020-12-16 09:12

    Alright, so after a very long day I figured it out. I'm going to lay out what I ended up using for libraries and what the issue was (see the third library). The main problem was essentially that the tutorial was outdated and no a lot of people use Firebase in apps script.

    OAuth2 (Server-side) Link

    I didn't have to change anything here! It was working fine and never an issue.


    FirebaseApp (Server-side) Link

    This is a nice library and I stuck with it because it worked well (once I got it there). I had to make a change to my original code that came from the tutorial I mentioned. My code ended up like this and worked:

    if (service.hasAccess()) {   
        return FirebaseApp.getDatabaseByUrl(fb_URL, service.getAccessToken()) //get OAuth Token
                          .createAuthToken(Session.getEffectiveUser().getEmail(), null, serviceAccount.client_email, serviceAccount.private_key);
      //... Added the null, private key, and service email parameters.
    

    Firebase (Client-side) Link

    Alright, so this is where my main issue was -- The tutorial I followed for client-side setup was old. I had to upgrade the code on my own to use the new 3.x version:

    <script src="https://www.gstatic.com/firebasejs/5.8.2/firebase.js"></script>
    
    // Initialize Firebase
    var config = {
      apiKey: "<Web API Key>",
      authDomain: "<Project ID>.firebaseapp.com",
      databaseURL: "https://<DB URL>.firebaseio.com/"
      };
    
    firebase.initializeApp(config);
    

    With this firebase instance I was able to update my original authenticateClient() method:

    function authenticateClient(userRequestToken) {
      firebase.auth().signInWithCustomToken(userRequestToken).catch(function(error) {
        // Handle Errors here.
        console.error("authClient: ", error.code, error.message);
      });
    
      return {
        uid: firebase.auth().currentUser.uid,
        metadata: {
          lastSignInTime: firebase.auth().currentUser.lastSignInTime
        }
      }; 
    }
    

    That's it! I now have a firebase instance with a signed in user via JWT Custom Token! I came across a few people with similar issues an I hope this helps.

    0 讨论(0)
提交回复
热议问题