我想在AngularJS和FireBase(admin,user)中进行基于角色的简单授权。 我做了基本授权和路由(看下面的3个文件)。
我在github和repository找到了article,但代码对我来说太难了。 有没有更简单的方法?如何更改代码以添加此功能? 我很感激能够帮助我的文章和知识库的链接。
app.js
var app = angular.module( 'journalApp', [ 'firebase', 'ngRoute' ] );
app.constant( 'FIREBASE', '<FIREBASE URL>' );
app.config( [ '$routeProvider', '$locationProvider', function( $routeProvider, $locationProvider ) {
$routeProvider.when( '/login', {
templateUrl: 'views/login.html',
controller: 'loginCtrl',
controllerAs: 'loginCtl'
} );
$routeProvider.when( '/logout', {
templateUrl: 'views/login.html',
controller: 'loginCtrl',
controllerAs: 'loginCtl',
resolve: {
"logout": [ "authService", function( authService ) {
authService.signOut();
}]
}
} );
$routeProvider.when( '/', {
templateUrl: 'views/dashboard.html',
resolve: {
"currentAuth": [ "authService", function( authService ) {
var auth = authService.auth();
return auth.$requireSignIn();
}]
}
});
$routeProvider.otherwise( {
redirectTo: '/'
} );
$locationProvider.html5Mode( true );
} ] );
app.run( [ "$rootScope", "$location", function( $rootScope, $location ) {
$rootScope.$on("$routeChangeError", function(event, next, previous, error) {
if (error === "AUTH_REQUIRED") {
$location.path("/login");
}
});
} ] );
loginCtrl.js
app.controller( 'loginCtrl', [ 'authService', function( authService ) {
var self = this;
self.signUp = function() {
authService.createUser(self.email, self.password);
};
self.logIn = function() {
authService.authUser(self.loginEmail, self.loginPassword);
};
self.signOut = function() {
authService.signOut();
};
}]);
authFactory.js
app.factory( 'authService', [ '$firebaseAuth', '$window', function( $firebaseAuth, $window ) {
var authService = {};
var auth = $firebaseAuth(firebase.auth());
authService.createUser = function(email, password) {
auth.$createUserWithEmailAndPassword( email, password );
};
authService.authUser = function( email, password ) {
auth.$signInWithEmailAndPassword( email, password ).then(function( user ) {
$window.location.href = "/";
}, function( error ) {
var errorCode = error.code;
var errorMessage = error.message;
console.info( "Error in authUser - errorCode: " + errorCode + ". errorMessage: " + errorMessage);
});
};
authService.signOut = function() {
auth.$signOut();
};
authService.auth = function() {
return auth;
};
return authService;
}]);
答案 0 :(得分:8)
有很多关于如何制作它的信息。
来自stackoverflow的文章/指南/答案
有用的存储库