Coder Social home page Coder Social logo

Comments (56)

cdock1029 avatar cdock1029 commented on June 12, 2024 32

Got this to work for me previously:

https://gist.github.com/cdock1029/9f3a58f352663ea90f8b9675412c4aea

const app = require('express')();

// define your routes, etc ...

exports.route = functions.https.onRequest((req, res) => {
	
	// https://some-firebase-app-id.cloudfunctions.net/route
	// without trailing "/" will have req.path = null, req.url = null
	// which won't match to your app.get('/', ...) route 
	
	if (!req.path) {
		
		// prepending "/" keeps query params, path params intact
		req.url = `/${req.url}`
	
	}
	
	return app(req, res)

});

from firebase-functions.

anlexN avatar anlexN commented on June 12, 2024 6

@team:

  1. why not to discuss here, it is very convenient.
  2. i make sure my expressjs is very good and successfully deployed, but also "Cannot GET null", why decrease my/our quota? it's your firebase bug. not my/our developer, customer wrong
  3. i am very very very angry! 😠 😠 😠

from firebase-functions.

Mistobaan avatar Mistobaan commented on June 12, 2024 5

I arrived here from the comment in the example-functions, getting the 'apply' error and using the Router object. I fixed it using @cdock1029 patch.

from firebase-functions.

cdock1029 avatar cdock1029 commented on June 12, 2024 4

@sarovin that won’t work if you have query params in the request. Scroll up and use my solution which checks path and prepends / to the url.

from firebase-functions.

thechenky avatar thechenky commented on June 12, 2024 4

Hello everyone - this issue has now been fixed for all Node runtimes >= 8 - see the public https://b.corp.google.com/issues/122944969. Thanks for everyone's patience!

from firebase-functions.

themikefuller avatar themikefuller commented on June 12, 2024 3

Here is how I've been getting around the issue.

Lets say the deployed function should be myfunc:


  // index.js 

  var app = require('express')();

  // Home route or main functionality
  app.get('/',function(req,res,next) {
    res.send('home or root functionality');
  });

  // Some other route
  app.get('/something',function(req,res,next) {
    res.send('something else');
  });

  // Another file with express router and routes in it
  var routes = require('./routes');
  app.use(routes);

  // This allows the cloud function to be called with or without a trailing slash
  function App(req,res) {
    if (!req.url) {
      req.url = '/';
      req.path = '/';
    }
    return app(req,res);
  }

  // Export myfunc or whatever you want to call your function
  exports.myfunc = App;

from firebase-functions.

Wison-PF avatar Wison-PF commented on June 12, 2024 2

Hey everybody, I just solved this problem under somebody's help, and like to share back to everyone, the key to solve this issue perfectly is that you need to fix the req.url BEFORE express app handler is called. If your fix code inside the express app handler, it has no chance to be called when missing end / 404 error happen AFTER deployed (but it works fine when you testing locally by running firebase serve --only hosting,functions or firebase experimental:functions:shell), here the hero's message on Firebase Slack - functions channel

Also I paste the screenshot here, make sure you can see that if not join the Firebase Slack - functions channel yet:

screen shot 2017-11-02 at 10 48 37

from firebase-functions.

themikefuller avatar themikefuller commented on June 12, 2024 2

@anlexN

Why are you angry?

Did you try the workaround I provided above? You can just splint that in front of your express app and it will work as expected (until a permanent fix is applied by Google Cloud). And even after that, it will continue to work because it only addresses the issue if the issue is present.

The http triggered Google Cloud Functions aren't providing an express app, its providing a node.js request/response endpoint. Express is used under the hood to manipulate the req/res before you get it in your function, but the service isn't actually intended to behave like an Express.Router. We are sort of bolting that functionality on top of it.

You might notice that body parsing is already done for you as well... Again, they are providing an endpoint and the server processing the routing is using STRICT routing, thats why leaving the slash off doesn't work.

