我有一个应用程序,我正在使用Firebase身份验证,我想知道是否有办法:
1-用户在创建帐户后无需登录
创建帐户后,应该自动登录2-用户
这是为了提供更好的用户体验
这是我的代码
$scope.createUser = function(user) {
if (user && user.email && user.name ) {
if (user.password === user.confirm ) {
auth.$createUser({
name: user.name,
email: user.email,
password: user.password,
confirmPass: user.confirm
}).then(function (userData) {
ref.child("users").child(userData.uid).set({
name: user.name,
email: user.email,
displayName: user.name
});
}).catch(function (error) {
console.log(error);
});
};
这里是登录功能
$scope.signIn = function (user) {
if (user && user.email && user.pwdForLogin) {
auth.$authWithPassword({
email: user.email,
password: user.pwdForLogin
}).then(function (authData) {
ref.child("users").child(authData.uid).once('value', function (snapshot) {
var val = snapshot.val();
$scope.$apply(function () {
$rootScope.name = val;
$scope.userDisplayInfoName = $rootScope.name.displayName;
$scope.userDisplayInfoEmail = $rootScope.name.email;
});
});
}).catch(function (error) {
console.log(error);
});
}
};
答案 0 :(得分:2)
以下是我编写的UserService
的部分,显示createUser
方法,该方法会创建新用户,然后登录该新用户
$firebaseAuth
服务进行身份验证。User
工厂,extends the $firebaseObject
service。 userModule.service('UserService', function($firebaseAuth, FBREF) {
var currentUser;
var _authObj = $firebaseAuth(FBREF); // AngularFire authentication service
self = {
init: function() {
if (_authObj.$getAuth()) {
// Create User from factory, download user's data from Firebase
// If this is confusing, I can share the factory implementation as well
currentUser = User(_authObj.$getAuth());
// Bind currentUser's data to $rootScope.userData
currentUser.$bindTo($rootScope, "userData").then(function() {
console.debug("Bound userData: ", currentUser);
});
}
},
createUser: function(user, successCb, errorCb){
// Logout any currentUser before creating new user
self.logout();
// First, create a new user
_authObj.$createUser({
email: user.email,
password: user.password
// Then, authenticate the new user
}).then(function(userData) {
// By authenticating with password
return _authObj.$authWithPassword({
email: user.email,
password: user.password
});
// Finally, run various initialization steps
}).then(function(authData) {
self.init();
if(successCb) {
successCb();
}
// Catch an error if one is thrown
}).catch(function(error) {
console.error("Error: ", error);
if(errorCb) {
errorCb(error);
}
});
}
};
return self;
});
控制器实现可以看起来像这样:
userModule.controller('UserCtrl', function($scope, $state, UserService) {
var someErrorCallback = function() {...}
var someMissingInputCallback = function() {...}
$scope.signupWithPassword = function() {
// rudimentary way to check inputs exist, this should be changed
if($scope.incomingUser.email && $scope.incomingUser.password) {
UserService.createUser($scope.incomingUser, function() {
// successCb
$state.go('stateToGoToAfterSuccessfulLogin');
}, function(error){
// errorCb
console.error("Signup error:",error);
someErrorCallback();
});
} else {
someMissingInputCallback();
}
};
});
希望有所帮助。
如果有什么不清楚,我可以进一步解释。