forked from prescottprue/redux-firebasev3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompose.js
126 lines (98 loc) · 3.57 KB
/
compose.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
import Firebase from 'firebase'
import { authActions, queryActions } from './actions'
export default (config, otherConfig) =>
next => (reducer, initialState) => {
const defaultConfig = {
userProfile: null,
enableLogging: false
}
const store = next(reducer, initialState)
const { dispatch } = store
const { apiKey, authDomain, databaseURL, storageBucket } = config
// Throw for missing Firebase Data
if (!databaseURL) throw new Error('Firebase databaseURL is required')
if (!authDomain) throw new Error('Firebase authDomain is required')
if (!apiKey) throw new Error('Firebase apiKey is required')
// Combine all configs
const configs = Object.assign({}, defaultConfig, config, otherConfig)
// Initialize Firebase
try {
Firebase.initializeApp({apiKey, authDomain, databaseURL, storageBucket})
} catch (err) {}
// TODO: Handle firebasev2 logging
// Enable Logging based on config
if (configs.enableLogging) {
Firebase.database.enableLogging(configs.enableLogging)
}
const ref = Firebase.database().ref()
const firebase = Object.defineProperty(Firebase, '_', {
value: {
watchers: {},
config: configs,
authUid: null
},
writable: true,
enumerable: true,
configurable: true
})
const set = (path, value, onComplete) =>
ref.child(path).set(value, onComplete)
const push = (path, value, onComplete) =>
ref.child(path).push(value, onComplete)
const update = (path, value, onComplete) =>
ref.child(path).update(value, onComplete)
const remove = (path, onComplete) =>
ref.child(path).remove(onComplete)
const uniqueSet = (path, value, onComplete) =>
ref.child(path)
.once('value')
.then(snap => {
if (snap.val && snap.val() !== null) {
const err = new Error('Path already exists.')
if (onComplete) onComplete(err)
return Promise.reject(err)
}
return ref.child(path).set(value, onComplete)
})
const watchEvent = (eventName, eventPath) =>
queryActions.watchEvent(firebase, dispatch, eventName, eventPath, true)
const unWatchEvent = (eventName, eventPath, queryId = undefined) =>
queryActions.unWatchEvent(firebase, eventName, eventPath, queryId)
const login = credentials =>
authActions.login(dispatch, firebase, credentials)
const logout = () =>
authActions.logout(dispatch, firebase)
const createUser = (credentials, profile) =>
authActions.createUser(dispatch, firebase, credentials, profile)
const resetPassword = (credentials) =>
authActions.resetPassword(dispatch, firebase, credentials)
const verifyPasswordResetCode = (code) =>
authActions.verifyPasswordResetCode(dispatch, firebase, code)
const confirmPasswordReset = (code, newPassword) =>
authActions.confirmPasswordReset(dispatch, firebase, code, newPassword)
const applyActionCode = (code) =>
authActions.applyActionCode(dispatch, firebase, code)
const checkActionCode = (code) =>
authActions.checkActionCode(dispatch, firebase, code)
firebase.helpers = {
set,
uniqueSet,
push,
remove,
update,
login,
logout,
createUser,
resetPassword,
verifyPasswordResetCode,
confirmPasswordReset,
applyActionCode,
checkActionCode,
watchEvent,
unWatchEvent,
storage: () => Firebase.storage()
}
authActions.init(dispatch, firebase)
store.firebase = firebase
return store
}