For the Google Cloud Functions team to fix this, they will probably need to disable STRICT routing in the enclosure that actually provides us with the req/res endpoint.

In the meantime, try this pattern: #27 (comment)

from firebase-functions.

nicolasgarnier avatar nicolasgarnier commented on June 12, 2024 1

this is caused by the empty value of req.url when hitting https://us-central1-app-name.cloudfunctions.net/functionName

Having read a bit online it seems that this is not an expected value for ExpressJS nor NodeJS. req.url should at least be '/':

expressjs/express#2281 (comment)

but why wouldn't you consider an empty string to be valid?

Because it's the contract of the req.url API from Node.js. The first line of an HTTP request is in the form HTTP/. req.url refers to whatever the exact string that appears in the portion and GET HTTP/1.1 is a syntax error (where the would resolve to an empty string). As such, we will not violate the expectation that req.url cannot be an empty string.

Basically req.url should never be an empty String. We should always have to to be '\' when requesting https://us-central1-app-name.cloudfunctions.net/functionName

And while we are at it, baseUrl and originalUrl should probably be '/functionName' instead of ''

Original report for reference: firebase/functions-samples#101

from firebase-functions.

themikefuller avatar themikefuller commented on June 12, 2024 1

@JulienMalige
You are welcome, and also correct. I found this link that details using a similar method with Express:
https://codeburst.io/express-js-on-cloud-functions-for-firebase-86ed26f9144c
See: "An Automated Solution"

It is very much what @cdock1029 shared previously. Seems req.path is not important for this, but I noticed that the example does not check if req.url IS defined, but missing the proceeding slash "/". That seems to make a difference later in routing when a query string is included.

One other note: While the functions emulator appears to allow HTTP DELETE methods with a request body, in production, the live cloud functions do not seem to acknowledge that DELETE methods might have a request body. Even reapplying the bodyParser middleware doesn't seem to work.

from firebase-functions.

nicolasgarnier avatar nicolasgarnier commented on June 12, 2024

By the way we get the same issue when using an Express router but the error message is Error: could not handle the request due to TypeError: Cannot read property 'apply' of undefined (that's what's in the logs)

from firebase-functions.

motss avatar motss commented on June 12, 2024

Just an update to this issue. Using express.Router still not solving this issue.

Simple example to reproduce the same issue:

const functions = require('firebase-functions');
const Router = require('express').Router;

const router = new Router();

router.get('*', (req, res) => {
  res.send('Hello world');
});

exports.helloWorld = functions.https.onRequest(router);

from firebase-functions.

BrodaNoel avatar BrodaNoel commented on June 12, 2024

Running in the same issue here.

from firebase-functions.

tbadalov avatar tbadalov commented on June 12, 2024

The same problem. Still not solved

from firebase-functions.

jthegedus avatar jthegedus commented on June 12, 2024

@turok1997 just do what @cdock1029 suggested above.

from firebase-functions.

sarovin avatar sarovin commented on June 12, 2024

I have fixed it here => https://github.com/sarovin/next-firebase-functions/blob/master/functionsES6/index.js#L9

from firebase-functions.

nareshbhatia avatar nareshbhatia commented on June 12, 2024

For me adding a slash at the end of the function name does not work. Instead I have to append /app/<functionName> like this:

https://us-central1-myapp.cloudfunctions.net/app/books

Note that I am using function name as a REST endpoint, so instead of getBooks it is simply books.

const expressApp = express();
expressApp.get('/books', getBooks);
expressApp.post('/books', createBook);

export const app = functions.https.onRequest(expressApp);

Does anyone see any problems in using firebase-functions this way?

from firebase-functions.

JulienMalige avatar JulienMalige commented on June 12, 2024

I know that it's not exactly the right place to ask it (tell me if it should be moved).

I meet the same issue with Google Cloud Functions (GCF), Cannot GET null because of the empty req.url. The @cdock1029 middleware seems to solve my issue but I miss one thing.

