Varun Gupta
Varun Gupta

Reputation: 3112

Cloud function for firebase to redirect to a URL

I am trying to create a download endpoint for my mobile app. The app is available on app store and play store. I want to have a single URL that can be used by users to download the app on either an iOS device or an Android device. I was trying to find out if I can create a cloud function for firebase for this purpose. I was thinking of checking the user-agent for the request and based on whether the request came from an iOS device or an Android device or a web browser, redirect the user to an appropriate URL. I have verified that the device information is present in the user-agent header. For e.g.

"user-agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 10_2 like Mac OS X) AppleWebKit/602.3.12 (KHTML, like Gecko) Version/10.0 Mobile/14C89 Safari/602.1 AppEngine-Google; (+http://code.google.com/appengine; appid: s~gcf-http-proxy)"

I guess the next things to do was to redirect the user to the app store URL in the above case. I tried to use res.redirect but that doesn't work. That ends up redirecting the user to cloud-function-base-url/redirect-url instead of just redirect-url.

Below is the code for the cloud function which is just trying to redirect to www.google.com when the function is triggered from the browser.

exports.appDownload = functions.https.onRequest((req, res) => {
  console.log(stringify(req.headers))
  // This will eventually redirect to a store URL based on the phone in user-agent
  res.redirect('www.google.com');
});

This ends up redirecting the browser to base-url/www.google.com instead of just www.google.com and I get an error message saying Not Found. I wanted to redirect the browser to just www.google.com.

I wanted to check if it is possible to achieve the above using a cloud function for firebase.

Upvotes: 13

Views: 17866

Answers (2)

Andrii Rudavko
Andrii Rudavko

Reputation: 370

Recently the Firebase Team released the "Dynamic Links" feature, that does exactly what you want, and more.

Firebase Dynamic Links

Upvotes: 4

Doug Stevenson
Doug Stevenson

Reputation: 317372

The documentation for redirect() suggests that you have to give it a fully qualified URL. Note the scheme "https" is present here (you're just giving "www.google.com", which would be taken as a path relative to the current URL):

res.redirect('https://www.google.com/')

Upvotes: 32

Related Questions