· 10 years ago · Feb 29, 2016, 08:21 PM
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}]);