On GCF, I have to export an express app export const app = expressApp and I don't think that I am able to overwrite a kind of functions.https.onRequest() (because functions is imported from firebase-functions)...

I tried to use an Express Router middleware but it seems to late to intercept the request.

All ideas are welcomes. Thanks.

from firebase-functions.

nareshbhatia avatar nareshbhatia commented on June 12, 2024

@themikefuller, very creative!

However, doesn't that limit the entire express app to serve only one function? How would you serve multiple functions from the same express instance? In my case, I want to serve multiple functions with the same name based on the HTTP method called (REST style), so:

GET /books  = getBooks
POST /books = createBook
...

For the most part, I am able to achieve this (see my code above). I just don't like the extra app/ sitting in my URLs.

from firebase-functions.

cdock1029 avatar cdock1029 commented on June 12, 2024

@themikefuller

Have you checked if your solution works on this:

https://somedomains.com/myfunc/?foo=bar

If you scroll up, there is a workaround checking path parameter & prepending the / to the url

@themikefuller you can make requests to multiple paths by defining them as express routes

Anyone else seeing this, the problem is "solved"-ish, just read the whole thread..

from firebase-functions.

themikefuller avatar themikefuller commented on June 12, 2024

I should say that this is for Google Cloud Functions, as I saw it mentioned here. Not sure if it would work with firebase, I don't see why it wouldn't.

To answer your questions:
@nareshbhatia and @cdock1029

This works for me
https://us-central1-app-name.cloudfunctions.net/myfunc/?foo=bar
// req.query will be equal to { foo: "bar"}

and also I am able to use multiple routing methods and paths. It basically operates like a normal Express app when you add that in.

from firebase-functions.

nareshbhatia avatar nareshbhatia commented on June 12, 2024

@themikefuller, do you mean this comment from @cdock1029: #27 (comment)?

from firebase-functions.

themikefuller avatar themikefuller commented on June 12, 2024

@nareshbhatia

It was in reference to the comment about the query string. I think Express is being used before the function is loaded so the req.query does not seem to effected from what I can see.

I built a FaaS using VM before and was having this issue. I think Google Cloud Functions is just assuming the functions will be called with a trailing slash. I'll do some testing to verify that the query is preserved regardless if the function is called with the slash.

When you test locally with functions emulator, it doesn't distinguish the / and just works as expected. It's only live functions that the / is required ( unless using something like the solution I posted)

from firebase-functions.

nareshbhatia avatar nareshbhatia commented on June 12, 2024

@themikefuller, I think I see my confusion. You are talking about keeping the query parameters intact after the terminating slash whereas I am talking about something else.

My (minor) issue is regarding the formation of url's when creating an express app and handing it over to functions. I was expecting the URL to be:

https://us-central1-myapp.cloudfunctions.net/myfunc

But it is turning out to be (see the extra /app in the middle):

https://us-central1-myapp.cloudfunctions.net/app/myfunc

Perhaps my question should be on a separate thread (google forums may be).

from firebase-functions.

themikefuller avatar themikefuller commented on June 12, 2024

@nareshbhatia Well now that I'm testing, I'm seeing that ../myfunc?ok=something doesn't work.

.../myfunc/?ok=something does work because technically you are SUPPOSE to have a trailing slash when using a query string. So.. I @cdock1029 's comment here has the right idea.

The solution I posted works if there is no query string and no trailing slash. You will be taken to the corresponding '/' route in your express routes. If you leave off the trailing slash it still calls the function but it cannot find a corresponding express route for "" or null so it just shows the default express 404. Thats why manipulating the req.url makes it work without the trailing slash. It just fakes it out basically.

I'll keep testing and post my code when (if) I get an example that passes both tests.

from firebase-functions.

themikefuller avatar themikefuller commented on June 12, 2024

I think I got it. I made a cloud function live to test it. I don't want to leave it live forever, but I'll leave it going for a bit so anyone can try it out:

