[英]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-for
或fastly-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.