/
admin.clj
169 lines (148 loc) · 7.59 KB
/
admin.clj
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
(ns charmander.admin
(:require [clojure.java.io :as io]
[environ.core :refer [env]])
(:import com.google.auth.oauth2.GoogleCredentials
com.google.firebase.FirebaseApp
com.google.firebase.FirebaseOptions
com.google.firebase.FirebaseOptions$Builder
com.google.firebase.auth.FirebaseAuth
com.google.firebase.auth.FirebaseAuthException
com.google.firebase.auth.UserRecord
com.google.firebase.auth.UserRecord$CreateRequest
com.google.firebase.auth.UserRecord$UpdateRequest
;firestore
com.google.firebase.cloud.FirestoreClient
com.google.cloud.firestore.Firestore
com.google.cloud.firestore.QueryDocumentSnapshot
com.google.cloud.firestore.QuerySnapshot
com.google.cloud.firestore.DocumentReference
com.google.cloud.firestore.DocumentSnapshot
com.google.cloud.firestore.WriteResult
com.google.api.core.ApiFuture
com.google.cloud.firestore.CollectionReference)
(:gen-class))
; private methods
(defn- string->stream
([s] (string->stream s "UTF-8"))
([s encoding]
(-> s
(.getBytes encoding)
(java.io.ByteArrayInputStream.))))
(defn- build-firebase-options
([]
(try
(-> (new FirebaseOptions$Builder) ;use thread-first when the final part of the function will return value to be used
(.setCredentials (GoogleCredentials/fromStream (string->stream (env :firebase-config))));(io/input-stream key-file-json)))
(.build))
(catch Exception e (println "\nError: FIREBASE_CONFIG AND GOOGLE_CLOUD_PROJECT environment variables must both be set"))))
([database-name]
(try
(-> (new FirebaseOptions$Builder) ;use thread-first when the final part of the function will return value to be used
(.setCredentials (GoogleCredentials/fromStream (string->stream (env :firebase-config))));(io/input-stream key-file-json)))
(.setDatabaseUrl (str "https://" database-name ".firebaseio.com"))
(.build))
(catch Exception e (println "\nError: FIREBASE_CONFIG AND GOOGLE_CLOUD_PROJECT environment variables must both be set")))))
(defn- build-create-user-request [email password]
(let [create-request (new UserRecord$CreateRequest)]
(doto create-request ;doto mutates the object. Use it when you're going to return the object
(.setEmail email)
(.setPassword password)
(.setEmailVerified false)
(.setDisabled false))))
(defn- convert-user-record-to-map [^UserRecord user-record]
{ :email (. user-record getEmail)
:email-verified (. user-record isEmailVerified)
:uid (. user-record getUid)
:provider-id (. user-record getProviderId)
:photo-url (. user-record getPhotoUrl)
:phone-number (. user-record getPhoneNumber)
:display-name (. user-record getDisplayName)
:disabled (. user-record isDisabled)})
; public methods
; init admin api
(defn init []
(let [database-name (env :firebase-database)]
(if (nil? database-name)
(try
(. FirebaseAuth getInstance)
(catch IllegalStateException ise
(let [options (build-firebase-options)]
(. FirebaseApp initializeApp options)))))
(try
(. FirebaseAuth getInstance)
(catch IllegalStateException ise
(let [options (build-firebase-options database-name)]
(. FirebaseApp initializeApp options))))))
; user management api
(defn create-user [email password]
(let [firebase-auth (. FirebaseAuth getInstance)
create-request (build-create-user-request email password)]
(try
(convert-user-record-to-map (. firebase-auth createUser create-request))
(catch FirebaseAuthException fae {:error true :error-code (. fae getErrorCode)}))))
(defn delete-user [uuid]
(let [firebase-auth (. FirebaseAuth getInstance)]
(try
(. firebase-auth deleteUser uuid)
(catch IllegalArgumentException iae {:error true :error-code "invalid-uid"})
(catch FirebaseAuthException fae {:error true :error-code (. fae getErrorCode)}))))
(defn get-user [uuid]
(let [firebase-auth (. FirebaseAuth getInstance)]
(try
(convert-user-record-to-map (. firebase-auth getUser uuid))
(catch FirebaseAuthException fae {:error true :error-code (. fae getErrorCode)}))))
(defn get-user-by-email [email]
(let [firebase-auth (. FirebaseAuth getInstance)]
(try
(convert-user-record-to-map (. firebase-auth getUserByEmail email))
(catch FirebaseAuthException fae {:error true :error-code (. fae getErrorCode)}))))
(defn get-user-by-phone-number [phone-number]
(let [firebase-auth (. FirebaseAuth getInstance)]
(try
(convert-user-record-to-map (. firebase-auth getUserByPhoneNumber phone-number))
(catch FirebaseAuthException fae {:error true :error-code (. fae getErrorCode)}))))
(defn set-user-email [uuid email]
(let [firebase-auth (. FirebaseAuth getInstance)
update-request (new UserRecord$UpdateRequest uuid)]
(try
(convert-user-record-to-map (. firebase-auth updateUser (doto update-request (.setEmail email) (.setEmailVerified false))))
(catch IllegalArgumentException iae {:error true :error-code "invalid-email"})
(catch FirebaseAuthException fae {:error true :error-code (. fae getErrorCode)}))))
(defn set-user-password [uuid password]
(let [firebase-auth (. FirebaseAuth getInstance)
update-request (new UserRecord$UpdateRequest uuid)]
(try
(convert-user-record-to-map (. firebase-auth updateUser (doto update-request (.setPassword password))))
(catch IllegalArgumentException iae {:error true :error-code "invalid-password"})
(catch FirebaseAuthException fae {:error true :error-code (. fae getErrorCode)}))))
(defn set-user-phone-number [uuid phone-number]
(let [firebase-auth (. FirebaseAuth getInstance)
update-request (new UserRecord$UpdateRequest uuid)]
(try
(convert-user-record-to-map (. firebase-auth updateUser (doto update-request (.setPhoneNumber phone-number))))
(catch IllegalArgumentException iae {:error true :error-code "invalid-phone-number"})
(catch FirebaseAuthException fae {:error true :error-code (. fae getErrorCode)}))))
(defn set-user-display-name [uuid display-name]
(let [firebase-auth (. FirebaseAuth getInstance)
update-request (new UserRecord$UpdateRequest uuid)]
(try
(convert-user-record-to-map (. firebase-auth updateUser (doto update-request (.setDisplayName display-name))))
(catch IllegalArgumentException iae {:error true :error-code "invalid-display-name"})
(catch FirebaseAuthException fae {:error true :error-code (. fae getErrorCode)}))))
(defn set-user-photo-url [uuid photo-url]
(let [firebase-auth (. FirebaseAuth getInstance)
update-request (new UserRecord$UpdateRequest uuid)]
(try
(convert-user-record-to-map (. firebase-auth updateUser (doto update-request (.setPhotoUrl photo-url))))
(catch IllegalArgumentException iae {:error true :error-code "invalid-photo-url"})
(catch FirebaseAuthException fae {:error true :error-code (. fae getErrorCode)}))))
(defn generate-password-reset-link [email]
(let [firebase-auth (. FirebaseAuth getInstance)]
(try
(. firebase-auth generatePasswordResetLink email)
(catch FirebaseAuthException fae {:error true :error-code (. fae getErrorCode)}))))
(defn generate-email-verification-link [email]
(let [firebase-auth (. FirebaseAuth getInstance)]
(try
(. firebase-auth generateEmailVerificationLink email)
(catch FirebaseAuthException fae {:error true :error-code (. fae getErrorCode)}))))