Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[🐛] Authentication not properly set on V2 Callable Cloud Function #6622

Closed
1 of 9 tasks
marcfrankel opened this issue Oct 21, 2022 · 13 comments · Fixed by #6626
Closed
1 of 9 tasks

[🐛] Authentication not properly set on V2 Callable Cloud Function #6622

marcfrankel opened this issue Oct 21, 2022 · 13 comments · Fixed by #6626
Labels
type: bug New bug report

Comments

@marcfrankel
Copy link

Issue

Apologies, if it's stated somewhere that V2 cloud functions are not yet supported in RNFirebase, but if they are I am having difficulty setting the auth param when calling a callable cloud function. According to the type definition, V2 functions should have an .auth property on the call request object that should have the uid and token. Everything else about the request is being correctly set, but not the auth property.

Please let me know though if V2 functions just aren't supported yet. And if so is there a timeline for it?


Project Files

Javascript

Click To Expand

package.json:

"@react-native-firebase/analytics": "16.1.1",
    "@react-native-firebase/app": "16.1.1",
    "@react-native-firebase/app-check": "16.1.1",
    "@react-native-firebase/auth": "16.1.1",
    "@react-native-firebase/crashlytics": "16.1.1",
    "@react-native-firebase/firestore": "16.1.1",
    "@react-native-firebase/functions": "16.1.1",
    "@react-native-firebase/in-app-messaging": "16.1.1",
    "@react-native-firebase/messaging": "16.1.1",
    "@react-native-firebase/perf": "16.1.1",
    "@react-native-firebase/remote-config": "16.1.1",
    "@react-native-firebase/storage": "16.1.1",

firebase.json for react-native-firebase v6:

{
    "$schema": "./node_modules/@react-native-firebase/app/firebase-schema.json",
    "react-native": {
        "analytics_auto_collection_enabled": false,
        "google_analytics_automatic_screen_reporting_enabled": false,
        "analytics_default_allow_ad_personalization_signals": false,
        "app_data_collection_default_enabled": false,
        "google_analytics_adid_collection_enabled": false,
        "crashlytics_auto_collection_enabled": false,
        "perf_auto_collection_enabled": false,
        "crashlytics_debug_enabled": false,
        "crashlytics_javascript_exception_handler_chaining_enabled": false,
        "in_app_messaging_auto_collection_enabled": false,
        "messaging_ios_auto_register_for_remote_messages": false,
        "messaging_auto_init_enabled": false,
        "android_task_executor_maximum_pool_size": 10,
        "android_task_executor_keep_alive_seconds": 3,
        "analytics_idfv_collection_enabled": false,
        "google_analytics_registration_with_ad_network_enabled": false
    }
}

iOS

Click To Expand

ios/Podfile:

  • I'm not using Pods
  • I'm using Pods and my Podfile looks like:
require File.join(File.dirname(`node --print "require.resolve('expo/package.json')"`), "scripts/autolinking")
require File.join(File.dirname(`node --print "require.resolve('react-native/package.json')"`), "scripts/react_native_pods")
require File.join(File.dirname(`node --print "require.resolve('@react-native-community/cli-platform-ios/package.json')"`), "native_modules")

require 'json'
podfile_properties = JSON.parse(File.read(File.join(__dir__, 'Podfile.properties.json'))) rescue {}

platform :ios, podfile_properties['ios.deploymentTarget'] || '12.4'
install! 'cocoapods',
  :deterministic_uuids => false

