Advertisement
FiringBlanks

Firebase Functions Code - Works on localhost but not in production

Aug 19th, 2022
1,091
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 3.56 KB | Software | 0 0
  1. "use strict";
  2.  
  3. Object.defineProperty(exports, "__esModule", { value: true });
  4. const functions = require("firebase-functions");
  5. const admin = require("firebase-admin");
  6. const { firestore } = require("firebase-admin");
  7. const Stripe = require("stripe");
  8. const moment = require('moment');
  9. const express = require('express');
  10. const app = express();
  11. const stripe = new Stripe(functions.config().stripe.testkey);
  12. const cors = require('cors')({origin: true});
  13. admin.initializeApp(functions.config().firebase);
  14. const SENDGRID_API_KEY = functions.config().sendgrid.key;
  15. const sgMail = require('@sendgrid/mail');
  16. sgMail.setApiKey(SENDGRID_API_KEY);
  17.  
  18.  
  19. app.all('*', function(req, res, next) {
  20.     const allowedOrigins = ['https://www.website.com','https://www.website.com/', 'http://localhost:4200'];
  21.     const origin = req.headers.origin;
  22.         console.log(origin);
  23.     if (allowedOrigins.includes(origin)) {
  24.         console.error('allowed')
  25.         res.setHeader('Access-Control-Allow-Origin', origin);
  26.     }
  27.     res.setHeader('Access-Control-Allow-Methods', 'POST, PUT, GET, OPTIONS');
  28.     res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Authorization, Content-Type, Accept");
  29.  
  30.     if (req.method === "OPTIONS") {
  31.         res.send('ok').status(200);
  32.         return
  33.      }    
  34.  
  35.     next();
  36. });
  37.  
  38.  
  39.  
  40. exports.createStripeCustomer = functions.https.onCall((data, context) => {
  41.  
  42.         console.log('in Cloud Functions > index.js > createStripeCustomer()');
  43.         //User auth data
  44.         const user = data.userObj;
  45.  
  46.         console.log('stripe testkey:');
  47.         console.log(functions.config().stripe.testkey);
  48.         console.log('user obj:');
  49.         console.log(user);
  50.         console.log('user.email:');
  51.         console.log(user.email);
  52.  
  53.         // Register Stripe user
  54.         return stripe.customers.create({
  55.             email: user.email
  56.         }).then((customerArg) => {
  57.             console.log(`Returned 'customer' object: `);
  58.             console.log(customerArg);
  59.             console.log('event.uid: ');
  60.             console.log(user.uid);
  61.             console.log('Saving customerID to users firestore at path: ');
  62.             console.log(admin.firestore().collection('Users').doc(user.uid).path);
  63.             //Save user's UID to customer in firestore 'Customers' collection (Change to batch when time permits)
  64.             return admin.firestore().collection('Customers').doc(customerArg.id).create({
  65.                 customerID: customerArg.id,
  66.                 userUID: user.uid,
  67.                 displayName: user.displayName,
  68.                 email: user.email,
  69.                 photoURL: user.photoURL,
  70.                 date: Date.now()
  71.             }).then(() => {
  72.                 console.log('Finished saving users UID to firestore Customers collection.');
  73.  
  74.                 setTimeout(() => {
  75.                     console.log('In setTimeout - saving customerID to user > uid in firestore');
  76.                    
  77.                     //Save customerID to user's firestore (Rewrite as batch)
  78.                     return admin.firestore().collection('Users').doc(user.uid).update({
  79.                         customerID: customerArg.id
  80.                     }).then(() => {
  81.                         console.log('Finished saving customerID to users firestore. Now saving users UID to Customer collection...');
  82.                     }).catch((err) => {
  83.                         console.error(err);
  84.                     });
  85.                 }, 5000);
  86.  
  87.             }).catch((err) => {
  88.                 console.error(err);
  89.             });
  90.         });
  91. });
  92.  
  93.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement