【发布时间】:2017-06-08 06:10:57
【问题描述】:
情况:
当我加载一个页面但尚未登录时,一切正常,我只在导航中看到 Login 和 Register。
但是当我登录并加载任何页面时,导航会在"Register", "Log in", "Profile" and "Log out" 同时出现的奇怪状态下停留大约 0.5 到 1 秒。
然后导航显示应该是:仅显示"Profile" and "Log out"。
编辑:ng-cloack 已修复此问题,但我登录时不会出现个人资料和注销。
代码:
header.ejs
<html ng-app = "app">
<div ng-controller="ctrlHead">
<ul class="nav navbar-nav">
<li ng-show="!authenticated"><a href="/users/register">JOIN</a></li>
<li ng-show="!authenticated"><a href="/users/login">Login</a></li>
<li ng-show="authenticated"><a href="/users/profile">ME</a></li>
<li ng-show="authenticated" onclick="signOut();"><a>Logout</a></li>
</ul>
</div>
问题:
如何在我的 ng-show 中输入 localStorage 变量?
P.S.:header.ejs 是一个 EJS 部分,加载到所有页面上。
我尝试了什么:
这可行,但会在每个页面重新加载导航,导致闪烁。
app.factory("Auth", ["$firebaseAuth",
function($firebaseAuth) {
return $firebaseAuth();
}
]);
app.controller("ctrlHead", ["$scope", "Auth", "$window",
function($scope, Auth, $window) {
$scope.auth = Auth;
$scope.auth.$onAuthStateChanged(function(firebaseUser) {
if (firebaseUser) {
setAppCookie();
}
$scope.authenticated = firebaseUser;
});
}
]);
我尝试使用localStorage:
app.factory("Auth", ["$firebaseAuth",
function($firebaseAuth) {
return $firebaseAuth();
}
]);
app.controller("ctrlHead", ["$scope", "Auth", "$window",
function($scope, Auth, $window) {
$scope.authenticated = $window.localStorage.getItem("authenticated");
$scope.auth = Auth;
$scope.auth.$onAuthStateChanged(function(firebaseUser) {
if (firebaseUser) {
setAppCookie();
}
$window.localStorage.setItem("authenticated", firebaseUser);
$scope.authenticated = $window.localStorage.getItem("authenticated");
});
}
]);
现在ngCookies... 不起作用:/ 无论身份验证状态如何,导航都卡在登录模式。
app.factory("Auth", ["$firebaseAuth",
function($firebaseAuth) {
return $firebaseAuth();
}
]);
app.controller("ctrlHead", ["$scope", "Auth", "$cookies",
function($scope, Auth, $cookies) {
$scope.auth = Auth;
$scope.auth.$onAuthStateChanged(function(firebaseUser) {
if (firebaseUser) {
setAppCookie();
$cookies.put('authenticated', firebaseUser);
$scope.authenticated = $cookies.get('authenticated');
} else {
$cookies.put('authenticated', firebaseUser);
$scope.authenticated = $cookies.get('authenticated');
}
});
}
]);
编辑:我现在正在使用 angularfire,它工作正常,除了一件事,我需要将 authstate 存储在 localStorage 中。我一直在尝试使用 ng-storage 和 $window.localSorage 但它没有用......
【问题讨论】:
-
这是生活在某个地方吗?
-
@SergChernata 遗憾的是:/
-
你能做个小提琴吗?我可以帮你调试,但如果没有现场示例,很难说出发生了什么。
-
@SergChernata 嗯,让我看看我该怎么做。我需要以某种方式对你进行身份验证。
-
我不得不说,我无法重现这个问题:jsfiddle.net/wyus5d6a
标签: javascript angularjs firebase firebase-authentication angularfire