Skip to content

Commit

Permalink
Fixed account signup through console.
Browse files Browse the repository at this point in the history
  • Loading branch information
tomas committed Oct 10, 2013
1 parent 6c935de commit 288ce06
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 74 deletions.
2 changes: 1 addition & 1 deletion lib/api/accounts.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ var set = function(key) {
}

var validate = function(data) {
if (!data.username)
if (!data.name || data.name.trim() == '')
return 'No username.';
else if (!data.email || !data.email.match(/\@/)) // TODO: fix this
return 'Invalid email.';
Expand Down
144 changes: 71 additions & 73 deletions lib/conf/prompt.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,115 +5,113 @@ var reply = require('reply'),
destiny;

var log = function(str){
console.log(str);
console.log(str);
};

var setup = {

attempt: 1,
attempt: 1,

existing_user: function(callback){
existing_user: function(callback){

var self = this;
if (this.attempt === 1) log("Well hello old friend!");
var self = this;
if (this.attempt === 1) log("Well hello old friend!");

this.get_email_and_password(function(err, email, pass){
if(err) return callback(err);
this.get_email_and_password(function(err, email, pass){
if(err) return callback(err);

var options = { username: email, password: pass };
var options = { username: email, password: pass };

api.accounts.authorize(options, function(err, key) {
self.check_response(err, key, callback);
});
api.accounts.authorize(options, function(err, key) {
self.check_response(err, key, callback);
});

});
});

},
},

new_user: function(callback){
new_user: function(callback){

var self = this;
if (this.attempt === 1) log("Warm greetings new friend.");
var self = this;
if (this.attempt === 1) log("Warm greetings new friend.");

this.get_email_and_password(function(err, email, pass){
if (err) return callback(err);
this.get_email_and_password(function(err, email, pass){
if (err) return callback(err);

var name_opts = {
message: "Ok, last one: What's your name?"
};
var name_opts = {
message: "Ok, last one: What's your name?"
};

reply.get({ name: name_opts }, function(err, answers){
if(err) return callback(err);
reply.get({ name: name_opts }, function(err, answers){
if(err) return callback(err);

var data = {
user: {
name: answers.name,
email: email,
password: pass,
password_confirmation: pass
}
};
var data = {
name: answers.name,
email: email,
password: pass,
password_confirmation: pass
};

api.accounts.signup(data, function(err, key){
self.check_response(err, key, callback);
});
api.accounts.signup(data, function(err, key){
self.check_response(err, key, callback);
});

});
});

});
});

},
},

check_response: function(err, data, callback){
check_response: function(err, data, callback){

if (!err){
if (!err){

callback(null, data);
callback(null, data);

} else if (this.attempt < 3) {
} else if (this.attempt < 3) {

log("Darn, couldn't make it: " + err.message);
log("Trying again in a sec...\n");
log("Darn, couldn't make it: " + err.message);
log("Trying again in a sec...\n");

++this.attempt;
var self = this;
setTimeout(function(){ self[destiny](callback); }, 1000);
++this.attempt;
var self = this;
setTimeout(function(){ self[destiny](callback); }, 1000);

} else {
} else {

log("Shoot. Seems like this is not your day. Try again in a minute.");
callback(err);
log("Shoot. Seems like this is not your day. Try again in a minute.");
callback(err);

}
}

},
},

get_email_and_password: function(callback){
get_email_and_password: function(callback){

var options = {
email: {
message: "Please type your account's email address.",
regex: /^([^\s]+)@([^\s]+)\.([^\s]+)$/
},
pass: {
message: "Well played. Now enter your password.",
type: 'password'
}
};
var options = {
email: {
message: "Please type your account's email address.",
regex: /^([^\s]+)@([^\s]+)\.([^\s]+)$/
},
pass: {
message: "Well played. Now enter your password.",
type: 'password'
}
};

reply.get(options, function(err, answers){
callback(err, answers.email, answers.pass);
});
reply.get(options, function(err, answers){
callback(err, answers.email, answers.pass);
});
}
};

exports.start = function(callback) {
process.stdout.write("\n");
var question = "Do you already have a Prey account?";

reply.confirm(question, function(err, yes){
if (err) return callback(err);
destiny = yes ? 'existing_user' : 'new_user';
setup[destiny](callback);
});
process.stdout.write("\n");
var question = "Do you already have a Prey account?";

reply.confirm(question, function(err, yes){
if (err) return callback(err);
destiny = yes ? 'existing_user' : 'new_user';
setup[destiny](callback);
});
};

0 comments on commit 288ce06

Please sign in to comment.