· 10 years ago · Mar 11, 2016, 12:48 AM
1{
2 "secretKey": true,
3
4 "clientList": {
5 "paid": {...}
6 },
7
8 "users": {
9 "$userID": {
10 "profile": {...}
11 }
12 }
13
14app.factory('RegisterFactory', ['AuthRefService', 'APP', '$q', 'appFunctions',
15 function(AuthRefService, APP, $q, appFunctions){
16
17 /**
18 * Precondition: A data structure of { 'secretKey': 'secretValue' },
19 * Postcondition: Returns a promise with the data at '/code' resolving with
20 * 'secretValue' or null.
21 */
22 var checkCode = function(code){
23 code = code.trim();
24 var ref = new Firebase(APP.BASEURL);
25 return ref.child(code).once('value');
26 };
27
28 /**
29 * Precondition: checkCode(code) resolves with true.
30 * Postcondition: A new user is created in Firebase and returns a
31 * promise resolving with userData (that only contains userData.uid and does not auth user) or error.
32 */
33 var createUser = function(credentials) {
34 return AuthRefService.$createUser({
35 email: credentials.email,
36 password: credentials.password
37 });
38 };
39
40 /**
41 * Precondition: checkCode(code) resolves with true.
42 * Postcondition: User is authenticated is and a promise resovles with authData or error
43 */
44 var authUser = function(credentials) {
45 return AuthRefService.$authWithPassword({
46 email: credentials.email,
47 password: credentials.password
48 });
49 };
50
51 /**
52 * Precondition: User is authenticated.
53 * Postcondition: Sets data at: /user/uid/profile and returns a
54 * promise resoloving w/o data if success, otherwise with error
55 */
56 var setProfile = function(authData, profile){
57 var ref = new Firebase(APP.BASEURL + "users/" + authData.uid);
58 return ref.child("profile").set(profile);
59 };
60
61 /**
62 * Precondition: User is authenticated.
63 * Postcondition: Sets data at: /clientList/paid and returns a
64 * promise resoloving w/o data if success, otherwise with error
65 */
66 var addToClientList = function(data, uid) {
67 var now = appFunctions.dateToNum(new Date()); // yyyyMMdd
68 var info = {
69 first: data.profile.first,
70 last: data.profile.last,
71 email: data.credentials.email,
72 dateJoined: now
73 };
74
75 var ref = new Firebase(APP.BASEURL + "clientList/paid");
76
77 return ref.child(uid).set(info);
78 };
79
80 return {
81 /**
82 * Precondition: user input, data, is complete
83 * Postcondition: A new user is created in database
84 */
85 register: function(data) {
86 return $q(function(resolve, reject) {
87 checkCode(data.credentials.code)
88 .then(function(snapshot){
89 if(snapshot) {
90 createUser(data.credentials)
91 .then(function(userData) {
92 console.log("User create with id:", userData.uid);
93 authUser(data.credentials)
94 .then(function(authData) {
95 setProfile(authData, data.profile)
96 .then(function(resp) {
97
98 // store new user in clientList index
99 addToClientList(data, authData.uid)
100 .then(function(resp) {
101 // TODO: deal with this better
102 // logout user, b/c I couldn't figure out a solution to smoothly get the new
103 // user to '/app' b/c of how app inits user upon login.
104 AuthRefService.$unauth();
105 resolve(true);
106 }, function(err){
107 if (typeof err !== "string") {
108 err = "Error";
109 }
110 reject(err);
111 });
112
113 }, function(err) {
114 if (typeof err !== "string") {
115 err = "Error";
116 }
117 reject(err);
118 });
119
120 }, function(err) {
121 if (typeof err !== "string") {
122 err = "Error";
123 }
124 reject(err);
125 });
126
127 }, function(err) {
128 if (typeof err !== "string") {
129 err = "Error";
130 }
131 reject(err);
132 });
133 } else {
134 reject("Error");
135 }
136
137
138 }, function(err){
139 reject("Invalid Registration Code");
140 });
141 });
142 }
143 };
144}]);
145
146return {
147 /**
148 * Precondition: user input, data, is complete
149 * Postcondition: A new user is created in database
150 */
151 register: function(data) {
152 return $q(function(resolve, reject) {
153 checkCode(data.credentials.code)
154 .then(function(snapshot){
155 if(snapshot) {
156 createUser(data.credentials)
157 .then(function(userData) {
158 console.log("User create with id:", userData.uid);
159 authUser(data.credentials)
160 .then(function(authData) {
161 setProfile(authData, data.profile)
162 .then(function(resp) {
163
164 // store new user in clientList index
165 addToClientList(data, authData.uid)
166 .then(function(resp) {
167 // TODO: deal with this better
168 // logout user, b/c I couldn't figure out a solution to smoothly get the new
169 // user to '/app' b/c of how app inits user upon login.
170 AuthRefService.$unauth();
171 resolve(true);
172 }, function(err){
173 if (typeof err !== "string") {
174 err = "Error";
175 }
176 reject(err);
177 });
178
179 }, function(err) {
180 if (typeof err !== "string") {
181 err = "Error";
182 }
183 reject(err);
184 });
185
186 }, function(err) {
187 if (typeof err !== "string") {
188 err = "Error";
189 }
190 reject(err);
191 });
192
193 }, function(err) {
194 if (typeof err !== "string") {
195 err = "Error";
196 }
197 reject(err);
198 });
199 } else {
200 reject("Error");
201 }
202
203
204 }, function(err){
205 reject("Invalid Registration Code");
206 });
207 });
208 }
209};
210
211asyncOperation1().then(() => {
212
213 // asyncOperation1 done
214 return asyncOperation2();
215
216}).then(() => {
217
218 // asyncOperation2 done
219 return asyncOperation3();
220
221}).then(() => {
222
223 // asyncOperation3 done
224
225});
226
227function (data){
228 return checkCode(data.credentials.code)
229 .then( snapshot => {
230 // Note that we can forcefully reject by returning a rejected promise.
231 // We can do that with `Promise.reject`.
232 return !snapshot ? Promise.reject('Error') : createUser(data.credentials);
233 })
234 .then( userData => {
235 return authUser(data.credentials);
236 })
237 .then( authData => {
238 // We need to pass through `authData` to the next then so `addToClientList`
239 // can use it. We attach a `then` to `setProfile` that resolves with
240 // `authData` and `resp` so that the next then receives it as `profile`
241 return setProfile(authData, data.profile).then( resp => { authData, resp });
242 })
243 .then( profile => {
244 return addToClientList(data, profile.authData.uid);
245 })
246 .then( resp => {
247 AuthRefService.$unauth();
248 });
249
250 // No need for catch. Should any errors happen, the entire thing will be a
251 // return a rejected promise which the caller can handle instead.
252}