https://us-central1-dev-project-9000.cloudfunctions.net/App?q=cool
https://us-central1-dev-project-9000.cloudfunctions.net/App/?ok=awesome
https://us-central1-dev-project-9000.cloudfunctions.net/App/something?ok=good

All three of these scenarios work. Here is the code:

index.js


  // A function with an entry point App
  // index.js

  var app = require('express')();

  var routes = require('./routes');
  app.use(routes);

  function App(req,res) {
    if (!req.url || req.url[0] != '/') {
      req.url = '/' + req.url;
      req.path = '/' + req.path;
    }
    return app(req,res);
  }

  exports.App = App;

routes.js


  // routes.js (where your stuff can go)

  var app = require('express').Router();

  app.get('/',function(req,res,next) {
    res.json({
      "message":"this is home or whatever",
      "req.query":req.query,
      "req.path":req.path});
    });

  app.get('/users',function(req,res,next) {
    res.send('a list of users');
  });

  app.get('/users/:username',function(req,res,next) {
    res.send('a user named ' + req.params.username);
  });

  app.get('*',function(req,res,next) {
    res.json({
      "message":"this is something else",
      "req.query":req.query,
      "req.path":req.path});
    });

  module.exports = app;

from firebase-functions.

cdock1029 avatar cdock1029 commented on June 12, 2024

@themikefuller

Is there anything wrong with my solution from months ago, that uses less code?

Just wondering if it's not adequate for what you're trying to achieve? Also people keep asking the same question, so if we can agree on one that works, we don't have to keep working on new ones.

from firebase-functions.

themikefuller avatar themikefuller commented on June 12, 2024

@cdock1029 Does the solution you provided work with the three scenarios I posted?

...cloudfunctions.net/App?q=cool
....cloudfunctions.net/App/?ok=awesome
....cloudfunctions.net/App/something?ok=good

from firebase-functions.

cdock1029 avatar cdock1029 commented on June 12, 2024

Yes. Since the only "bug" this thread is dealing with is when /App has no trailing slash. (I was wrong with my question a few comments ago.. I meant to ask about the "no-trailing-slash" case).

In that case, path is empty, so the block will prepend a slash to the url, which won't erase any path or query params.

The other two cases won't be affected by the if block.

from firebase-functions.

themikefuller avatar themikefuller commented on June 12, 2024

@cdock1029
Maybe I misunderstood. I have been having some issues with this over the past few days.

I found your answer a few days ago and it worked initially, but there are some edge cases I ran into as well. Sometimes I get slightly different results in the emulator than I do live. I thought I'd share my code in case it helps someone.

If your answer works with and without the trailing slash then its perfect :)

from firebase-functions.

JulienMalige avatar JulienMalige commented on June 12, 2024

@themikefuller your solution almost do the job on Google Cloud Functions (@cdock1029):

  function App(req,res) {
    if (!req.url || req.url[0] != '/') {
      req.url = '/' + req.url;
      req.path = '/' + req.path;
    }
    return app(req,res);
  }

It gave me a mistake:
Error: could not handle the request (returned error)
TypeError: Cannot set property path of #<IncomingMessage> which has only a getter (logging)

So I deleted the req.path = '/' + req.path; line and now it's working (I think this is linked to the version of Express).

Thanks for help !

from firebase-functions.

zackify avatar zackify commented on June 12, 2024

@themikefuller I have found that the req.body is empty when doing a patch, but post works fine

from firebase-functions.

themikefuller avatar themikefuller commented on June 12, 2024

@zackify I noticed the same thing when sending a DELETE method with a request body. It works in the functions emulator. It does not work in live production.

Technically, an HTTP DELETE method is permitted to carry a request body. It appears that Cloud functions may not bother parsing the body in such circumstances. The bodyParser middleware could be re-run, though I can't say for sure that it would have the intended effect.

In any case, a req.body is expected on a PATCH method.

from firebase-functions.

jsguy avatar jsguy commented on June 12, 2024

