简体   繁体   English

如何根据 Firebase 身份验证状态重定向用户?

[英]How to redirect a user based on Firebase Authentication status?

I would like to redirect users when they sign in with Github or others based on whether they are a new user or a returning user.我想根据用户是新用户还是回访用户在使用 Github 或其他人登录时重定向用户。 I'm having trouble accessing the isNewUser property referenced in this answer: How to differentiate signin and signup user in firebase using google auth?我无法访问此答案中引用的 isNewUser 属性: 如何使用 google auth在 firebase 中区分登录和注册用户?

I have a standard sign in function:我有一个标准的登录功能:

const signinWithGoogle = () => {
    return auth.signInWithPopup(googleProvider)
    .then((response) => {
        handleUser(response.user)
    })
}

This is the handleUser function:这是 handleUser 函数:

const handleUser = (rawUser) => {
    if (rawUser) {
        const currentUser = formatUser(rawUser)
        createUser(currentUser.uid, currentUser)
        setCurrentUser(currentUser)
        if (currentUser.providerData[0].isNewUser===true) {
            history.push("/onboarding")
        } else {
            history.push("/")
        }
        return currentUser
    }
    else {
        setCurrentUser(false)
        return false
    }
}

And this is formatUser:这是格式用户:

const formatUser = (user) => {
return {
    uid: user.uid,
    email: user.email,
    name: user.displayName,
    provider: user.providerData[0].providerId,
    avatar: user.photoURL,
}
}

Can anyone see what I'm doing wrong, please?任何人都可以看到我做错了什么吗?

Cheers, Matt干杯,马特

EDIT:编辑:

If we pass the response to the HandleUser function and console log response.additionalUserInfo.isNewUser we get 'true'.如果我们将响应传递给 HandleUser 函数和控制台日志 response.additionalUserInfo.isNewUser 我们得到“true”。 However, if we use that in our if statement, it seems to be ignored for some reason但是,如果我们在 if 语句中使用它,它似乎由于某种原因被忽略了

const handleUser = (response) => {
    if (response) {
        console.log("response: ", response.additionalUserInfo.isNewUser)
        const currentUser = formatUser(response.user)
        createUser(currentUser.uid, currentUser)
        setCurrentUser(currentUser)
        console.log('response', response)
        console.log('additional info', response.additionalUserInfo)
        const isNew = response.additionalUserInfo.isNewUser
        console.log('isNewUser', isNewUser)
        if (isNew) {
            console.log('redirecting to /onboarding')
            history.push("/onboarding")
        } else {
            console.log('redirecting to /')
            history.push("/")
        }
        return currentUser
    }
    else {
        setCurrentUser(false)
        return false
    }
}

EDIT 2: Here is the output from the console logs编辑 2:这是控制台日志的输出

在此处输入图片说明

That error is coming from the signInWithGithub function in the modal该错误来自模态中的 signInWithGithub 函数

async function signInGitHub() {
    try {
        await signinWithGitHub()
    }

    catch(err) {
        console.log("Error: ",err.code)
    }

    finally {
        closeModal();
    }
}

It looks like you are passing a User to that function and not the raw response.看起来您将User传递给该函数,而不是原始响应。 The isNewUser is present on the additionalUserInfo property. isNewUser存在于additionalUserInfo属性上。 Please try refactoring as shown below:请尝试重构,如下所示:

const handleUser = (rawUser) => {
    if (rawUser) {
        const currentUser = formatUser(rawUser.user)
        createUser(currentUser.uid, currentUser)
        setCurrentUser(currentUser)
        if (currentUser.additionalUserInfo.isNewUser) {
            history.push("/onboarding")
        } else {
            history.push("/")
        }
        return currentUser
    }
    else {
        setCurrentUser(false)
        return false
    }
}

Also make sure you pass the raw response:还要确保传递原始响应:

handleUser(response.user)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM