-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlf-firebase-auth-service.js
201 lines (182 loc) · 7.52 KB
/
lf-firebase-auth-service.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
/**
* @ngdoc service
* @name lfFirebaseAuth.lfFirebaseAuthService
* @description
* authentication for the Firebase password authentication service.
* Calling module must have defined a firebase URL, named 'FIREBASE_URL'
*
* @requires FIREBASE_URL a string constant with the firebase url.
*
* @filename: user-service.js
* @author lfortes on 11/27/2014.
*
* */
(function () {
'use strict';
angular.module('lfFirebaseAuth', []).factory('lfFirebaseAuthService', ['FIREBASE_URL', '$rootScope', '$q', function(FIREBASE_URL, $rootScope, $q) {
var authServiceError = {};
var currentUser = {};
var temporaryPassword = false;
var BACKEND = new Firebase(FIREBASE_URL);
var loginAfterCreateUser = function(user, login, callback) {
BACKEND.authWithPassword(user, function(error, authData) {
if (error === null) {
authServiceError = {};
var userData = user;
userData.id = authData.uid;
delete userData.password;
delete userData.repeatPassword;
delete userData.repeatEmail;
BACKEND.child('lfUsers').child(authData.uid).set(userData);
if (login) {
currentUser = userData;
$rootScope.$emit('USER_LOGGED_IN_EVENT');
window.localStorage.setItem('storeUser', angular.toJson(currentUser));
}
else {
BACKEND.unauth();
currentUser = {};
}
callback(true);
}
else {
authServiceError = error;
callback(false);
}
});
};
var getUserData = function(authData) {
//TODO: get user from localStorage or backend
var local = angular.fromJson(window.localStorage.getItem('storeUser'));
if (local) {
currentUser = local;
}
else {
var sync = BACKEND.child('lfUsers').child(authData.uid);
sync.on('value', function(snapshot) {
currentUser = snapshot.val();
$rootScope.$emit('USER_LOGGED_IN_EVENT');
window.localStorage.setItem('storeUser', angular.toJson(currentUser));
});
}
};
var processServerResponse = function(error) {
if (error === null) {
authServiceError = {};
return true;
}
else {
authServiceError = error;
return false;
}
};
return {
/**
* Adds a new user. Returns promise with success and failure results.
* Upon successful user creation, if login parameter is true, user is automatically signed in
* and a 'USER_LOGGED_IN_EVENT' is emitted on $rootScope.
* @param user {object} object with required 'mail' and 'password' fields. Other optional fields
* will be stored in the authentication server 'lfUsers' file. Fields 'repeatPassword' and 'repeatEmail, if present, are not stored.
* @param login {boolean} true if should login after user is created, false if should not login
*/
add: function(user, login) {
return $q(function(resolve, reject) {
BACKEND.createUser(user, function(error) {
if (error === null) {
loginAfterCreateUser(user, login, function(userIn) {
if (userIn) { resolve('Success'); }
else { reject(authServiceError);}
});
} else {
authServiceError = error;
reject(error);
}
});
});
},
/**
* Logs in an existing user. Returns promise with success and failure results.
* On successful login, it emits 'USER_LOGGED_IN_EVENT' on $rootScope
* @param user {object} object with 'mail' and 'password' fields
*/
login: function(user) {
return $q(function(resolve, reject) {
BACKEND.authWithPassword(user, function(error, authData) {
if (error === null) {
// user authenticated with Firebase
getUserData(authData);
temporaryPassword = authData.password.isTemporaryPassword;
authServiceError = {};
resolve('logged in');
} else {
authServiceError = error;
reject(error);
}
});
});
},
/**
* Returns {boolean} true if user is logged in.
* @returns {boolean}
*/
isLoggedIn: function() {
var authData = BACKEND.getAuth();
if (authData) {
getUserData(authData);
return true;
} else {
return false;
}
},
/**
* Logs out current user.
*/
logout: function() {
BACKEND.unauth();
currentUser = {};
localStorage.removeItem("storeUser");
},
/**
* Returns an error object with 'message' and other properties, if error exists. Otherwise returns an empty object.
* @returns {{}}
*/
authServiceError: function() { return authServiceError; },
/**
* Returns logged in user.
* @returns {{}}
*/
user: function() { return currentUser; },
/**
* Resets user password. Returns promise with success and failure results.
* @param user {object} object with 'email' property
*/
resetPassword: function(user) {
return $q(function(resolve, reject) {
BACKEND.resetPassword(user, function(error) {
var success = processServerResponse(error);
if (success) { resolve('Success'); }
else { reject(authServiceError); }
});
});
},
/**
* Returns true if current user password is temporary
* @returns {boolean}
*/
isTemporaryPassword: function() { return temporaryPassword; },
/**
* Changes user password. Returns promise with success and failure results.
* @param user {object} object with 'email', 'oldPassword', and 'newPassword' properties.
*/
changePassword: function(user) {
return $q(function(resolve, reject) {
BACKEND.changePassword(user, function(error) {
var success = processServerResponse(error);
if (success) { resolve('Success'); }
else { reject(authServiceError); }
});
});
}
};
}]);
})();