This repository has been archived by the owner on Oct 5, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
/
accounts.js
212 lines (192 loc) · 4.74 KB
/
accounts.js
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/**
* @flow
*/
import chalk from 'chalk';
import program from 'commander';
import { User as UserManager } from 'xdl';
import type { LoginType, User } from 'xdl/build/User';
import CommandError from './CommandError';
import prompt from './prompt';
import log from './log';
UserManager.initialize();
type CommandOptions = {
username?: string,
password?: string,
};
export async function loginOrRegisterIfLoggedOut() {
let user = await UserManager.getCurrentUserAsync();
if (user) {
return user;
}
log.warn('An Expo user account is required to proceed.');
if (program.nonInteractive) {
throw new CommandError(
'NOT_LOGGED_IN',
`Not logged in. Use \`${program.name} login -u username -p password\` to log in.`
);
}
const questions = [
{
type: 'list',
name: 'action',
message: 'How would you like to authenticate?',
choices: [
{
name: 'Make a new Expo account',
value: 'register',
},
{
name: 'Log in with an existing Expo account',
value: 'existingUser',
},
{
name: 'Cancel',
value: 'cancel',
},
],
},
];
const { action } = await prompt(questions);
if (action === 'register') {
return register();
} else if (action === 'existingUser') {
return login({});
} else {
throw new CommandError('BAD_CHOICE', 'Not logged in.');
}
}
export async function login(options: CommandOptions) {
const user = await UserManager.getCurrentUserAsync();
const nonInteractive = options.parent && options.parent.nonInteractive;
if (!nonInteractive) {
if (user) {
const question = [
{
type: 'confirm',
name: 'action',
message: `You are already logged in as ${chalk.green(
user.username
)}. Log in as new user?`,
},
];
const { action } = await prompt(question);
if (!action) {
// If user chooses to stay logged in, return
return;
}
}
return _usernamePasswordAuth(options.username, options.password);
} else if (options.username && options.password) {
return _usernamePasswordAuth(options.username, options.password);
} else {
throw new CommandError(
'NON_INTERACTIVE',
'Username and password not provided in non-interactive mode.'
);
}
}
async function _usernamePasswordAuth(username?: string, password?: string): Promise<User> {
const questions = [];
if (!username) {
questions.push({
type: 'input',
name: 'username',
message: 'Username/Email Address:',
validate(val) {
if (val.trim() === '') {
return false;
}
return true;
},
});
}
if (!password) {
questions.push({
type: 'password',
name: 'password',
message: 'Password:',
validate(val) {
if (val.trim() === '') {
return false;
}
return true;
},
});
}
const answers = await prompt(questions);
const data = {
username: username || answers.username,
password: password || answers.password,
};
let user = await UserManager.loginAsync('user-pass', data);
if (user) {
console.log(`\nSuccess. You are now logged in as ${chalk.green(user.username)}.`);
return user;
} else {
throw new Error('Unexpected Error: No user returned from the API');
}
}
export async function register(): Promise<User> {
console.log(
`
Thanks for signing up for Expo!
Just a few questions:
`
);
let questions = [
{
type: 'input',
name: 'email',
message: 'E-mail:',
filter: val => val.trim(),
validate(val) {
if (val.trim() === '') {
return false;
}
return true;
},
},
{
type: 'input',
name: 'username',
message: 'Username:',
filter: val => val.trim(),
validate(val) {
if (val.trim() === '') {
return false;
}
return true;
},
},
{
type: 'password',
name: 'password',
message: 'Password:',
filter: val => val.trim(),
validate(val) {
if (val.trim() === '') {
return 'Please create a password';
}
return true;
},
},
{
type: 'password',
name: 'passwordRepeat',
message: 'Confirm Password:',
validate(val, answers) {
if (val.trim() === '') {
return false;
}
if (val.trim() !== answers.password.trim()) {
return `Passwords don't match!`;
}
return true;
},
},
];
let answers = await prompt(questions);
let registeredUser = await UserManager.registerAsync(answers);
console.log('\nThanks for signing up!');
return registeredUser;
}