Skip to content

Instantly share code, notes, and snippets.

@eldarc
Last active October 7, 2021 18:46
Show Gist options
  • Select an option

  • Save eldarc/e3e7176370e98bc2c9d30592a436db29 to your computer and use it in GitHub Desktop.

Select an option

Save eldarc/e3e7176370e98bc2c9d30592a436db29 to your computer and use it in GitHub Desktop.

Revisions

  1. eldarc revised this gist Jul 7, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gfc-geolocation-basic-whitelist.js
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    const cors = require('cors')

    // Set `useWhitelist` to `false` if you wanted to accept all requests.
    // Set `useWhitelist` to `false` if you want to accept all requests.
    const config = {
    useWhitelist: true
    }
  2. eldarc revised this gist Jul 7, 2018. 1 changed file with 1 addition and 3 deletions.
    4 changes: 1 addition & 3 deletions gfc-geolocation-basic-whitelist.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,4 @@
    const cors = require('cors')
    const cityTimezones = require('city-timezones');

    // Set `useWhitelist` to `false` if you wanted to accept all requests.
    const config = {
    @@ -40,8 +39,7 @@ function _geolocation(req, res) {
    region: req.headers["x-appengine-region"],
    city: req.headers["x-appengine-city"],
    cityLatLong: req.headers["x-appengine-citylatlong"],
    userIP: req.headers["x-appengine-user-ip"],
    cityData: cityTimezones.lookupViaCity(req.headers["x-appengine-city"])
    userIP: req.headers["x-appengine-user-ip"]
    }

    res.json(data)
  3. eldarc created this gist Jul 7, 2018.
    57 changes: 57 additions & 0 deletions gfc-geolocation-basic-whitelist.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,57 @@
    const cors = require('cors')
    const cityTimezones = require('city-timezones');

    // Set `useWhitelist` to `false` if you wanted to accept all requests.
    const config = {
    useWhitelist: true
    }

    // Define from which origins requests are allowed.
    const whitelist = [
    // 'https://fiddle.jshell.net',
    'https://ministryofprogramming.com',
    'https://mop.ba'
    ];

    // Parse the whitelist and decide if the request is allowed.
    const corsOptionsWhitelist = function (req, callback) {
    var corsOptions;

    if (whitelist.indexOf(req.header('Origin')) !== -1) {
    corsOptions = { origin: true }
    } else {
    corsOptions = { origin: false }
    }

    callback(null, corsOptions);
    }

    // Options when not using the whitelist.
    const corsOptions = {
    origin: true
    }

    // Handle the response within this function. It can be extended to include more data.
    function _geolocation(req, res) {
    // res.header('Cache-Control','no-cache');

    const data = {
    country: req.headers["x-appengine-country"],
    region: req.headers["x-appengine-region"],
    city: req.headers["x-appengine-city"],
    cityLatLong: req.headers["x-appengine-citylatlong"],
    userIP: req.headers["x-appengine-user-ip"],
    cityData: cityTimezones.lookupViaCity(req.headers["x-appengine-city"])
    }

    res.json(data)
    };

    // Export the cloud function.
    exports.geolocation = (req, res) => {
    const corsHandler = config.useWhitelist ? cors(corsOptionsWhitelist) : cors(corsOptions);

    return corsHandler(req, res, function() {
    return _geolocation(req, res);
    });
    };