我正在使用Firebase和使用index.html上的侧面导航栏在AngularJS中制作单页应用。
登录后,我希望用户名应该在侧面导航栏上,但$ scope适用于控制器上的当前页面。
scotchApp.controller('mainController', function($scope) {
$scope.validateLogin = function()
{
var email = $scope.login.userName + "@xyz.co";
var password = $scope.login.password;
firebase.auth().signInWithEmailAndPassword(email, password).catch(function(error) {
$scope.message = "please enter the correct details";
});
firebase.auth().onAuthStateChanged(function(user)
{
if(user)
{
$scope.usermobile = $scope.login.userName;
window.location.assign("/#/equipment");
}
});
}
$scope.message = '';
});
答案 0 :(得分:0)
firebase.auth().currentUser
中提供了mainController
中的当前用户,因此then
中您不需要监听身份验证更改。
在您的代码中,每次用户尝试登录时都会附加一个新的侦听器,但您实际需要的是响应一次成功登录,因此只需在登录时添加firebase.auth().signInWithEmailAndPassword(email, password)
.then(function (user) {
console.log('the logged in user: ', user);
// Go to the other route here.
// It is recommended to use the router instead of "manually" changing `window.location`
})
.catch(function(error) {
$scope.message = "please enter the correct details";
});
:
while($row = $result->fetch_object()){
if($username == $row->username)
{
$checkPassword = password_verify($password,$row->password);
if($checkPassword ){
session_start();
$_SESSION['loggedIn'] = true;
$_SESSION['user'] = $row->username;
$_SESSION['admin'] = $row->admin;
$_SESSION['userpic'] = $row->userpic;
$_SESSION['displayname'] = $row->displayname;
$_SESSION['id'] = $row->id;
header("Location:music.php?success");
exit();
}
}
else{
$fail = true;
if($fail){
echo "<script>
alert('You typed in wrong password or username, please try again mate!');
window.location.href='music.php';
</script>";
}
}
}