How do i grant access to only activated users in my app, Accountsuser

my db has a default field of ‘Not activated’ which the admin changes, to ‘activated’, i want only activated users to be able to login.

     "submit #login": function(event, template) {
     event.preventDefault();
    var username = template.find('#login-username').value,
        password = template.find('#login-password').value;


    Meteor.loginWithPassword(username, password, function(error) {
      

            if (error) {
                alert(error.reason);
               
            } else {
                Router.go('/');
           }
        }
    );
}

I think you need a method that run in both client and server to query the Meteor.users.email[0].verified against _id:this.userId field

my db schema is like this

         Accounts.createUser({
        username: template.find("#regstudent-regno").value,
        password: template.find("#regstudent-password").value,

        profile: {
            firstname: template.find("#regstudent-firstname").value,
            lastname: template.find("#regstudent-lastname").value,
            phonenumber: template.find("#regstudent-phone").value,
            user: 'Student',
            AccountStatus: 'Not Activated'
            // Other required field values can go here
        }

I want my code to check ‘AccountStatus’ field and login if it is set to ‘Activated’

Please help me out, i’m new in meteor do not know how to go about it.

Something like:

let username = template.find('#login-username').value;
let user = Users.findOne({ username: username });

// Short circuit this mamma-jamma, the user is not activated.
if (!user.activated) { return; }

Meteor.loginWithPassword(username, password, function(error) {
}

i think @sergiotapia provide a good answer, you can further modify it
var profile = Users.findOne({ username: username },{profile:1, _id:0});
if (profile.actived){
Meteor.loginWithPassword(username, password, function(error) {
}

 Template.signup.helpers({
//add you helpers here
 });

Template.signup.events({
//add your events here
"submit #signup-form": function(event, template) {
event.preventDefault();
Accounts.createUser({
username: template.find("#signup-staffid").value,
password: template.find("#signup-password").value,
profile: {
firstname: template.find("#signup-firstname").value,
lastname: template.find("#signup-lastname").value,
phonenumber: template.find("#signup-phone").value,
user: 'Staff', 
AccountStatus: 'Not Activated'
// Other required field values can go here
}
}, function(error) {
if (error) {
// Display the user creation error to the user however you want
alert(error.reason);
} else{
alert("Registered Successfully");
  }
 });
   },

"submit #regstudent-form": function(event, template) {
event.preventDefault();
Accounts.createUser({
   username: template.find("#regstudent-regno").value,
   password: template.find("#regstudent-password").value,

profile: {
 firstname: template.find("#regstudent-firstname").value,
 lastname: template.find("#regstudent-lastname").value,
phonenumber: template.find("#regstudent-phone").value,
user: 'Student',
AccountStatus: 'Not Activated'
// Other required field values can go here
}
}, function(error) {
if (error) {
// Display the user creation error to the user however you want
alert(error.reason);
} else{
alert("Registered Successfully");
  }
});

}


   });

  }
    Template.login.helpers({
     //add you helpers here
     });

  Template.login.events({
 //add your events here

'click .regStaff' : function () {
    Modal.show('signup');

},
"submit #login": function(event, template) {
    event.preventDefault();
       var username = template.find('#login-username').value,
        password = template.find('#login-password').value;

      //  profile = Meteor.users.findOne({ username: username }, {'profile.AccountStatus': 'Activated'});

    // Short circuit this mamma-jamma, the user is not activated.
    //if (profile.Activated) { //return; }

    Meteor.loginWithPassword(username, password, function(error) {
      //  function(error)
            if (error) {
                alert(error.reason);
                // Display the login error to the user however you want
            } else {
                Router.go('/');
            }
           }
    ); 
 }
});

this is my login.js code tried your method and it’s still not working

Use the hook Accounts.onLogin()

Accounts.onLogin(function(){
   if(Meteor.user().profile && Meteor.user().profile.AccountStatus === "Activated"){
//all well, route to home
} else {show error and sign out user}

}

I would suggest to use a boolean ‘isActive’ though, rather than testing strings

Thank you all for your assistance, the problem i have now is if the user is not deactivated. the dialog error box pops up and doesnt close until i click close multiple times.

   "submit #login": function(event, template) {
    event.preventDefault();
          var username = template.find('#login-username').value,
        password = template.find('#login-password').value;

            Accounts.onLogin(function() {
                if (Meteor.user().profile && Meteor.user().profile.AccountStatus === "Activated") {
                    //all well, route to home
                    Router.go('/');
                } else  
    {  
            alert("Account is inactive, Please contact Admin");
                              }

            })
        }
    );


   }

You should really be using this:

http://docs.meteor.com/#/full/accounts_validateloginattempt

Just define it on the server, and throw a Meteor Error if AccountStatus !== Activated, then just handle it on the client however you like.

onLogin is a hook that needs to be created only once, just put it in your Meteor.startup(), not inside the event handler like that.

Just tried it, its not working, if it is activated it will login, if it is deactivated it will still login.

@ralof finally fixed it i decided to use Modal to display my Errors, Thank you all I’m very grateful