-
Notifications
You must be signed in to change notification settings - Fork 2
/
auth.js
111 lines (104 loc) · 3.07 KB
/
auth.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
var Parse = require('parse').Parse,
usr = require('user-settings').file('.omgconfig'),
inquirer = require('inquirer'),
validator = require('validator'),
models = require(__dirname + '/models'),
spinner = require(__dirname + '/spinner');
require('colors');
function noop() {}
function logOut(fn) {
if (fn && typeof fn !== 'function') throw new Error('argument must be a function');
usr.unset('sessionToken');
console.log('ACCESS TERMINATED'.green);
process.exit(0);
fn();
}
function logIn(fn) {
if (fn && typeof fn !== 'function') throw new Error('argument must be a function');
var inqUser = {name:'username', message:'username: '};
var inqPass = {name:'password', message:'password: ', type:'password'};
inquirer.prompt([inqUser, inqPass], function(input) {
spinner.start('Authenticating'.grey);
Parse.User.logIn(input.username, input.password, {
success: function(user) {
spinner.stop();
console.log('ACCESS GRANTED'.green);
usr.set('sessionToken', user.getSessionToken());
fn(user);
},
error: function(user, error) {
spinner.stop();
if (error.code === 101) {
console.error('ACCESS DENIED'.red.bold);
process.exit(1);
} else {
console.error((error.message).red);
process.exit(1);
}
}
});
});
}
function signUp(fn) {
if (fn && typeof fn !== 'function') throw new Error('argument must be a function');
var inqUser = {name:'username', message:'username: '};
var inqPass = {name:'password', message:'password: ', type:'password'};
var inqEmail = {name:'email', message:'email: ', validate:validator.isEmail};
inquirer.prompt([inqUser, inqPass, inqEmail], function(input) {
var user = new Parse.User();
user.set('username', input.username);
user.set('password', input.password);
user.set('email', input.email);
spinner.start('Signing up'.grey);
user.signUp(null, {
success: function(user) {
spinner.stop();
console.log('ACCESS GRANTED'.green);
usr.set('sessionToken', user.getSessionToken());
fn(user);
},
error: function(user, error) {
spinner.stop();
console.error((error.message).red);
process.exit(1);
}
});
});
}
function session(fn) {
if (fn && typeof fn !== 'function') throw new Error('argument must be a function');
var sessionToken = usr.get('sessionToken');
if(sessionToken) {
spinner.start('Authenticating'.grey);
Parse.User.become(sessionToken, {
success: function(user) {
spinner.stop();
fn(user);
},
error: function(user, error) {
spinner.stop();
usr.unset('sessionToken');
console.error('ACCESS EXPIRED'.red);
process.exit(1);
}
})
} else {
var inqAuth = {
name:'auth',
message:'Authorization',
type:'list',
choices: [ 'Log In', 'Register', 'Leave' ]
};
inquirer.prompt([inqAuth], function(input) {
if (input.auth === 'Log In') logIn(fn);
else if (input.auth === 'Register') signUp(fn);
else process.exit(0);
});
}
}
module.exports = {
session: function(fn) { session(fn); },
signUp: function() { signUp(noop); },
logIn: function() { logIn(noop); },
logOut: function() { logOut(noop); }
};