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

$auth not determining logged in status properly #28

Closed
kimchirichie opened this issue Aug 15, 2016 · 2 comments
Closed

$auth not determining logged in status properly #28

kimchirichie opened this issue Aug 15, 2016 · 2 comments

Comments

@kimchirichie
Copy link

kimchirichie commented Aug 15, 2016

Following 1.3.2 doc & 1.3.6 doc I've tried denying access to non-admin to a page.

resolve: {
    user: function($auth){
        return $auth.awaitUser(function(user){
            if (user.admin) {
                return true;
            } else {
                return 'Access Denied';
            }
        });
    }
}

The problem is, $auth does not properly determine if the user is logged in or not. Even the following code did not determine the login status properly and denied access to the page.

resolve: {
    user: ($auth) => {
        return $auth.awaitUser();
    }
}

I tried copying the code in #22 :

resolve: {
    'currentUser': function ($q) {
        var defer = $q.defer();
        Meteor.autorun(c => {
            let loggingIn = Meteor.loggingIn();
            if (!loggingIn) {
                if (Meteor.userId()) {
                    console.log('user exists');
                    defer.resolve();
                } else {
                    console.log('user doesnt');
                    defer.reject();
                }
                c.stop();
            }
        });
        return defer.promise;

    }
}

This determined the user login status as expected.

@kimchirichie
Copy link
Author

kimchirichie commented Aug 15, 2016

After doing a lot of trial & error, it seems like this is only because Meteor.user() is null when page is refreshed.

@kimchirichie
Copy link
Author

My solution was to just wait it out until Meteor.user() has time to load.

resolve: {
    admin: function ($q) {
        var defer = $q.defer();
        Meteor.setTimeout(function(){
            var user = Meteor.user();
            if(user && user.profile.role == 'admin'){
                console.log('admin found');
                defer.resolve()
            } else {
                if(user){
                    console.log(user);
                    console.log('user not admin');
                } else {
                    console.log('user not found');
                }
                defer.reject();
            }
        },500)
        return defer.promise;
    }
}

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