1

I have little problem with Angurafire with angular-ui-router.

I have this code for my login script, and it works

.factory('Auth', function($firebaseAuth){
  var auth = $firebaseAuth();
  return auth;
 })

.controller('authCtrl', function(Auth, $state) {
  var authCtrl = this;

  authCtrl.user = {
    email : '',
    password  : ''
  };

  authCtrl.login = function (){
    Auth.$signInWithEmailAndPassword(authCtrl.user.email, authCtrl.user.password).then(function(auth){
      $state.go('home');
    }, function (error){
      $('#loginError').text('Helaas, uw gegevens kom niet overeen');
    });
  };

angular.module('myApp.cursustext', ['ui.router'])

.config(function($stateProvider, $urlRouterProvider){
  $stateProvider.state('cursustext', {
   url: '/cursustext',
   templateUrl: 'views/cursustext/cursustext.html',
   controller: 'cursustextCtrl'
  })
});

How can I arrange that on some pages, you must be logged in?

EXTRA

In the database there is a userlevel at the user, how can I arrange that you need some special userlevel for a page (9 or something for Admin)

UPDATE

Somthing likes this:

.config(function($stateProvider, $urlRouterProvider){
  $stateProvider.state('cursustext', {
   url: '/cursustext',
   templateUrl: 'views/cursustext/cursustext.html',
   controller: 'cursustextCtrl',
   loginstatus: true,
   userlevel: '5'
  })
});

1 Answer 1

1

Let's say each user object retrieved from Firebase has some paramater user.level that is an integer and, the higher the integer, the higher the user's permissions. I would go about solving your problem as follows. (I am assuming you are making your single-page app the standard way with either components or templates+controllers for each page to which a user might try to navigate.)

I would inject your firebase service or some go-between for Firebase into each of your controllers for pages/components that need to check user-authorization level and, from there, check the user's auth level at the very start of the controller's initialization. Once the user's auth-level is known, check whether it is higher than the page's permission level and, if it isn't, reroute the user to a different page (perhaps a forbidden or home page) using the $window or $location.

i.e.

angular.module('MyApp').controller("adminPageController", function(FirebaseService, $location) {
    if (FirebaseSerice.user.level < 5) {
        $location.path('/homePage')
    }

    // ... rest of your code for your controller here
}
Sign up to request clarification or add additional context in comments.

2 Comments

But, is this the easiest why? because know you must do this every page where you want it. $stateProvider.state('cursustext', { url: '/cursustext', templateUrl: 'views/cursustext/cursustext.html', controller: 'cursustextCtrl', userlevel: '5' }) There is nothing in this way? That it is always checked when you change path, before you get to the page. And one the same why with loginstatus: true?
I suppose you could add some resolve's to your routing config that might intercept and reroute.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.