dimanche 8 janvier 2017

$auth.isAuthenticated is not a function

I'm using laravel-angular material starter and I'm encountering an error when going to /#!/home/ when I enabled data {auth:true} on the routes.config.js. The error is $auth.isAuthenticated is not a function under routes.run.js. I don't know what is the problem here I already declared the $auth based on some solutions but still I'm encountering the error.

routes.run.js

export function RoutesRun($state, $auth, $transitions) {
    'ngInject';

    let requiresAuthCriteria = {
        to: ($state) => $state.data && $state.data.auth
    };

    let redirectToLogin = ($auth) => {
        'ngInject';
        if (!$auth.isAuthenticated()) {
            return $state.target('app.login', undefined, {location: false});
        }
    };

    $transitions.onBefore(requiresAuthCriteria, redirectToLogin, {priority:10});

}

routes.config.js

export function RoutesConfig($stateProvider, $urlRouterProvider) {
    'ngInject';

    let getView = (viewName) => {
        return `./views/app/pages/${viewName}/${viewName}.page.html`;
    };

    $urlRouterProvider.otherwise('/');

    /*
        data: {auth: true} would require JWT auth
        However you can't apply it to the abstract state
        or landing state because you'll enter a redirect loop
    */

    $stateProvider
        .state('app', {
            abstract: true,
            data: {},
            views: {
                header: {
                    templateUrl: getView('header')
                },
                footer: {
                    templateUrl: getView('footer')
                },
                main: {}
            }
        })
        .state('app.landing', {
            url: '/',
            views: {
                'main@': {
                    templateUrl: getView('landing')
                }
            }
        })
        .state('app.login', {
            url: '/login',
            data: {
                title: 'Welcome'
            },
            views: {
                'main@': {
                    templateUrl: getView('login')
                }
            }
        })
        .state('app.register', {
            url: '/register',
            views: {
                'main@': {
                    templateUrl: getView('register')
                }
            }
        })
        .state('app.forgot_password', {
            url: '/forgot-password',
            views: {
                'main@': {
                    templateUrl: getView('forgot-password')
                }
            }
        })
        .state('app.reset_password', {
            url: '/reset-password/:email/:token',
            views: {
                'main@': {
                    templateUrl: getView('reset-password')
                }
            }
        })
                .state('app.home', {
            url: '/home',
                        data: {
                            auth: true
                        },
            views: {
                'main@': {
                    templateUrl: getView('home')
                }
            }
        })
                .state('app.alumni_registration', {
            url: '/alumni-registration',
            views: {
                'main@': {
                    templateUrl: getView('alumni-registration')
                }
            }
        });
}

error

TransitionRejection(type: 6, message: The transition errored, detail: TypeError: $auth.isAuthenticated is not a function)
$defaultErrorHandler @ final-078150a113.js:38844
(anonymous) @ final-078150a113.js:39167
processQueue @ final-078150a113.js:16648
(anonymous) @ final-078150a113.js:16692
$eval @ final-078150a113.js:17972
$digest @ final-078150a113.js:17786
$apply @ final-078150a113.js:18080
bootstrapApply @ final-078150a113.js:1841
invoke @ final-078150a113.js:4842
doBootstrap @ final-078150a113.js:1839
bootstrap @ final-078150a113.js:1859
angularInit @ final-078150a113.js:1744
(anonymous) @ final-078150a113.js:32977
trigger @ final-078150a113.js:3314
final-078150a113.js:38846 TypeError: $auth.isAuthenticated is not a function
    at redirectToLogin (eval at <anonymous> (final-078150a113.js:94187), <anonymous>:20:20)
    at TransitionHook.invokeHook (final-078150a113.js:35066)
    at TransitionHook.runSynchronousHooks (final-078150a113.js:35120)
    at Transition.run (final-078150a113.js:34685)
    at StateService.transitionTo (final-078150a113.js:39171)
    at Array.$urlRouterProvider.when.state._urlRule (final-078150a113.js:38713)
    at Object.invoke (final-078150a113.js:4842)
    at handleIfMatch (final-078150a113.js:37138)
    at final-078150a113.js:37326
    at check (final-078150a113.js:37158)

Aucun commentaire:

Enregistrer un commentaire