Accounts let users log on a meteor app. This example shows how to let users log with passwords.

The Accounts example code is on github, to get it run:

git clone https://github.com/RemyNoulin/accounts.git

  1. Packages
  2. Templates
  3. Javascript
  4. Configuration

1. Packages

Add these packages to the meteor app:

meteor add accounts-ui
meteor add accounts-password

For the bootstrap version of accounts, add these packages:

meteor add ian:accounts-ui-bootstrap-3
meteor add accounts-password

2. Templates

The login buttons can added anywhere in the templates:

Use ‘#if currentUser’ and ‘#unless currentUser’ to detect if the user logged on.

{{#if currentUser}}
{{currentUser.username}}
{{currentUser.profile.name}}
{{/if}}

{{#unless currentUser}}
Please log.
{{/unless}}

3. Javascript

The user is identified by a userid: Meteor.userId()

Users can be logged on several devices, there is a function to log off all other clients: Meteor.logoutOtherClients()

User data is updated with Meteor.user.update(), for example:

Meteor.users.update({'_id':Meteor.userId()},
                    {$set: {'profile.name': 'Jane Schmidt'}}
);

Users are deleted with the function remove:

// remove user
Meteor.users.remove(userid);

A function can be called when new user are created:

Accounts.onCreateUser(function(options, user) {
  user.profile = {name:'Joe Schmoe', avatar:'Missing'};
  return user;
});

To get the number of accounts use:

Meteor.users.find().count();

To create new accounts from the code, use Accounts.createUser():

var userid = Accounts.createUser({
  email:    'demo@demo.com',
  password: 'demo',
  username: 'demo',
  profile:  {name: 'demo'}
});

Meteor.users.update({_id:userid}, {$set: {'email.0.verified':true}});

4. Configuration

Accounts.config holds the global accounts options. We can enable/disable account creation, force email verification and number of days until the user must log on again. Accounts.ui.config holds the options for the ui. With passwordSignupFields, we choose which to display in the accounts ui: only username or username and email.

Accounts.config({
  forbidClientAccountCreation : false
});

Accounts.ui.config({
  passwordSignupFields: USERNAME_AND_OPTIONAL_EMAIL
});

USERNAME_ONLY