简体   繁体   中英

How to get firebase userID from inside a firebase cloud function

My user is directed from my app to a website which eventually returns a response to a redirect uri. I have a cloud function in firebase which listen for that link and get the response. Now I would like to save that information along with the details of the firebase user who triggered the process? For example I would like to save the response in Firestore in a document names as the userID. How can I achieve it??

I have added a picture of my process flow在此处输入图像描述

Here is my function

exports.connectStripeStandardAccount = functions.https.onRequest((req, res) => {

  let authCode = req.query.code;
  //here I would like to get the uid of the user who triggered the website
  return res.send(authCode);

  });
});

Here is the code for step nr 1 (my app is written in flutter)

link is in this format link = https://connect.stripe.com/oauth/authorize?response_type=code&client_id=xxxxxxxxxxxx&scope=read_write

_launchURLWebsite(String link) async {
    if (await canLaunch(link)) {
      await launch(link);
    } else {
      throw 'Could not launch $link';
    }
  }

Thanks

From the different comments to your question, it appears that you are using the Stripe Connect OAuth Reference .

The documentation (link above) explains that you can add a state parameter to your request, which is "an arbitrary string value we will pass back to you".

So, by adding a state parameter to your URL, as follows

https://connect.stripe.com/oauth/authorize?response_type=code&client_id=xxxxxxxxxxxx&scope=read_write&state=theuidofyouruser

you will receive the value of state as a query string parameter in your Cloud Function, as explained here: https://stripe.com/docs/connect/oauth-reference#get-authorize-response

In the Cloud Function, you can get the value of state by doing req.query.state , see the Cloud Functions documentation on this point.

I think this should work for you. By using this method you will be able to call the user's uid from anywhere

Install the provider package ( https://pub.dev/packages/provider#-installing-tab- ) by adding it to you pubspec.yaml dependencies.

pubspec.yaml

dependencies:
  provider: ^3.1.0+1

Next, create a new dart file called auth.dart (you can name it anything you want..

Inside that file create a new class called Auth, like this:

import 'package:firebase_auth/firebase_auth.dart';

class Auth {

String userId;

final FirebaseAuth _auth = FirebaseAuth.instance;

String get userId {
    return _userId;
  }

void getUserID() async{
    FirebaseUser user = await _auth.currentUser();
    _userId = user.uid;
  }

}

Then in your main.dart file

import the Provider package by adding this import:

import 'package:provider/provider.dart';

and also in the main.dart file, right before your MaterialApp is returned ( return MaterialApp...)

Wrap it with a new widget like this:

Consumer<Auth>(
 builder: (ctx, auth, _) => MaterialApp(...),),

Now, inside any widget where you want to call the user's Id you can do this..

@override
  Widget build(BuildContext context) {

final auth = Provider.of<Auth>(context);

return Container(
 child: Text(auth.uid);
 );
}


The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM