FlutterFlow / flutterflow-issues

A community issue tracker for FlutterFlow.
106 stars 18 forks source link

Regarding the JWT custom token for Firebase authentication. #3028

Closed Miyotch closed 1 month ago

Miyotch commented 1 month ago

Has your issue been reported?

Current Behavior

I have successfully integrated Piano ID, a third-party authentication provider, with FlutterFlow's Custom Authentication feature. However, when attempting to utilize Firebase as the backend database, I encountered an issue where CRUD operations fail unless the permission is set to "everyone." To address this limitation, I am considering switching to an authentication method based on Firebase's Custom Tokens.

I have configured a Cloud Function to convert these access tokens into custom tokens for Firebase authentication. However, deployment attempts result in an "Unknown Error." Despite retrieving the HTTP header, the response lacks detailed error information.

Expected Behavior

A user logs into a website that uses Piano for authentication. The Piano system generates an access token for the user. The user's browser then sends the access token to a Cloud Function. The Cloud Function exchanges the Piano access token for a Firebase custom token and returns the custom token to the browser. The browser then uses the custom token to authenticate with Firebase Authentication. The user is now granted access to the Firebase resources.

Steps to Reproduce

A third-party access token is converted to a Firebase custom token in the following Cloud Function.

ex) const admin = require('firebase-admin'); const functions = require('firebase-functions');

firebase.initializeApp();

exports.createCustomToken = functions.https.onCall((data, context) => { const uid = data.uid;

return admin.auth().createCustomToken(uid) .then(customToken => { return { customToken }; }) .catch(error => { console.error('Error creating custom token:', error); return { error }; }); }); image

Reproducible from Blank

Bug Report Code (Required)

IT8shfLlvJRgocdG0b6Ja8ZV9SAUQl87aucVku1+a0kbJuv2PZYHe+PCbVVuZ7C7a1w3CmeWmjsypq3Zv/PyGu1cNUyCbKlq3btcWwvMfHmtMsnXC6qrdWpfM5lRI0PEy8GZpAx9FNp2S1Fh3FyTf+ivVgnXQpP6IkdERPH/NrjNj0qhLDLNMCdW0iZecCfn

Context

I kindly request your assistance in troubleshooting the Cloud Function deployment issue and exploring the feasibility of implementing a Piano-like authentication system using Cloud Functions and Firebase.

Visual documentation

google_screen_recording_2024-05-31T12-47_12.077Z.webm

HTTP header image

Additional Info

Ref: JWT Token https://docs.flutterflow.io/data-and-backend/firebase/authentication/jwt-token

Custom token https://firebase.google.com/docs/auth/admin/create-custom-tokens?hl=ja

Piano ID https://piano.io/ja/product/id/

Environment

- FlutterFlow version: 4.1.54+
- Platform: Web
- Browser name and version: Chrome 125.0.6422.114
- Operating system and version affected: Windows11

General

Relative to the time the changes were made, data was lost within

When following my steps to reproduce, data loss happens

paulperez-dev commented 1 month ago

Hi @Miyotch! I was not able to access/create an account on PianoIO to test the integration. Did you follow all the steps from the first reference you shared? Are you using the Firebase Admin SDK? O creating a token following the specifications?

I've taken a look at your project and with a lot going on, It's challenging to pinpoint the issue.

image.png
Miyotch commented 1 month ago

Hi @paulperez-dev,

I have identified the issue with the action flow and have resolved it. The MISSING_IDENTIFIER error is no longer occurring. However, I have some additional questions. I made some changes to the Cloud Function that generates custom tokens for Firebase based on uids, but I'm getting an error when I try to deploy it. However, there is no clear error message, so I don't know what's causing the problem. Is there anything I can do from the GCP or Firebase console to check the deployment error?

image image

Cloud function:

const functions = require('firebase-functions'); const admin = require('firebase-admin'); admin.initializeApp();

exports.generateCustomToken = functions.https.onRequest(async (req, res) => { // UIDをリクエストから取得 const uid = req.query.uid; if (!uid) { res.status(400).send('UID is required'); return; }

try { // Custom Tokenを生成 const customToken = await admin.auth().createCustomToken(uid); res.status(200).send({ token: customToken }); } catch (error) { console.log('Error creating custom token:', error); res.status(500).send('Error creating custom token'); } });

Retrieval of HTTP headers yields only the following message: "Unknown error. Please contact support@flutterflow.io."

paulperez-dev commented 1 month ago

Because the reported issue is not a but, I'm going to close this issue.

Regarding the errors you are facing, are your firebase project in the Blaze plan?

Also consider the following auto-generated suggestions:

  1. View Logs: You can check the logs in the Firebase console to see if there are any error messages or stack traces that might give you more information about what went wrong during the deployment process. To view logs, navigate to your Firebase project in the Firebase console, then go to the "Functions" section. There should be a tab or option to view logs for your functions.
  2. Cloud Functions Logs: Additionally, you can view the logs for your Cloud Functions directly in the Google Cloud Console. Navigate to the Cloud Functions section of the Google Cloud Console, find your function, and view its logs there. Sometimes, more detailed error messages might be available in the Cloud Functions logs.
  3. CLI Deployment: Try deploying your Cloud Function using the Firebase CLI with the --debug flag. This will provide more detailed output during the deployment process, which might help you identify the cause of the error. Run the following command to deploy with debug output:

    firebase deploy --debug
  4. Check Dependencies: Make sure that all the dependencies required by your Cloud Function are correctly specified in your package.json file. Sometimes, deployment errors can occur if there are missing or incompatible dependencies.
  5. Review Code Changes: Double-check the changes you made to the Cloud Function code to ensure that there are no syntax errors, missing imports, or other issues that could cause deployment to fail.

By following these steps and examining the logs and deployment output, you should be able to identify the cause of the deployment error and take appropriate action to fix it.