Skip to content

Commit

Permalink
[#6] add create with specified uid (@alekcz)
Browse files Browse the repository at this point in the history
  • Loading branch information
alekcz authored Mar 23, 2024
2 parents 24e2cc1 + 1bc8bd8 commit 3bb3ee9
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 18 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,18 @@ The admin api only allows the creating of users using the email/password sign-in
;; :disabled false
;;}

(charm-admin/create-user "[email protected]" "superstrong6characterpassword" "my-very-own-custom-uuid")
;;{
;; :email [email protected]
;; :email-verified false
;; :uid my-very-own-custom-uuid
;; :provider-id firebase
;; :photo-url nil
;; :phone-number nil
;; :display-name nil
;; :disabled false
;;}

(charm-admin/get-user "vMnMJvS28kWr5pb6sByHULMLelJ3")
;;{
;; :email [email protected]
Expand Down
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(defproject alekcz/charmander "1.0.4"
(defproject alekcz/charmander "1.0.5"
:description "Charmander: a set of libraries to make working with firebase easier in clojure"
:url "https://github.com/alekcz/charmander"
:license {:name "Eclipse Public License"
Expand Down
42 changes: 29 additions & 13 deletions src/charmander/admin.clj
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,22 @@
(catch Exception e (println "\nError: FIREBASE_CONFIG/GOOGLE_APPLICATION_CREDENTIALS 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- 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))))
([email password uid]
(let [create-request (new UserRecord$CreateRequest)]
(doto create-request ;doto mutates the object. Use it when you're going to return the object
(.setUid uid)
(.setEmail email)
(.setPassword password)
(.setEmailVerified false)
(.setDisabled false)))))

(defn- convert-user-record-to-map [^UserRecord user-record]
{ :email (. user-record getEmail)
Expand Down Expand Up @@ -84,12 +93,19 @@

; user management api

(defn create-user [email password]
(try
(let [firebase-auth (. FirebaseAuth getInstance)
create-request (build-create-user-request email password)]
(convert-user-record-to-map (. firebase-auth createUser create-request)))
(catch Exception e {:error true :error-data (format-error e)})))
(defn create-user
([email password]
(try
(let [firebase-auth (. FirebaseAuth getInstance)
create-request (build-create-user-request email password)]
(convert-user-record-to-map (. firebase-auth createUser create-request)))
(catch Exception e {:error true :error-data (format-error e)})))
([email password uid]
(try
(let [firebase-auth (. FirebaseAuth getInstance)
create-request (build-create-user-request email password uid)]
(convert-user-record-to-map (. firebase-auth createUser create-request)))
(catch Exception e {:error true :error-data (format-error e)}))))

(defn delete-user [uuid]
(try
Expand Down
14 changes: 12 additions & 2 deletions test/charmander/admin_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"Template for tests"

(deftest test-tempate
(testing "Testing functionname"
(testing "Testing function name"
(let [data "" other ""]
(do
(is (= (#'charmander.admin/privatefunction inputs) answer))
Expand All @@ -40,7 +40,17 @@
(is (= (:email response) (str unique "@domain.com")))
(is (not (= response response2)))
(#'charmander.admin/delete-user (:uid response)))))))


(deftest test-create-user-with-uid
(testing "Testing the creating of new users with uid"
(let [unique (str (uuid/v1) "-" (uuid/v1))]
(let [response (#'charmander.admin/create-user (str unique "@domain.com") "superDuperSecure" unique)
response2 (#'charmander.admin/create-user (str unique "[email protected]") "superDuperSecure" unique)]
(do
(is (= (:email response) (str unique "@domain.com")))
(is (not (= response response2)))
(#'charmander.admin/delete-user unique))))))

(deftest test-all-get-users
(testing "Testing the retrieval of user by uid or by email"
(let [unique (str (uuid/v1))]
Expand Down
14 changes: 12 additions & 2 deletions test/charmander/admin_test_2.clj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"Template for tests"

(deftest test-tempate
(testing "Testing functionname"
(testing "Testing function name"
(let [data "" other ""]
(do
(is (= (#'charmander.admin/privatefunction inputs) answer))
Expand All @@ -40,7 +40,17 @@
(is (= (:email response) (str unique "@domain.com")))
(is (not (= response response2)))
(#'charmander.admin/delete-user (:uid response)))))))


(deftest test-create-user-with-uid
(testing "Testing the creating of new users with uid"
(let [unique (str (uuid/v1) "-" (uuid/v1))]
(let [response (#'charmander.admin/create-user (str unique "@domain.com") "superDuperSecure" unique)
response2 (#'charmander.admin/create-user (str unique "[email protected]") "superDuperSecure" unique)]
(do
(is (= (:email response) (str unique "@domain.com")))
(is (not (= response response2)))
(#'charmander.admin/delete-user unique))))))

(deftest test-all-get-users
(testing "Testing the retrieval of user by uid or by email"
(let [unique (str (uuid/v1))]
Expand Down

0 comments on commit 3bb3ee9

Please sign in to comment.