firebase 如何在google注册后重定向到主页?

2cmtqfgy  于 2022-12-04  发布在  Go
关注(0)|答案(1)|浏览(98)

我有Reactfirebase v9谷歌认证。
登入:

import { useNavigate } from 'react-router-dom';
const Login = () => {
const navigate = useNavigate();

 const signInWithGoogle = async () => {

    signInWithPopup(auth, provider)
      .then((result) => {
        const credential = GoogleAuthProvider.credentialFromResult(result);
        const token = credential.accessToken;
        const user = result.user;
        navigate('/'); // Redirect not work
      })
      .catch((error) => {
        const errorCode = error.code;
        const errorMessage = error.message;
        const email = error.customData.email;
        const credential = GoogleAuthProvider.credentialFromError(error);
      });
  };

 ....

  <Button variant="dark" onClick={signInWithGoogle}>
    Google
  </Button>
...

如何在google注册后重定向到主页?
更新:问题

const credential = GoogleAuthProvider.credentialFromResult(result);
    const token = credential.accessToken;
    const user = result.user;
zrfyljdw

zrfyljdw1#

您可以使用react-router-dom提供的useNavigate钩子在成功通过Google身份验证后重定向到主页。
下面是一个示例,说明如何在Google身份验证后重定向到主页:

import { useNavigate } from 'react-router-dom';
const Login = () => {
const navigate = useNavigate();

 const signInWithGoogle = async () => {

    signInWithPopup(auth, provider)
      .then((result) => {
        const credential = GoogleAuthProvider.credentialFromResult(result);
        const token = credential.accessToken;
        const user = result.user;
        navigate('/'); // Redirect to home page
      })
      .catch((error) => {
        const errorCode = error.code;
        const errorMessage = error.message;
        const email = error.customData.email;
        const credential = GoogleAuthProvider.credentialFromError(error);
      });
  };

 ....

  <Button variant="dark" onClick={signInWithGoogle}>
    Google
  </Button>
...

认证成功后,then块将被执行,它将调用navigate函数,以主页的路径作为参数,将用户重定向到主页。

相关问题