成功认证后,Firebase Google signInWithRedirect不会重定向到指定页面 [英] Firebase Google signInWithRedirect not redirecting to specified page on successful authentication

查看:161
本文介绍了成功认证后,Firebase Google signInWithRedirect不会重定向到指定页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下问题需要一点帮助/解释.我正在为项目使用Vue和Firebase.

Little bit help/explanation needed with the following problem. I am using Vue and Firebase for my project.

我正在尝试通过调用 signInWithRedirect 来设置Firebase Google登录.身份验证本身可以工作,但是不完全符合我的要求.

I am trying to set up the Firebase Google sign-in by calling signInWithRedirect . Authentication itself works, however not exactly how I want it.

在进行身份验证后,我需要它重定向到/dashboard,但目前在短暂加载后它会重定向回到/login页面.

I need it to redirect to /dashboard after authentication takes place, but currently it redirects back to /login page after a short loading.

我知道我必须使用 getRedirectResult ,但是我不确定如何准确地调用它.如果有人可以解释我如何正确编写代码以获得所需结果,将不胜感激.

I understand that I have to use getRedirectResult, but I am not sure how exactly call it. Would be very grateful if someone could explain how I can write my code correctly in order to get the wanted results.

.signInWithRedirect(fb.googleProvider)
.then(credential => {
this.$store.commit("setCurrentUser", credential.user)
fb.usersCollection.doc(credential.user.uid).set({
}).then(() => {
this.$store.dispatch("fetchUserProfile")
this.updateGmailData()
this.$router.push("/dashboard")
}).catch(err => {
console.log(err)
})
}).catch(err => {
console.log(err);
});
},

我相信我的初始身份验证应该看起来像这样-

I believe that my initial authentication should look something like this -

googleLogin() {
fb.auth.signInWithPopup(fb.googleProvider)
}

然后,我应该为 getRedirectResult 设置功能,该功能应该将路由器推到/dashboard吗?

And after that I should set up the function for the getRedirectResult, which should push the router to the /dashboard?

如何进行重定向以等待getRedirectResult并在获得所需结果后重定向/dashboard.

How to make redirection to wait for the getRedirectResult and to redirect /dashboard after getting the needed results.

我不知道如何实现它.

推荐答案

signInWithRedirect将重定向回到完成登录的同一页面.然后,您可以在用户登录后使用getRedirectResult()onAuthStateChanged侦听器重定向到仪表板.

signInWithRedirect will redirect back to the same page where sign-in will be completed. You can then use getRedirectResult() or onAuthStateChanged listener to redirect to your dashboard after user is signed in.

在登录按钮点击处理程序中,致电:

In your sign in button click handler, call:

// This will redirect to IdP for sign-in.
firebase.auth.signInWithRedirect(fb.googleProvider);

然后在同一页面上,您可以设置一个侦听器:

Then on the same page, you can set a listener:

firebase.auth().onAuthStateChanged((user) => {
  if (user) {
    // User signed in, you can get redirect result here if needed.
    // ...
    this.$router.push("/dashboard");
    // ....
  } else {
    // Show sign in screen with button above.
  }
});

这篇关于成功认证后,Firebase Google signInWithRedirect不会重定向到指定页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