我目前正在开发一个使用AngularFire和Firebase的AngularJS Web应用程序。
我已成功实施Firebase Facebook身份验证功能,允许用户使用其个人Facebook帐户登录Web应用程序。
我还有一个使用Firebase电子邮件/密码身份验证功能的注册表单。
问题
好的,问题是当用户注册时使用注册表单,一旦提交 - 表单不会重定向到破折号!我知道这是因为$ routeProvider>解析或.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>
App.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");
}
});
}]);
Factory.js
var fbfactories = angular.module('fbfactories', []);
fbfactories.factory("Auth", ["$firebaseAuth", function($firebaseAuth) {
var ref = new Firebase("https://courtyard-bridal.firebaseio.com/");
return $firebaseAuth(ref);
}]);
Controller.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');
};
}]);
答案 0 :(得分:0)
var fbcontrollers = angular.module('fbcontrollers', []);
fbcontrollers.controller('authCtrl', ['$scope', 'Auth', '$location', function($scope, Auth, $location) {
$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');
});
};
// Default Form Data
$scope.form = ({
'email': '',
'password': ''
});
// Login Form
$scope.login = function() {
var email = $scope.form.email;
var password = $scope.form.password;
$scope.authData = null;
$scope.auth.$authWithPassword({
email: email,
password: password
}).then(function(Auth) {
$scope.authData = Auth;
$location.path('/dash');
}).catch(function(error) {
console.log(error);
});
};
// Register (Create User) Form
$scope.register = function() {
var email = $scope.form.email;
var password = $scope.form.password;
// Create User
$scope.auth.$createUser({
email: email,
password: password
}).then(function(Auth) {
// Login Created User
$scope.authData = null;
$scope.auth.$authWithPassword({
email: email,
password: password
}).then(function(Auth) {
$scope.authData = Auth;
$location.path('/dash');
}).catch(function(error) {
console.log('error');
});
}).catch(function(error) {
console.log(error);
});
};
}]);
fbcontrollers.controller('dashCtrl', ['$scope', 'Auth', '$location', function($scope, Auth, $location) {
$scope.auth = Auth;
$scope.user = $scope.auth.$getAuth();
// Logout
$scope.logout = function() {
$scope.auth.$unauth();
$location.path('/auth');
};
}]);