【发布时间】:2015-07-06 06:40:12
【问题描述】:
我从这个主题开始的原因是,在每次搜索与 Firebase 和 User Auth 相关的内容时,您看到的所有内容都会转到 SIMPLE LOGIN,它已经不推荐使用了。因此,我想消除一些疑问,并希望收到有关实施 Firebase 功能/方法的最佳方法的反馈。
如果我们提供带有 JS 和 HTML 部分的示例会更好。我将从我正在开发的应用程序的一些代码开始
首先创建新用户,登录和注销
//CREATE USER
<form name="sign-up">
<input type="text" ng-model="user.name">
<input type="email" ng-model="user.email">
<input type="password" ng-model="user.password">
<input type="password" ng-model="user.confirm">
<button ng-click="createUser(user)">
Create Account
</button>
<label ng-if="signUpErrorShow">
<span>{{signUpErrorMsg}}</span>
</label>
</div>
</form>
//LOGIN USER
<form name="login">
<input type="text" ng-model="user.email">
<input type="password" ng-model="user.pwdForLogin">
<button ng-click="signIn(user)">
LOGIN
</button>
<button ng-click="logOut(user)">
Logout
</button>
<label class="item item-text-wrap text-center" ng-if="signInErrorShow">
<span>{{signInErrorMsg}}</span>
</label>
</div>
</form>
这是用于创建部分和登录的 HTML,这里我有一个问题,除了电子邮件和密码之外,我添加了一个带有 user.name 的输入,用户在其中输入他的姓名,一旦登录,什么可以我会使用 Angular 插值显示他的名字吗?
这里是服务
angular.module('urbanet.app.services', [])
// create a custom Auth factory to handle $firebaseAuth
.factory("Auth", function ($firebaseAuth, $rootScope) {
var ref = new Firebase('https://urbanetapp.firebaseio.com/');
return $firebaseAuth(ref);
});
还有什么我需要补充的吗?
现在是控制器:
angular.module('urbanet.app.controllers', [])
.controller("LoginCtrl", function($scope, $rootScope, $ionicLoading, $ionicModal,
$timeout, $firebaseAuth, $state, $ionicPopup) {
var ref = new Firebase('https://urbanetapp.firebaseio.com/'),
auth = $firebaseAuth(ref);
$scope.signUpErrorShow = false;
$scope.signInErrorShow = false;
//CREATING USER
$scope.createUser = function(user) {
$scope.validationError = false;
if (user && user.email && user.name ) {
if (user.password === user.confirm ) {
auth.$createUser({
email: user.email,
password: user.password
}).then(function (userData) {
ref.child("users").child(userData.uid).set({
email: user.email,
displayName: user.name
});
}).catch(function (error) {
alert("Error: " + error);
$ionicLoading.hide();
});
$ionicPopup.show({
template: 'Succesfully created',
scope: $scope,
buttons: [
{
text: 'Accept',
onTap: function() {
$state.transitionTo('tabs.news');
}
}
]
});
}else {
$scope.signUpErrorMsg = "Error confirming pass";
}
}else {
$scope.signUpErrorMsg = "Required field";
}
};
//LOGIN USER
$scope.signIn = function (user) {
$scope.signInErrorShow = false;
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.displayName = val;
});
});
}).catch(function (error) {
$ionicPopup.alert({
title: 'Error entering',
template: "Auth failed " + error.message
});
});
} else
$scope.signInErrorShow = true;
$scope.signInErrorMsg = 'E-mail & pass required'
};
// LOG OUT USER
$scope.logOut = function() {
ref.unauth();
};
});
现在,我的代码有问题吗?这里有些奇怪的是,一旦我创建了用户,我就会在浏览器的网络部分看到返回的数据,一旦我登录时也是如此,但是一旦我注销,我就看不到任何事情发生,这是正确的行为吗?
另外,这是我最需要你帮助的地方,我需要实现一种方法来重置密码。 Here are the docs for that,该功能如何工作?我需要先设置更改密码的方法吗?还是怎么做?
你们所有人的帮助对于其他人来说非常重要,不仅仅是我,正如我上面提到的,大多数网络用户的例子都与旧的firebase身份验证方法有关。
【问题讨论】:
-
您尝试了什么重置密码?因为您拥有的文档参考非常明确(并且是最新的):
ref.resetPassword({ email: "whatever@yourprovider.com" })
标签: angularjs authentication firebase