如何在用户使用Firebase身份验证登录后重定向? [英] How to redirect after a user signs in with Firebase authentication?

查看:208
本文介绍了如何在用户使用Firebase身份验证登录后重定向?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在用户登录后重定向至其他网页?

目前,当用户登录时,数据会被检索到,但不会将用户重定向到其他网站。

我知道我应该使用'getRedirectResult',但是有人可以告诉我如何使用它,以及如何将用户重定向到不同的网页,维护检索到的用户数据。



我的javascript工作:

$ p $ function toggleSignIn(){
if(!firebase.auth()。currentUser){
// [START createprovider]
var provider = new firebase.auth.GoogleAuthProvider();
// [END createprovider]
// [START addscopes]
provider.addScope('https://www.googleapis.com/auth/plus.login');
// [END addscopes]
// [START signin]
firebase.auth()。signInWithPopup(provider).then(function(result){
//你可以使用它来访问Google API
var token = result.credential.accessToken;
//登录的用户信息
var user = result .user;
// [START_EXCLUDE]
document.getElementById('quickstart-oauthtoken')。textContent = token;
// [END_EXCLUDE]
})。catch (错误){
//处理错误
var errorCode = error.code;
var errorMessage = error.message;
//使用的用户帐户的电子邮件。
var email = error.email;
//使用的firebase.auth.AuthCredential类型
var credential = error.credential;
// [START_EXCLUDE]
if(errorCode ==='auth / account-exists-with-different-credential'){
alert(You have alrea dy注册了该电子邮件的另一个auth提供程序。);
//如果您在应用程序中使用多个授权提供程序,则应该在此处处理
//用户帐户的链接。

else if(errorCode ==='auth / auth-domain-config-required'){
alert(需要授权域配置);

else if(errorCode ==='auth / canceled-popup-request'){
alert(Popup Google sign in was cancelled);

else if(errorCode ==='auth / operation-not-allowed'){
alert(Operation is not allowed);

else if(errorCode ==='auth / operation-not-supported-in-this-environment'){
alert(在此环境中不支持操作);

else if(errorCode ==='auth / popup-blocked'){
alert(Sign in popup blocked blocked);

else if(errorCode ==='auth / popup-closed-by-user'){
alert(Google登录弹出被取消);

else if(errorCode ==='auth / unauthorized-domain'){
alert(Unauthorized domain);
}
else {
console.error(error);
}
// [END_EXCLUDE]
});
// [END signin]
} else {
// [START signout]
firebase.auth()。signOut();
// [END signout]
}
// [START_EXCLUDE]
document.getElementById('quickstart-sign-ing')。disabled = false;
// [END_EXCLUDE]
}


解决方案

如果您正在构建单页面样式的应用程序,则可能不需要重定向。相反,当用户登录时只需更改应用程序状态。但是,如果您想更改URL,则只需在成功回调中使用JavaScript设置窗口位置即可。例如:

  window.location ='/logged_in.html'

请注意,在新的页面上,您还需要侦听身份验证状态:

<$ p $ onAuthStateChanged(function(currentUser){
if(currentUser){
//用户登录后,现在可以引导功能
}
});

一般来说,如果您不使用构建您的应用程序需要在状态转换之间进行硬页面加载。一切都可以,理想情况下,应该使用JavaScript来管理,而不需要额外的页面加载。


How can I redirect to a different webpage after the user has signed in?

Currently when a user logs in, data gets retrieved however, it doesn't redirect the user to a different website.

I know I should use 'getRedirectResult', but can someone show me how to use it and how it redirect the user to a different webpage, maintaining the retrieved user data.

My javascript work:

function toggleSignIn() {
  if (!firebase.auth().currentUser) {
    // [START createprovider]
    var provider = new firebase.auth.GoogleAuthProvider();
    // [END createprovider]
    // [START addscopes]
    provider.addScope('https://www.googleapis.com/auth/plus.login');
    // [END addscopes]
    // [START signin]
    firebase.auth().signInWithPopup(provider).then(function(result) {
      // This gives you a Google Access Token. You can use it to access the Google API.
      var token = result.credential.accessToken;
      // The signed-in user info.
      var user = result.user;
      // [START_EXCLUDE]
      document.getElementById('quickstart-oauthtoken').textContent = token;
      // [END_EXCLUDE]
    }).catch(function(error) {
      // Handle Errors here.
      var errorCode = error.code;
      var errorMessage = error.message;
      // The email of the user's account used.
      var email = error.email;
      // The firebase.auth.AuthCredential type that was used.
      var credential = error.credential; 
      // [START_EXCLUDE]
      if (errorCode === 'auth/account-exists-with-different-credential') {
        alert("You have already signed up with a different auth provider for that email.");
        // If you are using multiple auth providers on your app you should handle linking
        // the user's accounts here.
      }
  else if (errorCode === 'auth/auth-domain-config-required') {
    alert("An auth domain configuration is required"); 
      }
      else if (errorCode === 'auth/cancelled-popup-request') {
          alert("Popup Google sign in was canceled");
      }
      else if (errorCode === 'auth/operation-not-allowed') {
          alert("Operation is not allowed");
      }
      else if (errorCode === 'auth/operation-not-supported-in-this-environment') {
          alert("Operation is not supported in this environment");
      }
      else if (errorCode === 'auth/popup-blocked') {
          alert("Sign in popup got blocked");
      }
      else if (errorCode === 'auth/popup-closed-by-user') {
          alert("Google sign in popup got cancelled");
      }
      else if (errorCode === 'auth/unauthorized-domain') {
          alert("Unauthorized domain");
      }
       else {
        console.error(error);
      }
      // [END_EXCLUDE]
    });
    // [END signin]
  } else {
    // [START signout]
    firebase.auth().signOut();
    // [END signout]
  }
  // [START_EXCLUDE]
  document.getElementById('quickstart-sign-ing').disabled = false;
  // [END_EXCLUDE]
}

解决方案

If you're building a single-page style application, you likely don't need to redirect. Instead you would just change your application state when the user logs in. If you do want to change the URL, however, you can simply set the window location using JavaScript in your success callback. For example:

window.location = '/logged_in.html'

Note that on the new page you will need to listen for the auth state as well:

firebase.auth().onAuthStateChanged(function(currentUser) {
  if (currentUser) {
    // the user is logged in, you can bootstrap functionality now
  }
});

In general Firebase applications on the web will work better if you don't structure your app to need hard page loads in between state transitions. Everything can and ideally should be managed using JavaScript without requiring an extra page load.

这篇关于如何在用户使用Firebase身份验证登录后重定向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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