-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathprompt.js
More file actions
118 lines (82 loc) · 2.4 KB
/
prompt.js
File metadata and controls
118 lines (82 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
"use strict";
var reply = require('reply'),
api = require('./api'),
destiny;
var api;
var log = function(str){
console.log(str);
};
var setup = {
attempt: 1,
existing_user: function(callback){
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);
var data = { username: email, password: pass };
api.accounts.authorize(data, function(err, key) {
self.check_response(err, key, callback);
})
});
},
new_user: function(callback){
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);
var name_opts = {
message: "Ok, last one: What's your name?"
};
reply.get({ name: name_opts }, function(err, answers) {
if (err) return callback(err);
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);
});
});
});
},
check_response: function(err, data, callback){
if (!err){
callback(null, data);
} else if (this.attempt < 3) {
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);
} else {
log("Shoot. Seems like this is not your day. Try again in a minute.");
callback(err);
}
},
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'
}
};
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);
});
};