<repo_name>AndreyAntipov/meteor-accounts-guest<file_sep>/accounts-guest-client.js
/*****************
 * special anonymous behavior so that visitors can
 * manipulate their work
 *
 */

// If our app has a Blaze, override the {{currentUser}} helper to deal guest logins
if (Package.blaze) {
    /**
     * @global
     * @name  currentUser
     * @isHelper true
     * @summary Calls [Meteor.user()](#meteor_user). Use `{{#if currentUser}}` to check whether the user is logged in.
     * Where "logged in" means: The user has has authenticated (e.g. through providing credentials)
     */
    Package.blaze.Blaze.Template.registerHelper('currentUser', function () {
        var user = Meteor.user();
        if (user &&
            typeof user.profile !== 'undefined' &&
            typeof user.profile.guest !== 'undefined' &&
            user.profile.guest &&
            AccountsGuest.name === false){
            // a guest login is not a real login where the user is authenticated.
            // This allows the account-base "Sign-in" to still appear
            return null;
        } else {
            return Meteor.user();
        }
    });
}

//no non-logged in users
/* you might need to limit this to avoid flooding the user db */
Meteor.loginVisitor = function (email, callback) {
    if (!Meteor.userId()) {
        Meteor.call('createGuest', email, function (error, result) {
            if (error) {
                return callback && callback(error);
            }

            /* if a simple "true" is returned, we are in a disabled mode */
            if(result === true) return callback && callback();

            Meteor.loginWithPassword(result.email, result.password, function(error) {
                if(error) {
                    callback && callback(error);
                } else {
                    callback && callback();
                }
            });
        });
    }
}

Meteor.startup(function(){
    Deps.autorun(function () {
        if (!Meteor.userId()) {
            if (AccountsGuest.forced === true) {
                Meteor.loginVisitor();
            }
        }
    });
});
<file_sep>/accounts-guest-server.js
Moniker = Npm.require('moniker');

Accounts.removeOldGuests = function (before) {
    if (typeof before === 'undefined') {
        before = new Date();
        before.setHours(before.getHours() - 1);
    }
    res = Meteor.users.remove({createdAt: {$lte: before}, 'profile.guest': true});
    //res = Meteor.users.remove( {'profile.guest': true});
    return res;
};

/* adapted from pull-request https://github.com/dcsan
* See https://github.com/artwells/meteor-accounts-guest/commit/28cbbf0eca2d80f78925ac619abf53d0769c0d9d
*/
Meteor.methods({
    createGuest: function (email)
    {
        check(email, Match.OneOf(String, null, undefined));
        
        /* if explicitly disabled, happily do nothing */
        if (AccountsGuest.enabled === false){
            return true;
        }

        //    count = Meteor.users.find().count() + 1
        if (AccountsGuest.name === true) {
          guestname = Moniker.choose();
          // Just in case, let's make sure this name isn't taken
          while (Meteor.users.find({username:guestname}).count() > 0) {
            guestname = Moniker.choose();
          }
        } else {
          guestname = "guest-#" + Random.id()
        }

        if (!email) {
            email = guestname + "@example.com";
        }

        guest = {
            username: guestname,
            email: email,
            profile: {guest: true},
            password: <PASSWORD>(),
        };
        Accounts.createUser(guest);
        //    console.log("createGuest" + guestname);
        return guest;
    }
});
