Thursday, 16 March 2017

Create New User Using npm scripts

I'd like to create a user using a js file in my package.json scripts, but when I run the script via npm run create_admin the user is not created and no error is thrown. Some quick debugging leads me to believe that Account.register is not being called.

package.json scripts -

"scripts": {
"start": "node bin/www",
"test": "mocha --recursive",
"create_admin": "node scripts/create_admin"}

create_admin.js -

var prompt = require('prompt'),
    Account = require('../models/account'),
    util = require('../utility/utility');

var schema = {
    properties: {
        username: {
            pattern:  /^[a-zA-Z\s\-]+$/,
            message: 'Username must contain only letters, spaces, or dashes.',
            required: true,
            description: 'Enter username'
    },
        password: {
            hidden: true,
            required: true,
            description: 'Enter password'
        },
        confirmPassword: {
            hidden: true,
            required: true,
            description: 'Confirm password'
        }
    }
};

prompt.start();
prompt.get(schema, function(err, result) {
    if (result.password === result.confirmPassword) {
        var newAccount = {
            username: result.username,
            isAdmin: true,
            regDate: Date.now(),
            lastLogin: Date.now()
        };
        Account.register(new Account(newAccount), result.password, function(err, account) {
            if (err) {
                console.log('Username already in use. Please try another username');
            } else {
                console.log('Admin account created');
            }
        });
    } else {
        console.log('Error - passwords do not match');
    }
});

Any help is appreciated.



via etemple1

No comments:

Post a Comment