How i can make a signup with cloud functions the firebase? - firebase

I'm learning to use the cloud functions and may create user with the method admin.auth().createUser, but now i would like make a sign up with account the google
I tried use admin.auth().GoogleAuthProvider() but tells me that propiety GoogleAuthProvider() not exist

It's not always possible use admin sdk. For example, I'm working with Google Actions and I've resolved with firebase dependencies (npm i --save firebase).
https://firebase.google.com/docs/auth/web/google-signin?hl=es-419
async function createUser(token: string) {
// Build Firebase credential with the Google ID token.
const credential = firebase.auth.GoogleAuthProvider.credential(token);
const userCredential = await firebase.auth().signInWithCredential(credential);
}

Related

How to import many users to firebase auth using flutter

I want to import 30 users using flutter to firebase, I couldn't find any sources to do so, the only thing I found out was this : https://firebase.google.com/docs/auth/admin/import-users ,
But I don't no how to implement this in my app
I wanted to store user data in a google docs and convert it into a ".csv" format and upload this from the flutter app to firebase ' Gmail password' authentication
The document you found uses the Firebase CLI. Unfortunately, you can't do this in dart/flutter. But if you want to implement it in flutter you can loop through a list of user objects.
Example:
List list = [{"email":"user1#example.com","Password":"strong password"},{"email":"use2r#example.com","Password":"strong password"},{"email":"user3#example.com","Password":"strong password"}];
final _auth = FirebaseAuth.instance;
list.forEach((element) async{
await _auth.createUserWithEmailAndPassword(email: element.email, password: element.password);
});
You can send a list of users to the firebase function or your backend service and perform an import like in the document you found.

Why this Firebase Cloud Function that create a new object into FireStore collection when a new user is created into Firebase Auth service goes wrong?

I am pretty new in Firebase and I have the following problem.
I need to create a new object into a Firestore collection named Users when a new user is registered on the Firebase Authentication service using a cloud function.
I defined and deployed this cloud function:
exports.newUserSignUp = functions.auth.user().onCreate(user => {
console.log("NEW USER CREATED: ", user);
var userObject = {
displayName : user.displayName,
email : user.email,
};
console.log("userObject: ", userObject);
let result = admin.firestore().doc('Users/').set(userObject);
return result;
});
The function is performed when a new user is created into Firebase Auth but I obtain the following error into my firebase cloud functions log:
Error: The default Firebase app does not exist. Make sure you call initializeApp() before using any of the Firebase services. at FirebaseAppError.FirebaseError [as constructor] (/srv/node_modules/firebase-admin/lib/utils/error.js:42:28) at FirebaseAppError.PrefixedFirebaseError [as constructor] (/srv/node_modules/firebase-admin/lib/utils/error.js:88:28) at new FirebaseAppError (/srv/node_modules/firebase-admin/lib/utils/error.js:123:28) at FirebaseNamespaceInternals.app (/srv/node_modules/firebase-admin/lib/firebase-namespace.js:101:19) at FirebaseNamespace.app (/srv/node_modules/firebase-admin/lib/firebase-namespace.js:452:30) at FirebaseNamespace.ensureApp (/srv/node_modules/firebase-admin/lib/firebase-namespace.js:468:24) at FirebaseNamespace.fn (/srv/node_modules/firebase-admin/lib/firebase-namespace.js:327:30) at exports.newUserSignUp.functions.auth.user.onCreate.user (/srv/lib/index.js:20:24) at cloudFunction (/srv/node_modules/firebase-functions/lib/cloud-functions.js:133:23) at /worker/worker.js:825:24
What is wrong? What am I missing? How can I fix it?
Assuming you are using the Admin SDK on the cloud functions server. All you need is this:
admin.initializeApp()
Make sure you stick this outside a function, then you will have full access to the admin SDK features, which is what you're trying to use.
The error is pointing that you need to initialize the SDK if you want to use Firebase, you can find detailed information about how to do it here, as is mentioned at the document:
"Once you have created a Firebase project, you can initialize the SDK with an authorization strategy that combines your service account file together with Google Application Default Credentials.
Firebase projects support Google service accounts, which you can use to call Firebase server APIs from your app server or trusted environment. If you're developing code locally or deploying your application on-premises, you can use credentials obtained via this service account to authorize server requests."
You should call initializeApp() before any other methods on the Admin object, for example:
admin.initializeApp({
credential: admin.credential.cert(servicesAccount),
databaseURL: "...",
});
const database = admin.firestore();