Had the same problem, got it to work by using a match-all route instead of root, ie:

app.get('*', (req, res) => {
    res.status(200).send("hello.")
})

module.exports = functions.https.onRequest(app)

Hope it helps someone.

Note: I also tried cdock1029's trick to no avail.

from firebase-functions.

Herohtar avatar Herohtar commented on June 12, 2024

@cdock1029's solution works until you try to do a redirect from the root. For example, I have:

app.get('/', (req, res) => {
  // do stuff
  res.redirect('somewhere')
})

If someone navigates to https://firebase-app-id.cloudfunctions.net/route/ they'll end up redirected to https://firebase-app-id.cloudfunctions.net/route/something as intended, but if they instead navigate without the trailing slash (https://firebase-app-id.cloudfunctions.net/route) they'll end up at https://firebase-app-id.cloudfunctions.net/something.

Is there a way to make the redirect work?

from firebase-functions.

thechenky avatar thechenky commented on June 12, 2024

Hi everyone! for context, @kevinajian, @joehan and I have been taking over Lauren's work on firebase functions and I want to make sure this is still an issue people are having so that we can prioritize it properly. Can someone confirm that this still happens with the latest version of firebase-functions?

from firebase-functions.

Herohtar avatar Herohtar commented on June 12, 2024

@thechenky Yes, this issue still exists.

from firebase-functions.

themikefuller avatar themikefuller commented on June 12, 2024

It would be great if Cloud Functions implemented the req.path fix by default. I've been utilizing Cloud Functions more frequently in the past few months and this pattern has consistently worked for me:

Google Cloud Functions

const app = require('express').Router();

app.get('/', (req, res) => {
  res.send('home works with or without trailing slash');
});

app.get('/:test', (req, res) => {
  res.send(req.params.test);
});

exports.myfunction = (req, res) => {
  if (!req.path) {
    req.path = '/';
    req.url = '/' + req.url;
  }
  app(req, res);
};

Though its specifically for Google Cloud Functions, I think the Cloud Functions for Firebase equivalent should work as well:

Cloud Functions for Firebase

const app = require('express').Router();

app.get('/', (req, res) => {
res.send('home works with or without trailing slash');
});

app.get('/:test', (req, res) => {
  res.send(req.params.test);
});
  
let myfunction = (req, res) => {
  if (!req.path) {
    req.path = '/';
    req.url = '/' + req.url;
  }
  app(req, res);
};

module.exports = functions.https.onRequest(myfunction);

from firebase-functions.

thechenky avatar thechenky commented on June 12, 2024

Internal bug reference: 36851728

from firebase-functions.

thechenky avatar thechenky commented on June 12, 2024

@themikefuller @Herohtar thank you for the feedback and thanks everyone for the discussion here. This would be a change in Google Cloud Functions itself, not the Firebase Functions SDK. As such, we would need to file an issue in the Google Cloud functions public issue tracker.

In the meantime I will close this out as this github repo tracks issues specific to the firebase-functions SDK.

from firebase-functions.

thechenky avatar thechenky commented on June 12, 2024

I have gone ahead and created the issue on the public tracker and linked to this discussion: https://issuetracker.google.com/issues/122944969. Please follow that thread!

from firebase-functions.

joehan avatar joehan commented on June 12, 2024

Hey @anlexN, super sorry to hear that this issue has been affecting you. We moved the issue over to the public Cloud tracker so that the team who is able to fix it can see its impact and prioritize it. Have you tried the workaround suggested in #27 (comment)? It seems to be a good temporary solution until this issue is fixed.

from firebase-functions.

cheney-enterprises avatar cheney-enterprises commented on June 12, 2024

