Skip to content
superafroman edited this page Nov 18, 2010 · 2 revisions

The UserProvider

A UserProvider is used to load users :).

A look at the InMemoryUserProvider

Although you can look up the source, for convenience the InMemoryUserProvider is below.

var InMemoryUserProvider = module.exports = function(options) {
    options = options || {};
    this.users = options.users || {};
    for (var key in this.users) {
        var user = this.users[key];
        this.users[key.toLowerCase()] = user;
        if (!user.getUsername) {
            user.getUsername = function() {
                return this.username;
            };
        }
        if (!user.getPassword) {
            user.getPassword = function() {
                return this.password;
            };
        }
    }
};

InMemoryUserProvider.prototype.loadUserByUsername = function(username, callback) {
    callback(this.users[username.toLowerCase()]);
};

The main piece here is the loadUserByUsername function. All UserProviders must declare this function so that the framework can load user information. This implementation is very simple, it just returns a user from a map based on the given username. In other implementations this may look at a database, or call another web service etc.

The user map is built based on the configuration given when the UserProvider is created. If the configured users do not have getUsername and getPassword functions they are added to the objects. Again this would differ in other implementations - Users would be stored in a database etc. and no in memory store kept.

The InMemoryUserProvider should generally be used only for testing.

Creating your own UserProvider

It's as easy as creating an object with the loadUserByUsername function and configuring that object in your filter chain!

Clone this wiki locally