Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resolve in state definition causes "unknown provider error" in controller #245

Open
elenat82 opened this issue Oct 10, 2017 · 1 comment

Comments

@elenat82
Copy link

elenat82 commented Oct 10, 2017

I would like to check if the user is logged in before letting him enter the state, so I am trying to use the resolve on the state defintion but always get this error: [$injector:unpr] Unknown provider: authorizedProvider <- authorized

Here is my example.component.js:

`

import template from './example.html';
import controller from './example.controller';

/*
* example Component
*/
const exampleComponent = {
    restrict: 'E',
    bindings: { },
    template,
    controller
};

export default exampleComponent;

`

Here is my example.controller.js:

`

/*
* Example Controller file
* @class
*/
class ExampleController {
    /** @ngInject */
    constructor( currentUser, authorized, $state) {
        var self = this;
        self.userIsLogged = authorized;
      }
}
export default ExampleController;

`

Here is my example.js:

`

import angular from 'angular';
import uiRouter from 'angular-ui-router';
import exampleComponent from './example.component';

const exampleModule = angular.module('example', [
    uiRouter
])

    .component('example', exampleComponent)

    .config(($stateProvider, $urlRouterProvider) => {
        'ngInject';

        $urlRouterProvider.otherwise('/');

        $stateProvider
            .state('example', {
                url: '/example',
                template: '',
                data: {
                    authorizedRoles: ['md', 'superuser']
                },
                resolve: {
                    authorized: function (currentUser) {
                        console.log(currentUser.logged);//this is a variable and it's true
                        return currentUser.logged;
                      }
                }
            });
    })

    .name;

export default exampleModule;

`

@elenat82
Copy link
Author

elenat82 commented Nov 8, 2017

I found out myself thanks to this ui-router issue

I updated my code as follows:

Here is my example.component.js:

`

import template from './example.html';
import controller from './example.controller';

/*
* example Component
*/
const exampleComponent = {
    restrict: 'E',
    bindings: { },
    template,
    controller
};

export default exampleComponent;

`

Here is my example.controller.js:

`

/*
* Example Controller file
* @class
*/
class ExampleController {
    /** @ngInject */
    constructor( currentUser, $state) {
        var self = this;
      }
}
export default ExampleController;

`

Here is my example.js:

`

import angular from 'angular';
import uiRouter from 'angular-ui-router';
import exampleComponent from './example.component';

const exampleModule = angular.module('example', [
    uiRouter
])

    .component('example', exampleComponent)

    .config(($stateProvider, $urlRouterProvider) => {
        'ngInject';

        $urlRouterProvider.otherwise('/');

        $stateProvider
            .state('example', {
                url: '/example',
                template: '',
                data: {
                    authorizedRoles: ['md', 'superuser']
                },
                resolve: {
                    authorized: function($q, $state,$timeout, $rootScope) {
                        if ($rootScope.userlogged == false) {
                            $timeout(function(){
                                $state.go('login');
                            });
                            return $q.reject('User is not logged in, redirect to login page');
                        }
                }
            });
    })

    .name;

export default exampleModule;

`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant