forked from deg/re-frame-firebase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathre_frame_firebase.cljs
182 lines (158 loc) · 6.75 KB
/
re_frame_firebase.cljs
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
;;; Author: David Goldfarb ([email protected])
;;; Copyright (c) 2017, David Goldfarb
;;; Built on ideas and code from
;;; http://timothypratley.blogspot.co.il/2016/07/reacting-to-changes-with-firebase-and.html
;;; and https://github.com/timothypratley/voterx
(ns com.degel.re-frame-firebase
(:require
[com.degel.re-frame-firebase.auth :as auth]
[com.degel.re-frame-firebase.core :as core]
[re-frame.core :as re-frame]))
;;; Write a value to Firebase.
;;; See https://firebase.google.com/docs/reference/js/firebase.database.Reference#set
;;;
;;; Example FX:
;;; {:firebase/write [:path [:my :data]
;;; :value 42
;;; :on-success #(prn "Write succeeded")
;;; :on-failure [:firebase-error]]}
;;;
(re-frame/reg-fx :firebase/write core/firebase-write-effect)
;;; Write a value to a Firebase list.
;;; See https://firebase.google.com/docs/reference/js/firebase.database.Reference#push
;;;
;;; Example FX:
;;; {:firebase/push [:path [:my :collection]
;;; :value "Hello world"
;;; :on-success #(prn "Push succeeded")
;;; :on-failure [:firebase-error]]}
;;;
(re-frame/reg-fx :firebase/push core/firebase-push-effect)
;;; Asynch one-time read of a value in Firebase.
;;; See https://firebase.google.com/docs/reference/js/firebase.database.Reference#once
;;;
;;; Example FX:
;;; {:firebase/read-once [:path [:my :data]
;;; :on-success [:got-my-data]
;;; :on-failure [:firebase-error]]}
;;;
(re-frame/reg-fx :firebase/read-once core/firebase-once-effect)
;;; Dispatch a vector of firebase effects
;;;
;;;
;;; Example FX:
;;; {:firebase/multi [[:firebase/write {:path ,,,}]
;;; [:firebase/push {:path ,,,}]
;;; ,,,]}
;;;
(re-frame/reg-fx
:firebase/multi
(fn [effects]
(run! (fn [[event-type args]]
(case event-type
:firebase/write (core/firebase-write-effect args)
:firebase/push (core/firebase-push-effect args)
:firebase/read-once (core/firebase-once-effect args)
(js/alert "Internal error: unknown firebase effect: " event-type " (" args ")")))
effects)))
;;; Watch a value in Firebase.
;;; See https://firebase.google.com/docs/reference/js/firebase.database.Reference#on
;;;
;;; Subscription:
;;; (<sub {:path [:my :data]
;;; :on-failure [:firebase-error]})
;;;
(re-frame/reg-sub-raw :firebase/on-value core/firebase-on-value-sub)
;;; Login to firebase, using one of OAuth providers
;;;
;;; Accepts a map of the following options:
;;;
;;; - :sign-in-method either :redirect (default) or :popup mode
;;;
;;; - :scopes a sequence of additional OAuth scopes; supported for following auth providers:
;;; Google: https://developers.google.com/identity/protocols/googlescopes
;;; Facebook: https://developers.facebook.com/docs/facebook-login/permissions
;;; GitHub: https://developer.github.com/apps/building-integrations/setting-up-and-registering-oauth-apps/
;;;
;;; - :custom-parameters check auth providers documentation for supported values:
;;; Google: https://firebase.google.com/docs/reference/js/firebase.auth.GoogleAuthProvider#setCustomParameters
;;; Facebook: https://firebase.google.com/docs/reference/js/firebase.auth.FacebookAuthProvider#setCustomParameters
;;; Twitter: https://firebase.google.com/docs/reference/js/firebase.auth.TwitterAuthProvider#setCustomParameters
;;; GitHub: https://firebase.google.com/docs/reference/js/firebase.auth.GithubAuthProvider#setCustomParameters
;;;
;;; Example usage:
;;; FX:
;;; {firebase/google-sign-in {:sign-in-method :popup
;;; :scopes ["https://www.googleapis.com/auth/contacts.readonly"
;;; "https://www.googleapis.com/auth/calendar.readonly"]
;;; :custom-parameters {"login_hint" "[email protected]"}}}
;;;
(re-frame/reg-fx :firebase/google-sign-in auth/google-sign-in)
(re-frame/reg-fx :firebase/facebook-sign-in auth/facebook-sign-in)
(re-frame/reg-fx :firebase/twitter-sign-in auth/twitter-sign-in)
(re-frame/reg-fx :firebase/github-sign-in auth/github-sign-in)
;;; Login to firebase using email/password authentication
;;; or registers a new user for email/password authentication.
;;;
;;; Accepts a map with :email and :password
;;;
;;; Example:
;;; FX:
;;; {:firebase/email-sign-in {:email "[email protected]" :password "myverysecretpassword"}}
;;; or to create a new user:
;;; {:firebase/email-create-user {:email "[email protected]" :password "anotherverysecretpassword"}}
;;;
(re-frame/reg-fx :firebase/email-sign-in auth/email-sign-in)
(re-frame/reg-fx :firebase/email-create-user auth/email-create-user)
;;; Logout
;;;
;;; FX:
;;; {firebase/sign-out []}
;;;
(re-frame/reg-fx :firebase/sign-out auth/sign-out)
;;; Monitor connection status
;;;
(re-frame/reg-sub
:firebase/connection-state
(fn [_ _]
(re-frame/subscribe [:firebase/on-value {:path [:.info :connected]}]))
(fn [connected? _]
{:firebase/connected? (= connected? true)}))
;;; Start library and register callbacks.
;;;
;;;
;;; In Iron style, most of the parameters can be either a function or a
;;; re-frame event/sub vector. If there is a parameter, it will passed to the
;;; function or conj'd onto the vector.
;;;
;;; - :firebase-app-info - Firebase application credentials. This is the one
;;; parameter that takes a map:
;;; {:apiKey "MY-KEY-MY-KEY-MY-KEY-MY-KEY"
;;; :authDomain "my-app.firebaseapp.com"
;;; :databaseURL "https://my-app.firebaseio.com"
;;; :storageBucket "my-app.appspot.com"}
;;;
;;; - :set-user-event - Function or re-frame event that will be called back
;;; to receive and store the user object from us, when login succeeds.
;;; This object is a map that includes several fields that we need, plus
;;; the following that may be useful to the calling app:
;;; :display-name - The user's full name
;;; :email - The user's email address
;;; :photo-url - The user's photo
;;; :uid - The user's unique id, used by Firebase.
;;;
;;; - :get-user-sub - Function or re-frame subscription vector that this
;;; library will use to access the user object stored by :set-user-event
;;;
;;; - :default-error-handler - Function or re-frame event that will be called
;;; to handle any otherwise unhandled errors.
;;;
(defn init [& {:keys [firebase-app-info
get-user-sub
set-user-event
default-error-handler]}]
(core/set-firebase-state :get-user-sub get-user-sub
:set-user-event set-user-event
:default-error-handler default-error-handler)
(js/firebase.initializeApp (clj->js firebase-app-info))
(auth/init-auth))