我目前正在开发用户 AngularFire 和 Firebase 的 AngularJS Web 应用程序。
我已经成功实现了 Firebase Facebook 身份验证功能,允许用户使用他们的个人 Facebook 帐户登录到 Web 应用程序。
我还有一个使用 Firebase 电子邮件/密码身份验证功能的注册表单。
问题
好的,所以问题是当用户使用注册表单进行注册时,一旦提交 - 表单不会重定向到破折号!我知道这是因为 $routeProvider > Resolve 或 .run 配置,但是我找不到正确的解决方案来让 Facebook 和注册表单相互配合。
任何和所有的建议都会有很大帮助!
谢谢你。
HTML
Auth<br/><br/>
<button ng-click="fblogin()">Facebook Login</button>
<br/><br/>
<form novalidate class="loginForm">
<input type="email" ng-model="loginForm.email" placeholder="Email" required/><br/>
<input type="password" ng-model="loginForm.password" placeholder="Password" required/>
</form>
<button ng-click="register()">Register</button>
<button ng-click="login()">Login</button>
应用程序.js
var fblogin = angular.module("fblogin", ["firebase", "ngRoute", "fbfactories", "fbcontrollers"]);
fblogin.config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {
$routeProvider
.when('/auth', {
templateUrl: 'views/auth.html',
controller: 'authCtrl',
resolve: {
"currentAuth": ["Auth", function(Auth) {
return Auth.$waitForAuth();
}]
}
})
.when('/dash', {
templateUrl: 'views/dash.html',
controller: 'dashCtrl',
resolve: {
"currentAuth": ["Auth", function(Auth) {
return Auth.$requireAuth();
}]
}
})
.otherwise({
redirectTo: '/auth'
});
}]);
fblogin.run(["$rootScope", "$location", function($rootScope, $location) {
$rootScope.$on("$routeChangeError", function(event, next, previous, error) {
if (error === "AUTH_REQUIRED") {
$location.path("/auth");
}
});
}]);
工厂.js
var fbfactories = angular.module('fbfactories', []);
fbfactories.factory("Auth", ["$firebaseAuth", function($firebaseAuth) {
var ref = new Firebase("https://courtyard-bridal.firebaseio.com/");
return $firebaseAuth(ref);
}]);
控制器.js
var fbcontrollers = angular.module('fbcontrollers', []);
fbcontrollers.controller('authCtrl', ['$scope', 'Auth', '$location', function($scope, Auth, $location) {
var ref = new Firebase("https://courtyard-bridal.firebaseio.com/");
$scope.auth = Auth;
$scope.user = $scope.auth.$getAuth();
// Facebook Login
$scope.fblogin = function() {
var scope = {
scope: 'email'
};
$scope.auth.$authWithOAuthPopup('facebook', scope).then(function(auth) {
// Redirtect on Success
$location.path('/dash');
}).catch(function(error) {
console.log('error');
});
};
// Form Data
$scope.loginForm = ({
'email': '',
'password': ''
});
// Register
$scope.register = function() {
// Form Data
var email = $scope.loginForm.email;
var password = $scope.loginForm.password;
// Create User
ref.createUser({
email: email,
password: password
}, function(error, userData) {
if (error) {
// Error
console.log("Error creating user:", error);
} else {
// Success
console.log("Successfully created user account with uid:", userData.uid);
$location.path('/dash');
}
});
};
}]);
fbcontrollers.controller('dashCtrl', ['$scope', 'Auth', '$location', function($scope, Auth, $location) {
$scope.auth = Auth;
$scope.user = $scope.auth.$getAuth();
// Facebook Logout
$scope.fblogout = function() {
$scope.auth.$unauth();
$location.path('/auth');
};
}]);