How do you read the userInfo of another user with Firebase's cloud functions?

I had in mind to create a cloud function that let a user read some of the user infos of another user under certaine conditions.
For example:
const user1 = ??? // user1 is the current user
const user1Data = await firestore().collection('Users').doc('user1.uid').get()
const user2 = ??? // user2 is the user whith user2.uid == user1Data.partnerUid
const user2Data = await firestore().collection('Users').doc('user2.uid').get()
if (user1Data.partnerEmail == user2.email && user1Data.partnerEmail == user2.email) {
// ...
// the endpoint deliver some of the user2 data to user1.
// ...
}
I have seen the documentation of Cloud functions:
https://firebase.google.com/docs/reference/functions/providers_auth_
I have seen that with the admin API we can call getUser:
admin.auth().getUser(uid)
The difference between functions.auth() and admin.auth() is not clear for me. Can we call admin within cloud functions ?
The difference between functions.auth() and admin.auth() is not clear for me.
When you import functions from firebase-functions, all that gets you is an SDK used for building the definition of functions for deployment. It doesn't do anything else. You can't access user data using functions.
When you import admin from firebase-admin, that gives you access to the Firebase Admin SDK that can actually manage user data in Firebase Authentication. You will want to use this to look up and modify users as needed, and it works just fine when running code in Cloud Functions.
The difference between functions.auth() and admin.auth() is not clear for me. Can we call admin within cloud functions ?
Basically functions.auth(), will let you trigger Cloud Functions in response to the creation and deletion of Firebase user accounts. For example, you could send a welcome email to a user who has just created an account in your app:
exports.sendWelcomeEmail = functions.auth.user().onCreate((user) => {
// ...
});
functions.auth() is from the cloud function package:
// The Cloud Functions for Firebase SDK to create Cloud Functions and setup triggers.
const functions = require('firebase-functions');
Using the above package you can preform firestore, database or auth triggers that will run in response to creating data in the database or creating a new user...
// The Firebase Admin SDK to access Cloud Firestore.
const admin = require('firebase-admin');
admin.initializeApp();
The firebase admin sdk is used to access the database from privileged environments example inside cloud functions.
Check the following links:
https://firebase.google.com/docs/functions/use-cases
https://firebase.google.com/docs/functions/auth-events
https://firebase.google.com/docs/admin/setup
https://firebase.google.com/docs/functions/auth-events

Search and update document on Firebase Cloud function

I'm trying to integrate Stripe with Firebase as a backend.
I need to create stripe customer when new user signs up to my app.
for that I wrote 1 cloud function which will execute when new user created by Firebase Auth.
but in that function I'm getting Firebase auth's user.
from that I need to update my collection's document which is created for that user. (I'm storing Auth user's uid in my collection's document).
but somehow Firebase cloud function is not updating it.
Here is my Cloud function for same.
// When a user is created, register them with Stripe
exports.createStripeCustomer = functions.auth.user().onCreate(async (user) => {
const customer = await stripe.customers.create({email: user.email});
return admin.firestore().collection('fl_users').doc(user.uid).set({customer_id: customer.id});
});
I don't know what need to update how. Can you please help me to solve my problem?

Cloud Functions for Firebase: can you detect app uninstall?

Is it possible to trigger a Cloud Function when the user uninstalls the app, so that we can clean up the anonymous user realtime database entry?
You can detect app uninstall for Android as an automatically collected Analytics event called app_remove. Then you could trigger a Cloud Function to run when that event occurs. You would also need to use the Firebase Admin SDK to access the database. Check out some of the Cloud Functions for Firebase GitHub samples to see examples of using Analytics triggers and using the Admin SDK. The function could work something like this:
exports.appUninstall = functions.analytics.event('app_remove').onLog(event => {
const user = event.user; // structure of event was changed
const uid = user.userId; // The user ID set via the setUserId API.
// add code for removing data
});

Resources