RafaelVidaurre / angular-permission

Simple route authorization via roles/permissions
MIT License
1.13k stars 212 forks source link

where i should store session and store after login ?? (Can you give example) #446

Open FerdinaKusumah opened 6 years ago

FerdinaKusumah commented 6 years ago
  1. Where i should define role first time app run ?
  2. Where i should define permission after login ??

app.controller('LoginController', ['$scope', 'USER_ROLES', '$state', 'PermPermissionStore', 'PermRoleStore', function ($scope, USER_ROLES, $state, PermPermissionStore, PermRoleStore) {

/** 
 * @argument {Clean session user}
 */
PermPermissionStore.clearStore();
PermRoleStore.clearStore();

const admin_permissions = ['canCreate', 'canRead', 'canUpdate', 'canDelete'];
const user_permissions = ['canRead'];

PermRoleStore.defineRole('ADMIN', ['canCreate', 'canRead', 'canUpdate', 'canDelete']);
PermRoleStore.defineRole('USER', ['canRead']);

$scope.login = {};

const verifyUser = async function (object) {
    username = object.username;
    password = object.password;

    const status = {};
    if ((username == 'admin') && (password == 'admin')) {
        status.valid = true;
        status.permissions = admin_permissions;
        status.username = 'ADMIN';
    } else if ((username == 'user') && (password == 'user')) {
        status.valid = true;
        status.permissions = user_permissions;
        status.username = 'USER';
    } else status.valid = false;

    return status;
};

$scope.login = async function () {
    let user = $scope.login;
    user_status = await verifyUser(user);

    if (user_status.valid) {
        PermRoleStore.defineRole(user_status.username, [user_status.permissions]);
        $state.go('home');
    } else {
        $scope.error = true;
        setTimeout(() => {
            $scope.error = false;
        }, 2000);
    }
};

}]); this is my login controller,

app.config(['$stateProvider', '$urlRouterProvider', '$locationProvider', function ($stateProvider, $urlRouterProvider, $locationProvider) {

$locationProvider.hashPrefix('');
$urlRouterProvider.otherwise("/login");

$stateProvider

    .state('login', {
        url: '/login',
        controller: 'LoginController',
        templateUrl: 'template/login/login.inc.html'
    })

    .state('home', {
        url: '/home',
        controller: 'homeController',
        templateUrl: 'template/home/home.inc.html',
        data: {
            permissions: {
                only: ['ADMIN','USER'],
                redirectTo: 'unauthorized'
            }
        }
    })

    .state('user', {
        url: '/user',
        controller: 'UserController',
        templateUrl: 'template/user/user.inc.html',
        data: {
            permissions: {
                only: ['USER'],
                redirectTo: 'unauthorized'
            }
        }
    })

    .state('unauthorized', {
        url: '/unauthorized',
        templateUrl: 'template/error/403.inc.html'
    })

;

}]);

this is my route controller,

Admin page

No Nim Nama Action
1 2013140178 Ferdina kusumah

this is my view.

thank youu