如何通过发布提交回调对身份验证方法
我正在实现用户注释功能,并且我想允许用户在要求身份验证以减少UX摩擦之前开始写作。
用户流将是:
- 用户单击输入字段,然后开始键入。
- 完成后,用户单击“提交”按钮,然后系统将检查用户是否经过身份验证。
- (a)如果用户已记录 在,然后将提交内容。 (b)如果用户未记录 在,打开登录/注册模式。
- (a)如果用户有帐户,则 选择电子邮件或社交登录方式。 (b)如果用户 是新的,他们通过电子邮件或社交注册了一个新帐户 帐户。
- 一旦用户进行身份验证,身份验证模式将关闭,然后将提交内容。 (用户无需再次单击“提交”按钮)
我认为这是一个非常常见的用例,但是我在Stackoverflow或Google搜索上找不到任何东西。
我正在使用Firebase身份验证的反应。我的第一个尝试是检查用户是否经过身份验证。
const submit = async (data) => {
if (!isAuthenticated) {
setOpenAuthDialog(true);
while (!isAuthenticated)
await new Promise((resolve) => setTimeout(resolve, 1000));
}
setDoc(doc(firestore, "comments", postId), data);
setInputField("");
};
但是,我认为这不是最好的做法,因为即使用户放弃身份验证流程,它也会继续检查。
我认为使用回调可能是更好的方法,但是身份验证模式和评论是同级组件。我不确定是否可以将回调函数传递给登录方法。
请让我知道是否有更好的方法解决此问题。
I'm implementing a user comments feature, and I'd like to allow users to start writing before asking for authentication to reduce UX friction.
The user flow would be:
- The user clicks on the input field, and start typing.
- Once done, the user clicks on the submit button, then the system will check if the user is authenticated.
- (A) If the user is logged
in, then it will submit the content.
(B) If the user is not logged
in, open a login/signup modal. - (A) If the user has an account, they
choose login methods either email or social login. (B) If the user
is new, they signup for a new account with either email or a social
account. - Once the user is authenticated, the authentication modal will close, and the content will then be submitted. (the user does not need to click on the submit button once again)
I think this is a very common use case, but I cannot find anything on Stackoverflow, or on Google search.
I'm using React with Firebase authentication. My first attempt is to check every second if the user is authenticated.
const submit = async (data) => {
if (!isAuthenticated) {
setOpenAuthDialog(true);
while (!isAuthenticated)
await new Promise((resolve) => setTimeout(resolve, 1000));
}
setDoc(doc(firestore, "comments", postId), data);
setInputField("");
};
However, I don't feel this is the best practice, because it will continue to check even when the user abandons the authentication flow.
I think using callbacks might be the better approach, but the authentication modal and comments are sibling components. I'm not sure if passing the callback function to the sign-in methods is possible.
Please let me know if there is an any better approach to this problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用回调的一种方法可能是:
One way to do it with callbacks could be: