问题描述
我在下面的使用火力建立认证。我可以创建用户提供的服务和控制器,但是他们注册不登录的新用户后。
I'm following thinkster's ang-news tutorial to build authentication using firebase. I can create users with a service and controller but after registering a new user they are not logged in.
我有一个 auth.js
服务:
app.factory('Auth', ['$rootScope', 'FIREBASE_URL', '$firebaseSimpleLogin', function($rootScope, FIREBASE_URL, $firebaseSimpleLogin){
var ref = new Firebase(FIREBASE_URL);
var auth = $firebaseSimpleLogin(ref);
var Auth = {
register: function(user){
return auth.$createUser(user.email, user.password);
},
signedIn: function(){
return auth.user !== null; // user signed in if user property is not null
},
login: function(user){
return auth.$login('password', user); // type of login
},
logout: function(){
auth.$logout();
}
};
$rootScope.signedIn = function(){
return Auth.signedIn();
};
return Auth;
}]);
auth.user始终保持为空。
auth.user always stays null.
这是我的控制器:
app.controller('AuthCtrl', ['$scope', '$location', 'Auth', function($scope, $location, Auth){
if (Auth.signedIn()){
$location.path('/')
}
$scope.register = function(){
Auth.register($scope.user).then(function(authUser){
console.log('authUser: ' + JSON.stringify(authUser))
$location.path('/');
})
}
}])
然后,我有一个 register.html
与电子邮件/密码的形式。它调用寄存器()
:
Then I have a register.html
with an email/password form. It calls register()
:
<form ng-submit='register()' novalidate>
<input type='email' placeholder='Email' class='form-control' ng-model='user.email'>
<input type='password' placeholder='Password' class='form-control' ng-model='user.password'>
<input type='submit' class='btn btn-success' value='Register'>
</form>
该用户在我火力地堡锻造获取生成,但我提交登记表后,注销按钮不会显示:
The users get generated in my Firebase Forge, but after I submit the register form, the Logout button does not show:
<ul class='nav navbar-nav navbar-right' ng-show='signedIn()'>
<li>
<a href='#' ng-click='logout()'>Logout</a>
</li>
</ul>
为什么 signedIn()
住宿falsey后,我创建的火力用户伪造?
Why does signedIn()
stay falsey after I create a user in the firebase forge?
推荐答案
创建用户不会自动登录他们(尽管它在旧版本angularFire的习惯)。
Creating a user does not automatically log them in (though it used to in an older version of angularFire).
更改您的控制器方法如下:
Change your controller method to look like:
$scope.register = function(){
Auth.register($scope.user).then(function(authUser){
console.log('authUser: ' + JSON.stringify(authUser))
Auth.login($scope.user);
$location.path('/');
})
}
这将创建在伪造的用户,然后一旦承诺解决,它会自动登录他们进来。
This will create the user in the Forge and then once that promise resolves, it will automatically log them in.
这篇关于不能显示$的createUser后注销按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!