[英]Why User Logs In But is Not Authenticating Sporadically In Firebase?
So I have a weird bug that I can't seem to track down.所以我有一个奇怪的错误,我似乎无法追踪。 I'm using firebase functions on the backend and SwiftUI. My login flow goes like this:
我在后端使用 firebase 函数和 SwiftUI。我的登录流程是这样的:
User logs in from loginView.用户从 loginView 登录。 The loginView then uses a callback to pass a user to move on to the next View after a user logs in.
loginView 然后使用回调传递用户以在用户登录后转到下一个视图。
After this a user is passed to the View where it calls the firebase functions.在此之后,用户被传递到调用 firebase 函数的视图。
The problem is that every once in a while a user fails authentication.问题是每隔一段时间,用户的身份验证就会失败。 This doesn't happen every time and usually happens when a user has not logged in for 12 hours or more.
这种情况不会每次都发生,通常在用户 12 小时或更长时间未登录时发生。 I thought it may have been a race condition at first but after further investigation decided that it wasn't given the fact that it's using a callback.
一开始我认为这可能是竞争条件,但在进一步调查后决定没有给出它正在使用回调的事实。
Has anyone else experienced anything similar?有没有其他人经历过类似的事情? If so is there any way to fix this?
如果是这样,有什么办法可以解决这个问题吗?
I have tried making my firebase function stay warm by setting minimum instances to 1 because I initially thought it may be a cold start issue but this hasn't helped.我尝试通过将最小实例数设置为 1 来使我的 firebase function 保持温暖,因为我最初认为这可能是冷启动问题,但这并没有帮助。
Any help would be greatly appreciated.任何帮助将不胜感激。 Thanks.
谢谢。
On the frontend the code is pulling like so:在前端,代码是这样拉的:
FirebaseAuthService().signIn(email: email, password: password) { result, error in
if (error?.occurred) != nil {
self.errorMessage = error!.details
self.state = .failed
self.showErrorAlert = true
return
}
if (localAuthEnabled) {
...... This piece of code works
FirebaseFirestoreService().fetchUser(userID: result?.user.uid ?? "", completion: { user, error
....... This piece of code works.
}
})
}
User is then taken to another view AFTER logging in然后用户在登录后被带到另一个视图
This view pulls from 5 or so firebase functions asynchronously (but the user is already logged in by this point).此视图从 5 个左右的 firebase 异步函数中提取(但此时用户已经登录)。 The function that it fails at is as follows
失败的function如下
self.function.httpsCallable("api-report").call() { (result, error) in
... It is at times it gives me an auth error inside of this function.
}
I am using this to log out whenever a user put the app in the background or hits the log out button:每当用户将应用程序置于后台或点击注销按钮时,我都会使用它注销:
func signOut() -> Bool {
do {
try Auth.auth().signOut()
self.session = nil
return true
} catch let err as NSError {
print("Error signing out: %@", err)
return false
}
}
on the backend the report call does the following with the report.在后端,报告调用对报告执行以下操作。 This is a large function. I have only added the call to show whats going on.
这是一个很大的 function。我只是添加了调用来显示发生了什么。
exports.handler = async (data, context) => {
if (!context.auth) {
console.log("report error context");
console.log(context);
throw new functions.https.HttpsError('failed-precondition', 'The function must be called ' +
'by authenticated users.', 'User must be authenticated');
}
}
It seems the call to your backend may be happening just as/after the user is logged out, or their token is being refreshed.似乎对您的后端的调用可能发生在用户注销时/之后,或者正在刷新他们的令牌。 If it's the token being refreshed, the client should just retry the call in cases such as this.
如果是正在刷新的令牌,客户端应该在这种情况下重试调用。
You could also check whether the token is about to expire, and force a refresh when that is the case, or delay calling the Cloud Function until the token has refreshed.您还可以检查令牌是否即将过期,并在这种情况下强制刷新,或者延迟调用 Cloud Function 直到令牌刷新。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.