target 'SaidThatLOCAL' do
  use_expo_modules!
  config = use_native_modules!

  use_frameworks! :linkage => podfile_properties['ios.useFrameworks'].to_sym if podfile_properties['ios.useFrameworks']

  # Flags change depending on the env values.
  flags = get_default_flags()

  use_react_native!(
    :path => config[:reactNativePath],
    :hermes_enabled => flags[:hermes_enabled] || podfile_properties['expo.jsEngine'] == 'hermes',
    :fabric_enabled => flags[:fabric_enabled],
    # An absolute path to your application root.
    :app_path => "#{Dir.pwd}/.."
  )

  # Uncomment to opt-in to using Flipper
  # Note that if you have use_frameworks! enabled, Flipper will not work
  #
  # if !ENV['CI']
  #   use_flipper!()
  # end

  post_install do |installer|
    react_native_post_install(installer)
    __apply_Xcode_12_5_M1_post_install_workaround(installer)

    # This is necessary for Xcode 14, because it signs resource bundles by default
    # when building for devices.
    installer.target_installation_results.pod_target_installation_results
      .each do |pod_name, target_installation_result|
      target_installation_result.resource_bundle_targets.each do |resource_bundle_target|
        resource_bundle_target.build_configurations.each do |config|
          config.build_settings['CODE_SIGNING_ALLOWED'] = 'NO'
        end
      end
    end
  end

  post_integrate do |installer|
    begin
      expo_patch_react_imports!(installer)
    rescue => e
      Pod::UI.warn e
    end
  end
end

AppDelegate.m:

// N/A


Android

Click To Expand

Have you converted to AndroidX?

  • my application is an AndroidX application?
  • I am using android/gradle.settings jetifier=true for Android compatibility?
  • I am using the NPM package jetifier for react-native compatibility?

android/build.gradle:

// N/A

android/app/build.gradle:

// N/A

android/settings.gradle:

// N/A

MainApplication.java:

// N/A

AndroidManifest.xml:

<!-- N/A -->


Environment

Click To Expand

react-native info output:

    react-native: 0.69.6 => 0.69.6 
  • Platform that you're experiencing the issue on:
    • iOS
    • Android
    • [X ] iOS but have not tested behavior on Android
    • Android but have not tested behavior on iOS
    • Both
  • react-native-firebase version you're using that has this issue:
    • `16.1.1
  • Firebase module(s) you're using that has the issue:
    • Functions
  • Are you using TypeScript?
    • Y


@marcfrankel marcfrankel added help: needs-triage Issue needs additional investigation/triaging. type: bug New bug report labels Oct 21, 2022
@mikehardy
Copy link
Collaborator

🤔 I was not aware there was anything special we needed to do to support v2 cloud functions. I think v2 cloud functions are more about how you deploy them using the firebase CLI ?

https://firebase.google.com/docs/functions/beta

https://firebase.google.com/docs/functions/beta/get-started#import_the_firebase-functions_sdk

All I see with regard to v2 functions are things in the Node SDK / Admin SDK / CLI etc, I do not see any changes to the client SDKs, and here we only handle things in the client SDKs.

When I look in the client SDKs I see no functions v2 activity:
https://github.com/firebase/firebase-android-sdk/issues?q=is%3Aissue+functions+v2+
https://github.com/firebase/firebase-ios-sdk/issues?q=is%3Aissue+functions+v2+

Implying, to me, that there is nothing to do here, but you may work to do outside the react-native-firebase area, where you develop+deploy functions code to access v2 APIs / types.

Does that seem on target or am I missing something obvious + important?

@atanaskanchev
Copy link

#6543

@marcfrankel
Copy link
Author

I guess the main thing I would add is this is happening when using the emulator so I don't think deploying really plays a role at this point. And to my knowledge, my version for the emulators supports emulated v2 functions.

@marcfrankel
Copy link
Author

@atanaskanchev I saw that issue, but I wasn't really sure how to make heads or tails of it. Are you implying a fix is on that thread that I'm not quite understanding, or that it's a related issue?

@mikehardy
Copy link
Collaborator

mikehardy commented Oct 21, 2022

There is not (yet) a resolution on that one - the problem there is related to the URLs that the v2 functions are deployed on in the cloud, they are currently (during v2 functions public preview) on different URLs:

https://firebase.google.com/docs/functions/beta/callable#write_and_deploy_the_callable_function (3rd paragraph or so)

Until Cloud Functions v2 supports cloudfunctions.net URLs, you will have to use a different initializer in your client code. Rather than providing a function name, provide a full URL to the client SDK. The URL for your function is printed at the end of a successful firebase deploy command:

which requires use of an API that we don't have here yet, to put that URL in:

https://firebase.google.com/docs/functions/beta/callable#call_the_function

That is, httpsCallableFromURL is an API we need to support in order to actually call the function in the cloud. Per that issue it works on the emulator, but cloud no.

But this all begs the question: what's with the .auth property on the request? Unsure on that - if as the linked issue says, it works on the emulator, then I would assume (danger!) that if you had the auth emulator and functions emulator up, and you authenticated, then when you called a v2 function in the emulator, it should work. You just won't be able to access the deployed v2 function in the cloud yet, pending actual resolution on #6543

edit, the functions we need to wrap here are:

mikehardy added a commit that referenced this issue Oct 23, 2022
@mikehardy mikehardy removed the help: needs-triage Issue needs additional investigation/triaging. label Oct 23, 2022
@mikehardy
Copy link
Collaborator

I've implemented getCallableFromUrl and I think that will help move this along. Merging that PR will close this, but if it's still not working afterward we can always reopen

@trevor-rex
Copy link

trevor-rex commented Mar 21, 2023

Just to confirm, to call http onCall v2 functions, we are supposed to pass the full function endpoint to the httpsCallable method instead of just the function name? I am still running into an issue where I get [Error: too many HTTP redirects] when I try this. Currently, we have to manually generate a custom token and pass it as a bearer token through axios to make the request successfully.

Here is an example v2 endpoint for reference: https://function-v2slug-uc.a.run.app

edit: apologies, when I was reading through the changelog I assumed httpsCallableFromUrl was some internal method you were writing and not one exposed to the client I was supposed to call. I used this in place of httpsCallable and everything worked great. Thank you!

@mikehardy
Copy link
Collaborator

It is used like this - with full URL to the function endpoint:

describe('httpsCallableFromUrl()', function () {
it('Calls a function by URL', async function () {
let hostname = 'localhost';
if (device.getPlatform() === 'android') {
hostname = '10.0.2.2';
}
const functionRunner = firebase
.functions()
.httpsCallableFromUrl(
`http://${hostname}:5001/react-native-firebase-testing/us-central1/helloWorld`,
);
const response = await functionRunner();
response.data.should.equal('Hello from Firebase!');
});
});

@nechmads
Copy link

nechmads commented Jul 5, 2023

Anyone knows if this was resolved in the latest version?
Google released V2 of functions to production but when I try to call them, the auth parameter of the request is undefined.
(using onCall functions)

@TrustyTechSG
Copy link

@nechmads Im also facing the same issue, request.auth is undefined in v2 onCall.

@julienreszka
Copy link

@TrustyTechSG It's undefined for me too.
Maybe the documentation is out of date? cf : https://firebase.google.com/docs/functions/callable?gen=2nd
snippet from the documentation:

// Message text passed from the client.
const text = request.data.text;
// Authentication / user information is automatically added to the request.
const uid = request.auth.uid;
const name = request.auth.token.name || null;
const picture = request.auth.token.picture || null;
const email = request.auth.token.email || null;

Did you find a solution?

@trevor-rex
Copy link

Have you tried updating to the latest package and invoking the function with the httpsCallableFromUrl function?

import functions from '@react-native-firebase/functions'

const fn = async () => {
   const response = await functions().httpsCallableFromUrl(
        https://something.myCloudFunction.com,
        options,
      )(param)
}

I have had no auth issues with v2 functions using the above syntax.

@TrustyTechSG
Copy link

Hi @julienreszka, we didn't find a good solution beside @trevor-rex's solution above(thank you @trevor-rex) which require a client side update, but this solution is more feeling like a workaround than a official support for v2 function. So we decide to leave it for now (as we didn't notice any noticeable improvement upgrade from v1 to v2)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug New bug report
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants