javascript - AngularFire:恢复匿名 session

标签 javascript angularjs firebase angularfire firebase-security

来自Simple Web Login文档,它声称您可以通过执行以下操作恢复 session :

var auth = new FirebaseSimpleLogin(chatRef, function(error, user) {
  ...
});

是否有 AngularFire 的等效项?

$firebaseSimpleLogin 构造函数将 firebase 引用作为其唯一参数..

具体来说,我想要做的是匿名登录一次并在刷新整个页面时恢复登录。这可以实现吗?

最佳答案

是否保留您的身份验证 session 取决于您传递给登录方法的参数。无论您使用 FirebaseSimpleLogin 还是 $firebaseSimpleLogin 在这里都无关紧要。

var auth = $firebaseSimpleLogin(ref);
$rootScope.$on('$firebaseSimpleLogin:login', function(user) {
   console.log('logged in', user.uid);
});

// automagically logs in the next time you call $firebaseSimpleLogin() after page refresh
auth.$login('anonymous', { rememberMe: true });

// does not automagically log in
auth.$login('anonymous');

更新从 Angular 1.x 和 Firebase 2.x 开始,此语法和行为已更改。

来自 Firebase 文档中的 remember 属性:

If not specified - or set to default - sessions are persisted for as long as you have configured in the Login & Auth tab of your App Dashboard. To limit persistence to the lifetime of the current window, set this to sessionOnly. A value of none will not persist authentication data at all and will end authentication as soon as the page is closed.

var auth = $firebaseAuth(ref);
$firebaseAuth.$onAuth(function(user) {
   console.log('logged ' + (user? 'in' : 'out'), user && user.uid);
});

// stays logged in for length specified in the app dashboard
auth.$authAnonymously();

// stays logged in until the browser closes
auth.$authAnonymously({ remember: 'sessionOnly' });

// does not stay logged in on page refresh
auth.$authAnonymously({ remember: 'none' });

关于javascript - AngularFire:恢复匿名 session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22774871/

相关文章:

javascript - 如何使用键盘输入来做某事?

java - 译文从何而来? Java servlet、tomcat 和 Ext-Js Sencha 应用程序

javascript - 字符串文字未由双引号 + javascript 正确关闭

android - flutter : how i get data from firestore in List<String>?

security - 防止 "delete and update"一个 child 在火力基地

swift - Firebase 批量更新/删除

javascript - 滚动问题后的 prestashop 固定菜单

javascript - 错误 : [ng:areq] Argument 'CompanyCtrl' is not a function, 未定义

javascript - __proto__ javascript 中构造函数的无限嵌套

angularjs - 在哪里实现 Retangular 的扩展模型