forked from felix-dumit/PFUser-Digits
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.js
113 lines (93 loc) · 3.08 KB
/
user.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
var _ = require('underscore');
var Buffer = require('buffer').Buffer;
var randomString = function(){
var str = new Buffer(24);
_.times(24, function (i) {
str.set(i, _.random(0, 255));
});
return str.toString('base64');
}
var createNewDigitsUser = function (userId) {
var user = new Parse.User();
var username = randomString();
var password = randomString();
user.set("username", username);
user.set("password", password);
user.set('digitsId', userId);
return user.signUp().then(function(user){
return Parse.User.logIn(username, password);
});
};
var verifyDigitsLogin = function(requestURL, authHeader){
var promise = new Parse.Promise();
Parse.Cloud.httpRequest({
url: requestURL,
headers: {'Authorization': authHeader}
}).then(function (res) {
if (res.status != 200) {
promise.reject("error with echo twitter authentication");
} else{
promise.resolve(true);
}
}, function(error){
promise.reject(error);
});
return promise;
};
Parse.Cloud.define("linkWithDigits", function(request, response){
Parse.Cloud.useMasterKey();
var userId = request.params.userId;
var digitsId = request.params.digitsId;
var requestURL = request.params.requestURL;
var authHeader = request.params.authHeader;
verifyDigitsLogin(requestURL, authHeader).then(function(){
var query = new Parse.Query(Parse.User);
query.equalTo('digitsId', digitsId);
return query.first();
}).then(function(user){
if(user){
return Parse.Promise.error("already a user linked with this digits account");
}
var query = new Parse.Query(Parse.User);
return query.get(userId);
}).then(function(user){
user.set('digitsId', digitsId);
user.set('phone', request.params.phoneNumber);
return user.save();
}).then(function(){
response.success(true);
}, function(error){
response.error(error);
});
});
Parse.Cloud.define("loginWithDigits", function (request, response) {
Parse.Cloud.useMasterKey();
var userId = request.params.digitsId;
var requestURL = request.params.requestURL;
var authHeader = request.params.authHeader;
var password = randomString();
verifyDigitsLogin(requestURL, authHeader).then(function(){
var query = new Parse.Query(Parse.User);
query.equalTo('digitsId', userId);
return query.first({useMasterKey: true}).then(function(user){
if(user){
user.set("password", password);
return user.save();
}
});
}).then(function (user) {
if(user){
return Parse.User.logIn(user.get("username"), password);
}
else {
return createNewDigitsUser(userId);
}
}).then(function (user) {
user.set('phone', request.params.phoneNumber);
return user.save();
}).then(function (user) {
response.success(user.getSessionToken());
}, function (error) {
response.error(error);
});
});