如何通过发布提交回调对身份验证方法

发布于 2025-02-10 06:07:53 字数 864 浏览 0 评论 0原文

我正在实现用户注释功能,并且我想允许用户在要求身份验证以减少UX摩擦之前开始写作。

用户流将是:

  1. 用户单击输入字段,然后开始键入。
  2. 完成后,用户单击“提交”按钮,然后系统将检查用户是否经过身份验证。
  3. (a)如果用户已记录 在,然后将提交内容。 (b)如果用户未记录 在,打开登录/注册模式。
  4. (a)如果用户有帐户,则 选择电子邮件或社交登录方式。 (b)如果用户 是新的,他们通过电子邮件或社交注册了一个新帐户 帐户。
  5. 一旦用户进行身份验证,身份验证模式将关闭,然后将提交内容。 (用户无需再次单击“提交”按钮)

我认为这是一个非常常见的用例,但是我在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:

  1. The user clicks on the input field, and start typing.
  2. Once done, the user clicks on the submit button, then the system will check if the user is authenticated.
  3. (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.
  4. (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.
  5. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

静谧 2025-02-17 06:07:53

使用回调的一种方法可能是:

const submit = async (data) => {
    if (!isAuthenticated) {
        setOpenAuthDialog(true);
        setDataWaitingForSubmission(data)
        return;
    }
    setDoc(doc(firestore, "comments", postId), data);
    setInputField("");
}

// This is passed as callback to auth modal 
const onAuthSuccess = () => {
    if (dataWaitingForSubmission && isAuthenticated) {
        submit(dataWaitingForSubmission)
    }
}

One way to do it with callbacks could be:

const submit = async (data) => {
    if (!isAuthenticated) {
        setOpenAuthDialog(true);
        setDataWaitingForSubmission(data)
        return;
    }
    setDoc(doc(firestore, "comments", postId), data);
    setInputField("");
}

// This is passed as callback to auth modal 
const onAuthSuccess = () => {
    if (dataWaitingForSubmission && isAuthenticated) {
        submit(dataWaitingForSubmission)
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文