@themikefuller - I have attempted to use your solution, and it works when using the direct link to the function (https://us-central1-app-name.cloudfunctions.net/app/). However, when incorporating to my custom domain, it does not work. Do you have any suggestions?

*edit: a bit more clarification: I can make the https example request work (bigben) with my redirect, at the root, and custom domain. However, when attempting to use the express version, it does not work.

also, just a bit of extra detail, I am requiring only express const express = require('express') as adding .Router/.router/.Router()/.router() all fail upload to cloud functions, stating TypeError: Cannot read property 'method' of undefined in the express/router/index.js file.

*edit: I did use your example as well, no change. Still unable to require the .Router() extension

/*Also, my prepend fix is attached directly to the https export

exports.app = functions.https.onRequest((req,res) =>{ if (!req.path) { // prepend to keep query, path params req.url = /${req.url} } return app(req,res) });*/

Lastly, I am also forwarding to a subdomain, using redirects. However, it was not working prior to that either.

Any help you can provide, would be greatly appreciated!

from firebase-functions.

themikefuller avatar themikefuller commented on June 12, 2024

@cheney-enterprises
Hello. When you saying "forwarding to a subdomain"...

Can you explain your setup? Thinking out loud I can envision a few reasons why forwarding it might be a problem. Skipping all those assumptions, I'll explain how I got a custom domain working:

Using Nginx on a vps, I used proxy_pass to forward requests on that domain (subdomain) to that function's direct link. In this instance, the vps is acting sort of like a load balancer, and its passing along certain headers in the process. I can give you some examples if Nginx is your flavor. If not, its probably not worth bringing up.

Let me know :)

-m

from firebase-functions.

Sheshagiri avatar Sheshagiri commented on June 12, 2024

@themikefuller does it also work when you deploy the firebase functions locally using npm run serve?

from firebase-functions.

Herohtar avatar Herohtar commented on June 12, 2024

@thechenky I can confirm it works! Just a heads up, that link seems to take me to a login for an internal site, not the public issue.

from firebase-functions.

thechenky avatar thechenky commented on June 12, 2024

@Herohtar excellent, thanks for confirming! Glad to see it's working for you.

from firebase-functions.

 avatar commented on June 12, 2024

Hi everyone, I'm using Angular Universal + Firebase Function, all works fine except root path '', so can someone help me :) (i never deal with them ^ before)

firebase.json

...
"rewrites": [
  {
    "source": "**",
    "function": "ssr"
  }
]
...

functions/src/index.ts

import * as functions from 'firebase-functions';

const universal = require(`${process.cwd()}/dist/server.js`).app;

export const ssr = functions.https.onRequest(universal);

server.ts

// All regular routes use the Universal engine
app.get('**', (req, res) => {
  res.render('index', { req });
});

from firebase-functions.

cheney-enterprises avatar cheney-enterprises commented on June 12, 2024

I am not in front of a computer right now, but I have a couple things you can try off the top of my head:

My biggest suspicion is that it is your app.get call. Try changing that to a single *. I think that will fix your issue.

If not leave the single * on app.get and:
Try adding a β€˜/β€˜ before your ** in firebase.json

Let me know if either of these work.

from firebase-functions.

 avatar commented on June 12, 2024

I am not in front of a computer right now, but I have a couple things you can try off the top of my head:

My biggest suspicion is that it is your app.get call. Try changing that to a single *. I think that will fix your issue.

If not leave the single * on app.get and:
Try adding a β€˜/β€˜ before your ** in firebase.json

Let me know if either of these work.

Thank you, I tried your suggestions, but they didn't work, anyway, I figured out with another one's help :)

from firebase-functions.

themikefuller avatar themikefuller commented on June 12, 2024

@themikefuller does it also work when you deploy the firebase functions locally using npm run serve?

@Sheshagiri Its been awhile. I don't know, does it?

from firebase-functions.

urwa avatar urwa commented on June 12, 2024

@cdock1029's solution works until you try to do a redirect from the root. For example, I have:

app.get('/', (req, res) => {
  // do stuff
  res.redirect('somewhere')
})

If someone navigates to https://firebase-app-id.cloudfunctions.net/route/ they'll end up redirected to https://firebase-app-id.cloudfunctions.net/route/something as intended, but if they instead navigate without the trailing slash (https://firebase-app-id.cloudfunctions.net/route) they'll end up at https://firebase-app-id.cloudfunctions.net/something.

Is there a way to make the redirect work?

Hi,

I am facing this exact issue. What might I be missing? I am redirected to https://us-central1-xero-XXXXXX.cloudfunctions.net/redirect_url instead of https://us-central1-xero-XXXXXX.cloudfunctions.net/myfunction/redirect_url

from firebase-functions.

themikefuller avatar themikefuller commented on June 12, 2024

I have revisited this recently, using Cloud Functions and Firebase Functions. I have it working in both circumstances. I will post a full example of both soon. Stay Tuned!

from firebase-functions.

urwa avatar urwa commented on June 12, 2024

Looking forward. Thanks.

from firebase-functions.

urwa avatar urwa commented on June 12, 2024

Sharing my code here for reference. I am able to reach root without using a trailing / but the problem arises with redirection.

const xero_node = require('xero-node');
const TokenSet = require('openid-client');
const express = require('express');
const session = require('express-session');
var FileStore = require('session-file-store')(session);

require('dotenv').config();

const client_id = process.env.CLIENT_ID;
const client_secret = process.env.CLIENT_SECRET;
const redirectUrl = process.env.REDIRECT_URI;
const scopes = process.env.XERO_SCOPES;

if (!client_id || !client_secret || !redirectUrl) {
    throw Error('Environment Variables not all set - please check your .env file in the project root or create one!');
}

const xero = new xero_node.XeroClient({
    clientId: client_id,
    clientSecret: client_secret,
    redirectUris: [redirectUrl],
    scopes: scopes.split(' ')
});

const app = express();
// const port = 5000;

const fileStoreOptions = {}

app.use(session({
    name: '__session',
    secret: "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
    store: new FileStore(fileStoreOptions),
    resave: true,
    saveUninitialized: false,
    cookie: { 
        httpOnly: false,
        secure: false,
        maxAge: 86400
    }
  }));

app.use((req, res, next) => {
    res.header('Cache-Control', 'private');
    next();
});


app.get('/', async (req, res) => {

    if(req.session.tokenSet) {
        // This reset the session and required data on the xero client after ts recompile
        await xero.setTokenSet(req.session.tokenSet)
        await xero.updateTenants()
      }

    try {
        let consentUrl = await xero.buildConsentUrl();
        res.redirect(consentUrl);
    } catch (e) {
        console.log('There was an ERROR /! \n');
        console.log('ERROR: %s', JSON.stringify(e));
    }
});

app.get('/callback', async (req, res) => {

    try {
        const url = req.url;
        const tokenSet = await xero.apiCallback(url);
        await xero.updateTenants();
        req.session.tokenSet = tokenSet
        req.session.allTenants = xero.tenants
        req.session.activeTenant = xero.tenants[0];
        
        res.redirect('/listcontacts');
    } catch (e) {
        console.log('There was an ERROR! callback\n');
        console.log('ERROR: %s', JSON.stringify(e));
    }

});

app.get('/listcontacts', async (req, res) => {
    try {
        const response = await xero.accountingApi.getContacts(req.session.activeTenant.tenantId);
        res.set('Content-Type', 'text/html');
        res.write(`${response.body.contacts.length} label(s) found.`);
        res.status(200).end();
    } catch (e) {
        console.log('There was an ERROR! contacts\n');
        console.log('ERROR: %s', JSON.stringify(e));
    }
})

module.exports = {
    app
};

This is the error I get:
image

My callback redirect url is https://us-central1-xero-XXXXXX.cloudfunctions.net/xero-function/callback, which i think works fine. The problem is with the after the callback is successful the /listcontacts redirection is not working.

I would appreciate any help. Thanks.

from firebase-functions.

urwa avatar urwa commented on June 12, 2024

I even tried the workaround mentioned above and still not redirecting.

from firebase-functions.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.