简体   繁体   English

从 firebase 身份验证触发器获取用户的 IP 地址?

[英]Get user's IP address from firebase auth trigger?

How can we get the user's IP address when using functions.auth.user().onCreate(...) ?我们如何在使用functions.auth.user().onCreate(...)时获取用户的 IP 地址? Basically, I want to track the IP address from when a user visits our website and the IP address from when they sign up on the app for analytics purposes, unless someone knows a better way.基本上,我想跟踪用户访问我们网站时的 IP 地址和他们注册应用程序时的 IP 地址以进行分析,除非有人知道更好的方法。 Thanks.谢谢。

This is not possible in any Cloud Functions background trigger. 这在任何Cloud Functions后台触发器中都是不可能的。 If you want to attempt to record an IP address (which may not be very accurate or helpful, given that network traffic can go through proxies and VPNs), you will have to use an HTTP type trigger that was invoked from your app or web site running on the the user's computer or device. 如果要尝试记录IP地址(由于网络流量可以通过代理和VPN进行记录,可能不太准确或没有帮助),则必须使用从应用程序或网站调用的HTTP类型触发器在用户的计算机或设备上运行。 The request might include a header called x-forwarded-for or fastly-client-ip that contains an IP address. 该请求可能包含名为x-forwarded-forfastly-client-ip的标头,其中包含IP地址。 But this isn't documented, so there is no guarantee this will exist. 但这没有记录,因此无法保证会存在。

See also: How to get client IP address in a Firebase cloud function? 另请参阅: 如何在Firebase云功能中获取客户端IP地址?

The IP information is available from Firebase auth using the EventContext object from auth.user().beforeSignIn() as well as auth.user().beforeCreate() IP 信息可从 Firebase auth 使用来自 auth.user().beforeSignIn() 和 auth.user().beforeCreate() 的 EventContext object 获得

To use it, you'll first need to upgrade to Firebase Authentication with Identity Platform .要使用它,您首先需要升级到Firebase Authentication with Identity Platform It's available in both Spark & Blaze plans and can be enabled by going to 'Firebase Authentication -> Settings' in your project.它在 Spark 和 Blaze 计划中均可用,并且可以通过转到项目中的“Firebase 身份验证 -> 设置”来启用。

The docs give the following use-case for getting, checking, and blocking based on IP from auth.user().beforeSignIn() for example:例如,文档给出了基于 IP 从 auth.user().beforeSignIn() 获取、检查和阻止的用例:

exports.beforeSignIn = functions.auth.user().beforeSignIn((user, 
context) => {
  if (isSuspiciousIpAddress(context.ipAddress)) {
    throw new functions.auth.HttpsError(
      'permission-denied', 'Unauthorized access!');
  }
});

For more details and examples of what else is available in the EventContext, see Getting user and context information .有关 EventContext 中其他可用内容的更多详细信息和示例,请参阅获取用户和上下文信息

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

相关问题 在 Firebase 身份验证中获取用户的 IP 地址的方法? - A way to get the IP address of a user in Firebase Authentication? 从 firebase 控制台获取设备 IP 地址 - Get Device IP address from firebase console Android Firebase Auth - 获取用户照片 - Android Firebase Auth - Get User's Photo 如何在Firebase身份验证中获取用户列表 - How to get user's list in firebase auth 在 firebase auth 触发器上获取新创建用户的来源 - Get origin for newly created user on firebase auth trigger 是否可以从 http(s) 请求的发件人到 firebase rest747A5DA5ZE7DA05Z - Is it possible to get the public ip address from the sender of an http(s) request to the firebase rest api? |已解决| 从 Firebase 获取用户身份验证数据并将其添加到 Firebase DB - |SOLVED| Get User Auth data from Firebase & add it to Firebase DB Firebase身份验证-从其他用户的节点获取数据,但规则设置为“ .read”:“ $ uid === auth.uid” - Firebase authentication - get data from other user's node but rules are set to “.read”: “$uid === auth.uid” 如何从 Firebase Auth REST API 仅由 Z0C83F57C78731C7EB4A3 获取单个用户的数据 - How can I get a single user's data from Firebase Auth REST API by email only, not by idToken 从 Firebase Callable 函数获取用户 IP 地址 - Get users IP address from Firebase Callable function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM