Skip to content

Commit

Permalink
Merge pull request #63 from nicoreed/master
Browse files Browse the repository at this point in the history
Signup command and login command
  • Loading branch information
Nico Reed committed Jul 11, 2011
2 parents 04816e5 + 0dc265e commit f7aee73
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 108 deletions.
12 changes: 10 additions & 2 deletions README.md
Expand Up @@ -57,7 +57,15 @@ If it's your first time using `jitsu`, you will be prompted to login with an exi
jitsu <resource> <action> <param1> <param2> ...

**Common Commands:**


*To sign up for [Nodejitsu](http://nodejitsu.com)*

jitsu signup

*To log into [Nodejitsu](http://nodejitsu.com)*

jitsu login

*Deploys current path to [Nodejitsu](http://nodejitsu.com)*

jitsu deploy
Expand Down Expand Up @@ -127,4 +135,4 @@ If you need to have multiple configuration files, use --localconf or --jitsuconf
### Need more?
The documentation for `jitsu` and the [Nodejitsu](http://nodejitsu.com) APIs is open-source and a work in-progress. For more information checkout the [Nodejitsu Handbook](http://github.com/nodejitsu/handbook)

#### (C) Copyright 2010, [Nodejitsu](http://nodejitsu.com) Inc.
#### (C) Copyright 2010, [Nodejitsu](http://nodejitsu.com) Inc.
11 changes: 11 additions & 0 deletions lib/jitsu.js
Expand Up @@ -50,7 +50,9 @@ jitsu.commands.alias('create', { resource: 'apps', command: 'create' });
jitsu.commands.alias('deploy', { resource: 'apps', command: 'deploy' });
jitsu.commands.alias('list', { resource: 'apps', command: 'list' });
jitsu.commands.alias('conf', { resource: 'config', command: 'list' });
jitsu.commands.alias('login', { resource: 'users', command: 'login' });
jitsu.commands.alias('logout', { resource: 'users', command: 'logout' });
jitsu.commands.alias('signup', { resource: 'users', command: 'create' });

var eyes = require('eyes'),
winston = require('winston'),
Expand Down Expand Up @@ -194,7 +196,16 @@ jitsu.auth = function (callback) {
//
jitsu.setupUser = function (callback) {
winston.warn('No user has been setup on this machine');
jitsu.setupUserNoWarn(callback);
};

//
// ### function setupUserNoWarn (callback)
// #### @callback {function} Continuation to pass control to when complete.
// Prompts the user for their username / password combo, then sets up the jitsu CLI,
// and saves the resulting configuration to disk without a warning
//
jitsu.setupUserNoWarn = function(callback) {
//
// Attempt to get the password three times.
//
Expand Down
31 changes: 0 additions & 31 deletions lib/jitsu/api/users.js
Expand Up @@ -81,34 +81,3 @@ Users.prototype.forgot = function (username, callback) {
});
};

//
// ### function isAvailable (username, callback)
// #### @user {String} the username to check if its available
// #### @callback {function} Continuation to pass control to when complete
// check if the username is available
//
Users.prototype.isAvailable = function (username, callback) {
this.request('GET', ['users', username, 'available'], null, callback, function (res, result) {
callback(null, result);
});
};

//
// ### function submit (username, email, password, callback)
// #### @user {String} the username to create
// #### @email {String} the user's email
// #### @password {String} the user's password
// #### @callback {function} Continuation to pass control to when complete
// try and create a username
//
Users.prototype.submit = function (username, email, password, callback) {
var options = {
username: username,
email: email,
password: password
};

this.request('POST', ['users', username], options, callback, function (res, result) {
callback(null, result);
});
};
2 changes: 1 addition & 1 deletion lib/jitsu/commands.js
Expand Up @@ -136,4 +136,4 @@ commands.alias = function (target, source) {
if (requireAuth.indexOf(source.resource) !== -1) {
requireAuth.push(target);
}
};
};
10 changes: 10 additions & 0 deletions lib/jitsu/commands/help.js
Expand Up @@ -30,6 +30,16 @@ help.usage = [
'Common Commands:'.cyan.bold.underline,
'',

'To sign up for Nodejitsu'.cyan,
'',
' jitsu signup',
'',

'To log into Nodejitsu'.cyan,
'',
' jitsu login',
'',

'Deploys current path to Nodejitsu'.cyan,
'',
' jitsu deploy',
Expand Down
136 changes: 62 additions & 74 deletions lib/jitsu/commands/users.js
Expand Up @@ -14,7 +14,7 @@ users.usage = [
'`jitsu users *` commands allow you to work with new',
'or existing Nodejitsu user accounts',
'',
'jitsu users create <username>',
'jitsu users create',
'jitsu users available <username>',
'jitsu users confirm <username>',
'jitsu users forgot <username>',
Expand All @@ -31,31 +31,68 @@ users.usage = [
// Attempts to create a Nodejitsu user account with the specified `username`.
// Prompts the user for additional `email` and `password` information.
//
users.create = function (username, callback) {
if (!callback) {
callback = username;
return callback(new Error('username is required'), true);
users.create = function (callback) {
var username, email, password;
getUsername();
function getUsername() {
jitsu.prompt.get('username', function (err, result) {
jitsu.users.available(result.username, function (err,result1) {
if (err) {
callback(err);
}
else if (result1.available === false) {
winston.error('Username was already taken');
getUsername();
}
else {
username = result.username;
getEmail();
}
})
});
}

jitsu.prompt.get(['email', 'password'], function (err, result) {
var user = {
email: result.email,
password: result.password,
username: username
};

winston.info('Creating user ' + username.magenta);
jitsu.users.create(user, function (err) {
return err ? callback(err) : callback();
function getEmail() {
jitsu.prompt.get('email', function (err, result) {
if (err) {
callback(err);
}
else {
email = result.email;
getPassword();
}
});
});
}

function getPassword() {
jitsu.prompt.get(['password', {name:'confirm password', hidden: true}], function (err, result) {
if (result['password'] === result['confirm password'] && result['password'] != "") {
password = result['password'];
var options = {
username: username,
email: email,
password: password
};
jitsu.users.create(options, callback);
}
else {
if (result['password'] === "") {
winston.error('You password must have characters in it');
} else {
winston.error('The entered passwords do not match');
}
getPassword();
}
});
}
};

users.create.usage = [
'Creates a new user account with Nodejitsu. You will be',
'prompted to provide additional `email` and `password` information.',
'',
'jitsu users create <username>'
'jitsu users create',
'jitsu signup'
];

//
Expand Down Expand Up @@ -123,7 +160,7 @@ users.confirm = function (username, callback) {

callback();
});
});
});
};

users.confirm.usage = [
Expand Down Expand Up @@ -188,62 +225,13 @@ users.forgot.usage = [
'jitsu users forgot <username>'
];

//
// ### function signup (callback)
// #### @callback {function} Continuation to pass control to when complete.
// Creates an account with nodejitsu
//
users.signup = function (callback) {
var username, email, password;
getUsername();
function getUsername() {
jitsu.prompt.get('username', function (err, result) {
jitsu.users.isAvailable(result.username, function (err,result1) {
if (err) {
callback(err);
}
else if (result1.available === false) {
winston.error('Username was already taken');
getUsername();
}
else {
username = result.username;
getEmail();
}
})
});
}

function getEmail() {
jitsu.prompt.get('email', function (err, result) {
if (err) {
callback(err);
}
else {
email = result.email;
getPassword();
}
});
}

function getPassword() {
jitsu.prompt.get(['password', {name:'confirm password', hidden: true}], function (err, result) {
if (result['password'] === result['confirm password'] && result['password'] != "") {
password = result['password'];
jitsu.users.submit(username, email, password, function(err) {
callback(err);
});
}
else {
winston.error('The entered passwords do not match');
getPassword();
}
});
}
users.login = function(callback) {
jitsu.setupUserNoWarn(callback);
}

users.signup.usage = [
'Creates a username with nodejitsu',
users.login.usage = [
'Allows the user to login',
'',
'jitsu users signup'
];
'jitsu users login',
'jitsu login'
]

0 comments on commit f7aee73

Please sign in to comment.