简体   繁体   English

Firebase Cloud Functions - 从 https 中的上下文获取来源可调用 function

[英]Firebase Cloud Functions - get origin from context in https callable function

I have a cloud function which sends a custom email verification email, which contains a link which redirects back to my website.我有一个云 function,它发送自定义 email 验证 email,其中包含一个重定向回我的网站的链接。 The problem I have is that when I am testing on localhost, the redirect URL goes to the production site.我遇到的问题是,当我在本地主机上进行测试时,重定向 URL 会转到生产站点。 I know this is the expected behaviour.我知道这是预期的行为。

Is there a way to use the context data that the https callable function recieves to get the origin URL to generate a more specific redirect URL?有没有办法使用 https 可调用 function 收到的context数据来获取来源 URL 以生成更具体的重定向 URL?

I cannot stringify the context object without getting a TypeError: Converting circular structure to JSON error, but when I log that to the console, in the cloud functions logs in the firebase console I see the origin URL deep in the JSON object.我无法在没有TypeError: Converting circular structure to JSON错误的情况下对context object 进行字符串化,但是当我将其记录到控制台时,在 firebase 控制台的云函数日志中,我在 JSON JSON 884028985 深处看到了起源 URL

Snippet from console:来自控制台的片段:

headers: 
      { host: 'us-central1-universal-alert.cloudfunctions.net',
        'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36',
        'transfer-encoding': 'chunked',
        accept: '*/*',
        'accept-encoding': 'gzip, deflate, br',
        'accept-language': 'en-GB,en-US;q=0.9,en;q=0.8,fr;q=0.7',
        authorization: 'Bearer eyJhbGciOiJSUzI1NiIsImtpZCI6ImZjMmM4YmIyNmE3OGM0M2JkODYzNzA1YjNkNzkyMWI0ZTY0MjVkNTQiLCJ0eXAiOiJKV1QifQ.eyJpc3MiOiJodHRwczovL3NlY3VyZXRva2VuLmdvb2dsZS5jb20vdW5pdmVyc2FsLWFsZXJ0IiwiYXVkIjoidW5pdmVyc2FsLWFsZXJ0IiwiYXV0aF90aW1lIjoxNTg5MjQ0MDg3LCJ1c2VyX2lkIjoiNjJwaGJmQTRvTVUyT3E5aVpzSm5zZXZNd2hNMiIsInN1YiI6IjYycGhiZkE0b01VMk9xOWlac0puc2V2TXdoTTIiLCJpYXQiOjE1ODkyNDQwODcsImV4cCI6MTU4OTI0NzY4NywiZW1haWwiOiJiZW5yc29tbWVyQGljbG91ZC5jb20iLCJlbWFpbF92ZXJpZmllZCI6ZmFsc2UsImZpcmViYXNlIjp7ImlkZW50aXRpZXMiOnsiZW1haWwiOlsiYmVucnNvbW1lckBpY2xvdWQuY29tIl19LCJzaWduX2luX3Byb3ZpZGVyIjoicGFzc3dvcmQifX0.luzWFCR3xr7OmbAp38_qXTlEv800_NMZUBqfa_DfHc5pM9i4CRBr5en3AjzRIjJP2AYF50Z3OE4c7LvnD5VPhEMLmDJcl8xwe_q0uzlMxIIEQdUANui0eFIEK__mFbZS9QmoBwFP4mBa2diuRaZsuUW0rJXkPCzweeiIvJoeGDfGnYnz1KKH1KuDdPKb5sFcHxKCnMCnyr61rDYDEfFbOwDrZBuC94gtrEQtwqb5ruy453CJk0v1wVU8NpbgMhOUvq4AHYCRtNFAqmDDKFNYr1WJTdx3Eqmcn2segocXj-MdQzT5FZtdpxtRad8SKA1FCI03vBYNQDL8rtwFaVFt_A',
        'content-type': 'application/json',
        forwarded: 'for="81.111.170.171";proto=https',
        'function-execution-id': 'b2egqxx6o36z',
        origin: 'http://localhost:3000',
        referer: 'http://localhost:3000/sign-up',
        'sec-fetch-dest': 'empty',
        'sec-fetch-mode': 'cors',
        'sec-fetch-site': 'cross-site',
        'x-appengine-city': 'chesham',
        'x-appengine-citylatlong': '51.709401,-0.612333',
        'x-appengine-country': 'GB',
        'x-appengine-default-version-hostname': 'uc2d66f008c134574p-tp.appspot.com',
        'x-appengine-https': 'on',
        'x-appengine-region': 'eng',
        'x-appengine-request-log-id': '5eb9f0ea00ff0d575c33c56a980001737e7563326436366630303863313334353734702d7470000133646535326134613634643339346235623664653837626165353130396466393a3230000100',
        'x-appengine-user-ip': '81.111.170.171',
        'x-cloud-trace-context': '17c316a639a7d55b4e52ddc72c5ae94f/11631072639755688744',
        'x-forwarded-for': '81.111.170.171',
        'x-forwarded-proto': 'https',
        connection: 'close' },
     rawHeaders: 
      [ 'Host',
        'us-central1-universal-alert.cloudfunctions.net',
        'User-Agent',
        'Mozilla/5.0 (Macintosh

Is the origin value here guaranteed and reliable?这里的原产地值有保障可靠吗? Is there a better way to implement this?有没有更好的方法来实现这个?

It looks like you can just send the value of context.rawRequest.headers.origin .看起来您只需发送context.rawRequest.headers.origin的值即可。 No need to send the entire object.无需发送整个 object。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从 Firebase 可调用 function(云功能)返回 stream 的正确方法? - Proper way to return stream from Firebase callable function (Cloud function)? Firebase 从应用上下文调用的云函数始终为 null - Firebase Cloud Functions called from app context is always null Firebase 可调用云 Function CORS 错误使用 Z035489FF8D092741943E4A83241F5 - Firebase Callable Cloud Function CORS Error using Firebase Emulator and Vite 来自 ESP8266 的 Firebase 云功能 POST HTTPS 请求 - Firebase cloud function POST HTTPS Request from ESP8266 Firebase 云函数中的聊天消息通知。 触发云时无法从 promise 获取数据 Function - Chat message notification in Firebase Cloud Functions. Can't get data from a promise when triggering a Cloud Function 如何从本地主机调用 firebase 可调用函数? - how to call firebase callable functions from localhost? 如何从云函数中的请求中获取参数? 火力地堡 - How to get parameters from request in cloud functions? Firebase 如何从 TypeScript 中的可调用 Firebase Cloud Function 向我的 Unity 应用程序发送自定义 object? - How can I send custom object from my callable Firebase Cloud Function in TypeScript to my Unity app? 如何从 Firebase 云调用异步函数 Function - How to call async functions from a Firebase Cloud Function 与 firebase 云函数中的 firebase firestore 交互 - Interacting with firebase firestore from firebase cloud functions
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM