-
-
Notifications
You must be signed in to change notification settings - Fork 559
/
auth.js
579 lines (537 loc) · 19.6 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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
import {
isArray,
isString,
isFunction,
forEach
} from 'lodash'
import { actionTypes } from '../constants'
import { getLoginMethodAndParams } from '../utils/auth'
import { promisesForPopulate } from '../utils/populate'
/**
* @description Dispatch login error action
* @param {Function} dispatch - Action dispatch function
* @param {Object} authError - Error object
* @private
*/
const dispatchLoginError = (dispatch, authError) =>
dispatch({
type: actionTypes.LOGIN_ERROR,
authError
})
/**
* @description Remove listener from user profile
* @param {Object} firebase - Internal firebase object
* @private
*/
export const unWatchUserProfile = (firebase) => {
const authUid = firebase._.authUid
const userProfile = firebase._.config.userProfile
if (firebase._.profileWatch) {
firebase.database()
.ref()
.child(`${userProfile}/${authUid}`)
.off('value', firebase._.profileWatch)
firebase._.profileWatch = null
}
}
/**
* @description Watch user profile. Internally dispatches SET_PROFILE actions
* @param {Function} dispatch - Action dispatch function
* @param {Object} firebase - Internal firebase object
* @private
*/
export const watchUserProfile = (dispatch, firebase) => {
const { authUid, config: { userProfile } } = firebase._
unWatchUserProfile(firebase)
if (userProfile && firebase.database) {
firebase._.profileWatch = firebase.database()
.ref()
.child(`${userProfile}/${authUid}`)
.on('value', snap => {
const {
profileParamsToPopulate,
autoPopulateProfile
} = firebase._.config
if (!profileParamsToPopulate || (!isArray(profileParamsToPopulate) && !isString(profileParamsToPopulate))) {
dispatch({ type: actionTypes.SET_PROFILE, profile: snap.val() })
} else {
// TODO: Share population logic with query action
// Convert each populate string in array into an array of once query promises
promisesForPopulate(firebase, snap.key, snap.val(), profileParamsToPopulate)
.then(data => {
// Fire actions for placement of data gathered in populate into redux
forEach(data, (result, path) => {
dispatch({
type: actionTypes.SET,
path,
data: result,
timestamp: Date.now(),
requesting: false,
requested: true
})
})
dispatch({ type: actionTypes.SET_PROFILE, profile: snap.val() })
// Dispatch action with profile combined with populated parameters
// Auto Populate profile
if (autoPopulateProfile) {
console.warn('Auto populate is no longer supported. We are working on backwards compatibility for v2.0.0') // eslint-disable-line no-console
}
})
}
})
}
}
/**
* @description Create user profile if it does not already exist. `updateProfileOnLogin: false`
* can be passed to config to disable updating. Profile factory is applied if it exists and is a function.
* @param {Function} dispatch - Action dispatch function
* @param {Object} firebase - Internal firebase object
* @param {Object} userData - User data object (response from authenticating)
* @param {Object} profile - Profile data to place in new profile
* @return {Promise}
* @private
*/
export const createUserProfile = (dispatch, firebase, userData, profile) => {
const { _: { config } } = firebase
if (!config.userProfile || !firebase.database) {
return Promise.resolve(userData)
}
// use profileFactory if it exists in config
if (isFunction(config.profileFactory)) {
// catch errors in user provided profileFactory function
try {
profile = config.profileFactory(userData, profile)
} catch (err) {
console.error('Error occured within profileFactory function:', err.message || err) // eslint-disable-line no-console
return Promise.reject(err)
}
}
// Check for user's profile at userProfile path if provided
return firebase.database()
.ref()
.child(`${config.userProfile}/${userData.uid}`)
.once('value')
.then(profileSnap =>
// update profile only if doesn't exist or if set by config
!config.updateProfileOnLogin && profileSnap.val() !== null
? profileSnap.val()
: profileSnap.ref.update(profile).then(() => profile) // Update the profile
)
.catch(err => {
// Error reading user profile
dispatch({ type: actionTypes.UNAUTHORIZED_ERROR, authError: err })
return Promise.reject(err)
})
}
/**
* @description Start presence management for a specificed user uid.
* Presence collection contains a list of users that are online currently.
* Sessions collection contains a record of all user sessions.
* This function is called within login functions if enablePresence: true.
* @param {Function} dispatch - Action dispatch function
* @param {Object} firebase - Internal firebase object
* @return {Promise}
* @private
*/
const setupPresence = (dispatch, firebase) => {
// exit if database does not exist on firebase instance
if (!firebase.database || !firebase.database.ServerValue) {
return
}
const ref = firebase.database().ref()
const { config: { presence, sessions }, authUid } = firebase._
let amOnline = ref.child('.info/connected')
let onlineRef = ref.child(isFunction(presence) ? presence(firebase.auth().currentUser, firebase) : presence).child(authUid)
let sessionsRef = isFunction(sessions) ? sessions(firebase.auth().currentUser, firebase) : sessions
if (sessionsRef) {
sessionsRef = ref.child(sessions)
}
amOnline.on('value', snapShot => {
if (!snapShot.val()) return
// user is online
if (sessionsRef) {
// add session and set disconnect
dispatch({ type: actionTypes.SESSION_START, payload: authUid })
// add new session to sessions collection
const session = sessionsRef.push({
startedAt: firebase.database.ServerValue.TIMESTAMP,
user: authUid
})
// set authUid as priority for easy sorting
session.setPriority(authUid)
const endedRef = session.child('endedAt')
endedRef.onDisconnect().set(firebase.database.ServerValue.TIMESTAMP, () => {
dispatch({ type: actionTypes.SESSION_END })
})
}
// add correct session id to user
// remove from presence list
onlineRef.set(true)
onlineRef.onDisconnect().remove()
})
}
/**
* @description Initialize authentication state change listener that
* watches user profile and dispatches login action
* @param {Function} dispatch - Action dispatch function
* @param {Object} firebase - Internal firebase object
* @private
*/
export const init = (dispatch, firebase) => {
// exit if auth does not exist
if (!firebase.auth) {
return
}
dispatch({ type: actionTypes.AUTHENTICATION_INIT_STARTED })
firebase.auth().onAuthStateChanged(authData => {
if (!authData) {
// Run onAuthStateChanged if it exists in config and enableEmptyAuthChanges is set to true
if (isFunction(firebase._.config.onAuthStateChanged) && firebase._.config.enableEmptyAuthChanges) {
firebase._.config.onAuthStateChanged(authData, firebase, dispatch)
}
return dispatch({ type: actionTypes.LOGOUT })
}
firebase._.authUid = authData.uid
// setup presence if settings and database exist
if (firebase._.config.presence && firebase.database && firebase.database.ServerValue) {
setupPresence(dispatch, firebase)
}
watchUserProfile(dispatch, firebase)
dispatch({ type: actionTypes.LOGIN, auth: authData })
// Run onAuthStateChanged if it exists in config
if (isFunction(firebase._.config.onAuthStateChanged)) {
firebase._.config.onAuthStateChanged(authData, firebase, dispatch)
}
})
// set redirect result callback if enableRedirectHandling set to true
if (firebase._.config.enableRedirectHandling && (
typeof window !== 'undefined' &&
window.location &&
window.location.protocol &&
window.location.protocol.indexOf('http') !== -1
)) {
firebase.auth().getRedirectResult()
.then((authData) => {
// Run onRedirectResult if it exists in config
if (typeof firebase._.config.onRedirectResult === 'function') {
firebase._.config.onRedirectResult(authData, firebase, dispatch)
}
if (authData && authData.user) {
const { user } = authData
firebase._.authUid = user.uid
watchUserProfile(dispatch, firebase)
dispatch({ type: actionTypes.LOGIN, auth: user })
createUserProfile(
dispatch,
firebase,
user,
{
email: user.email,
displayName: user.providerData[0].displayName || user.email,
avatarUrl: user.providerData[0].photoURL,
providerData: user.providerData
}
)
}
})
.catch((error) => {
dispatchLoginError(dispatch, error)
return Promise.reject(error)
})
}
firebase.auth().currentUser // eslint-disable-line no-unused-expressions
dispatch({ type: actionTypes.AUTHENTICATION_INIT_FINISHED })
}
/**
* @description Login with errors dispatched
* @param {Function} dispatch - Action dispatch function
* @param {Object} firebase - Internal firebase object
* @param {Object} credentials - Login credentials
* @param {Object} credentials.email - Email to login with (only needed for email login)
* @param {Object} credentials.password - Password to login with (only needed for email login)
* @param {Object} credentials.provider - Provider name such as google, twitter (only needed for 3rd party provider login)
* @param {Object} credentials.type - Popup or redirect (only needed for 3rd party provider login)
* @param {Object} credentials.token - Custom or provider token
* @return {Promise}
* @private
*/
export const login = (dispatch, firebase, credentials) => {
if (firebase._.config.resetBeforeLogin) {
dispatchLoginError(dispatch, null)
}
const { method, params } = getLoginMethodAndParams(firebase, credentials)
return firebase.auth()[method](...params)
.then((userData) => {
// Handle null response from getRedirectResult before redirect has happened
if (!userData) return Promise.resolve(null)
// For email auth return uid (createUser is used for creating a profile)
if (method === 'signInWithEmailAndPassword') {
return { user: userData }
}
// For token auth, the user key doesn't exist. Instead, return the JWT.
if (method === 'signInWithCustomToken') {
if (!firebase._.config.updateProfileOnLogin) {
return { user: userData }
}
return createUserProfile(
dispatch,
firebase,
userData
)
}
// Create profile when logging in with external provider
const user = userData.user || userData
return createUserProfile(
dispatch,
firebase,
user,
{
email: user.email,
displayName: user.providerData[0].displayName || user.email,
avatarUrl: user.providerData[0].photoURL,
providerData: user.providerData
}
)
.then((profile) => ({ profile, ...userData }))
})
.catch(err => {
dispatchLoginError(dispatch, err)
return Promise.reject(err)
})
}
/**
* @description Logout of firebase and dispatch logout event
* @param {Function} dispatch - Action dispatch function
* @param {Object} firebase - Internal firebase object
* @private
*/
export const logout = (dispatch, firebase) =>
firebase.auth()
.signOut()
.then(() => {
dispatch({
type: actionTypes.LOGOUT,
preserve: firebase._.config.preserveOnLogout
})
firebase._.authUid = null
unWatchUserProfile(firebase)
return firebase
})
/**
* @description Create a new user in auth and add an account to userProfile root
* @param {Function} dispatch - Action dispatch function
* @param {Object} firebase - Internal firebase object
* @param {Object} credentials - Login credentials
* @return {Promise}
* @private
*/
export const createUser = (dispatch, firebase, { email, password, signIn }, profile) => {
dispatchLoginError(dispatch, null)
if (!email || !password) {
dispatchLoginError(dispatch, new Error('Email and Password are required to create user'))
return Promise.reject(new Error('Email and Password are Required'))
}
return firebase.auth()
.createUserWithEmailAndPassword(email, password)
.then((userData) =>
// Login to newly created account if signIn flag is not set to false
firebase.auth().currentUser || (!!signIn && signIn === false)
? createUserProfile(dispatch, firebase, userData, profile || { email })
: login(dispatch, firebase, { email, password })
.then(() =>
createUserProfile(dispatch, firebase, userData, profile || { email })
)
.catch(err => {
if (err) {
switch (err.code) {
case 'auth/user-not-found':
dispatchLoginError(dispatch, new Error('The specified user account does not exist.'))
break
default:
dispatchLoginError(dispatch, err)
}
}
return Promise.reject(err)
})
)
.catch((err) => {
dispatchLoginError(dispatch, err)
return Promise.reject(err)
})
}
/**
* @description Send password reset email to provided email
* @param {Function} dispatch - Action dispatch function
* @param {Object} firebase - Internal firebase object
* @param {String} email - Email to send recovery email to
* @return {Promise}
* @private
*/
export const resetPassword = (dispatch, firebase, email) => {
dispatchLoginError(dispatch, null)
return firebase.auth()
.sendPasswordResetEmail(email)
.catch((err) => {
if (err) {
switch (err.code) {
case 'auth/user-not-found':
dispatchLoginError(dispatch, new Error('The specified user account does not exist.'))
break
default:
dispatchLoginError(dispatch, err)
}
return Promise.reject(err)
}
})
}
/**
* @description Confirm the password reset with code and password
* @param {Function} dispatch - Action dispatch function
* @param {Object} firebase - Internal firebase object
* @param {String} code - Email confirmation reset code
* @param {String} password - Password to set it to
* @return {Promise}
* @private
*/
export const confirmPasswordReset = (dispatch, firebase, code, password) => {
dispatchLoginError(dispatch, null)
return firebase.auth()
.confirmPasswordReset(code, password)
.catch((err) => {
if (err) {
switch (err.code) {
case 'auth/expired-action-code':
dispatchLoginError(dispatch, new Error('The action code has expired.'))
break
case 'auth/invalid-action-code':
dispatchLoginError(dispatch, new Error('The action code is invalid.'))
break
case 'auth/user-disabled':
dispatchLoginError(dispatch, new Error('The user is disabled.'))
break
case 'auth/user-not-found':
dispatchLoginError(dispatch, new Error('The user is not found.'))
break
case 'auth/weak-password':
dispatchLoginError(dispatch, new Error('The password is not strong enough.'))
break
default:
dispatchLoginError(dispatch, err)
}
return Promise.reject(err)
}
})
}
/**
* @description Verify that password reset code is valid
* @param {Function} dispatch - Action dispatch function
* @param {Object} firebase - Internal firebase object
* @param {String} code - Password reset code
* @return {Promise} email - Email associated with reset code
* @private
*/
export const verifyPasswordResetCode = (dispatch, firebase, code) => {
dispatchLoginError(dispatch, null)
return firebase.auth()
.verifyPasswordResetCode(code)
.catch((err) => {
if (err) {
dispatchLoginError(dispatch, err)
}
return Promise.reject(err)
})
}
/**
* @description Update user profile
* @param {Function} dispatch - Action dispatch function
* @param {Object} firebase - Internal firebase object
* @param {Object} userData - User data object (response from authenticating)
* @param {Object} profile - Profile data to place in new profile
* @return {Promise}
* @private
*/
export const updateProfile = (dispatch, firebase, profileUpdate) => {
dispatch({ type: actionTypes.PROFILE_UPDATE_START, payload: profileUpdate })
const { database, _: { config, authUid } } = firebase
const profileRef = database().ref(`${config.userProfile}/${authUid}`)
return profileRef
.update(profileUpdate)
.then(() =>
profileRef
.once('value')
.then((snap) => {
dispatch({
type: actionTypes.PROFILE_UPDATE_SUCCESS,
payload: snap.val()
})
return snap
})
)
.catch((payload) => {
dispatch({ type: actionTypes.PROFILE_UPDATE_ERROR, payload })
return Promise.reject(payload)
})
}
/**
* @description Update Auth Object. Internally calls
* `firebase.auth().currentUser.updateProfile` as seen [in the firebase docs](https://firebase.google.com/docs/auth/web/manage-users#update_a_users_profile).
* @param {Function} dispatch - Action dispatch function
* @param {Object} firebase - Internal firebase object
* @param {Object} profileUpdate - Update to be auth object
* @return {Promise}
* @private
*/
export const updateAuth = (dispatch, firebase, authUpdate, updateInProfile) => {
dispatch({ type: actionTypes.AUTH_UPDATE_START, payload: authUpdate })
if (!firebase.auth().currentUser) {
const msg = 'User must be logged in to update auth.'
dispatch({ type: actionTypes.AUTH_UPDATE_ERROR, payload: msg })
return Promise.reject(msg)
}
return firebase.auth().currentUser
.updateProfile(authUpdate)
.then((payload) => {
dispatch({
type: actionTypes.AUTH_UPDATE_SUCCESS,
payload: firebase.auth().currentUser
})
if (updateInProfile) {
return updateProfile(dispatch, firebase, authUpdate)
}
return payload
})
.catch((payload) => {
dispatch({ type: actionTypes.AUTH_UPDATE_ERROR, payload })
return Promise.reject(payload)
})
}
/**
* @description Update user's email. Internally calls
* `firebase.auth().currentUser.updateEmail` as seen [in the firebase docs](https://firebase.google.com/docs/auth/web/manage-users#update_a_users_profile).
* @param {Function} dispatch - Action dispatch function
* @param {Object} firebase - Internal firebase object
* @param {String} newEmail - Update to be auth object
* @return {Promise}
* @private
*/
export const updateEmail = (dispatch, firebase, newEmail, updateInProfile) => {
dispatch({ type: actionTypes.EMAIL_UPDATE_START, payload: newEmail })
if (!firebase.auth().currentUser) {
const msg = 'User must be logged in to update email.'
dispatch({ type: actionTypes.EMAIL_UPDATE_ERROR, payload: msg })
return Promise.reject(msg)
}
return firebase.auth().currentUser
.updateEmail(newEmail)
.then((payload) => {
dispatch({ type: actionTypes.EMAIL_UPDATE_SUCCESS, payload: newEmail })
if (updateInProfile) {
return updateProfile(dispatch, firebase, { email: newEmail })
}
return payload
})
.catch((payload) => {
dispatch({ type: actionTypes.EMAIL_UPDATE_ERROR, payload })
return Promise.reject(payload)
})
}