2016-07-07 98 views
0

我有一個可以從各種設備登錄的Firebase應用程序,但是如果使用相同的帳戶製作新的應用程序,我想斷開其他連接。Firebase用戶已登錄

我看到這段代碼,但我認爲這可能是舊版本:

firebase.auth().onAuthStateChanged(function(user) { 
    if (user) { 
    // User is signed in. 
    } else { 
    // No user is signed in. 
    } 
}); 

這看起來是個好主意 - 如果調用此方法我可以顯示圖形的說法,「糟糕的樣子您在其他設備上登錄過。「然後發起斷開連接,同時允許另一個連接繼續?

回答

0

這不是您可以單獨使用auth處理的事情,因爲令牌是獨立生成和存儲的,並且沒有可以查詢的「設備會話」的概念。但是,你可以做這樣的事情:

var deviceId = generateARandomDeviceIDAndStoreItInLocalStorage(); 

firebase.auth().signInWithPopup(/* ... */).then(function(user) { 
    var userRef = firebase.database().ref('users').child(user.uid); 
    return userRef.update({ 
    deviceId: deviceId 
    }); 
}); 

firebase.auth().onAuthStateChanged(function(user) { 
    var userRef = firebase.database().ref('users').child(user.uid); 
    userRef.child('deviceId').on('value', function(snap) { 
    if (snap.val() !== deviceId) { 
     // another device has signed in, let's sign out 
     firebase.auth().signOut(); 
    } 
    }); 
}); 

重要的警告:這並不是保證一次只有一個設備登錄安全的,可執行的方式。相反,客戶端驅動的方式通常只能實現一個設備登錄的目標。