Saves email address and profile photos
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
(ns auto-ap.datomic.users
|
||||
(:require
|
||||
[auto-ap.datomic :refer [conn]]
|
||||
[datomic.api :as dc]))
|
||||
[datomic.api :as dc]
|
||||
[datomic.api :as d]))
|
||||
|
||||
(defn add-arg [query name value where & rest]
|
||||
(let [query (-> query
|
||||
@@ -19,27 +20,27 @@
|
||||
(map #(update % :user/role :db/ident))
|
||||
first))
|
||||
|
||||
(defn find-or-insert! [{:keys [:user/provider :user/provider-id] :as new-user}]
|
||||
|
||||
(defn find-or-insert! [{:keys [:user/provider :user/provider-id ] :as new-user}]
|
||||
(let [is-first-user? (not (seq (dc/q [:find '?e
|
||||
:in '$
|
||||
:where '[?e :user/provider]]
|
||||
(dc/db conn))))
|
||||
user (some-> (dc/q [:find '(pull ?e [*
|
||||
{:user/clients [*]}
|
||||
{:user/role [:db/ident]}])
|
||||
:in '$ '?provider '?provider-id
|
||||
:where '[?e :user/provider ?provider]
|
||||
'[?e :user/provider-id ?provider-id]]
|
||||
(dc/db conn) provider provider-id)
|
||||
first
|
||||
first
|
||||
(update :user/role :db/ident))]
|
||||
(if user
|
||||
user
|
||||
(let [new-user-trans @(dc/transact conn [(cond-> new-user
|
||||
true (assoc :db/id "user")
|
||||
is-first-user? (assoc :user/role :user-role/admin))])]
|
||||
(get-by-id (-> new-user-trans :tempids (get "user")))))))
|
||||
user-id (ffirst (dc/q '[:find ?e
|
||||
:in $ ?provider ?provider-id
|
||||
:where [?e :user/provider ?provider]
|
||||
[?e :user/provider-id ?provider-id]]
|
||||
(dc/db conn) provider provider-id))
|
||||
result @(dc/transact conn [[:upsert-entity (cond-> (assoc new-user :db/id (or user-id "user"))
|
||||
(not user-id) (assoc :user/role :user-role/none)
|
||||
is-first-user? (assoc :user/role :user-role/admin))]])
|
||||
user-id (or user-id (get-in result [:tempids "user"]))]
|
||||
(update (dc/pull (dc/db conn)
|
||||
'[*
|
||||
{:user/clients [*]}
|
||||
{:user/role [:db/ident]}]
|
||||
user-id)
|
||||
:user/role :db/ident)))
|
||||
|
||||
(defn raw-graphql [_]
|
||||
(->> (dc/q {:find ['(pull ?e [*
|
||||
|
||||
@@ -236,6 +236,8 @@
|
||||
:user
|
||||
{:fields {:id {:type :id}
|
||||
:name {:type 'String}
|
||||
:profile_image_url {:type 'String}
|
||||
:email {:type 'String}
|
||||
:role {:type :role}
|
||||
:clients {:type '(list :client)}}}
|
||||
|
||||
|
||||
@@ -5,7 +5,8 @@
|
||||
[clj-http.client :as http]
|
||||
[clj-time.core :as time]
|
||||
[clojure.tools.logging :as log]
|
||||
[config.core :refer [env]]))
|
||||
[config.core :refer [env]]
|
||||
[com.brunobonacci.mulog :as mu]))
|
||||
|
||||
(def google-client-id "264081895820-0nndcfo3pbtqf30sro82vgq5r27h8736.apps.googleusercontent.com")
|
||||
(def google-client-secret "OC-WemHurPXYpuIw5cT-B90g")
|
||||
@@ -30,13 +31,17 @@
|
||||
"grant_type" "authorization_code"}
|
||||
:as :json})
|
||||
:body)
|
||||
|
||||
token (:access_token auth)
|
||||
profile (-> (http/get "https://www.googleapis.com/oauth2/v1/userinfo"
|
||||
{:headers {"Authorization" (str "Bearer " token)} :as :json})
|
||||
:body)
|
||||
_ (mu/log ::got-profile
|
||||
:profile profile)
|
||||
user (users/find-or-insert! {:user/provider "google"
|
||||
:user/provider-id (:id profile)
|
||||
:user/role :user-role/none
|
||||
:user/email (:email profile)
|
||||
:user/profile-image-url (:picture profile)
|
||||
:user/name (:name profile)})
|
||||
auth {:user (:name profile)
|
||||
:exp (time/plus (time/now) (time/days 30))
|
||||
@@ -44,9 +49,10 @@
|
||||
(select-keys c [:client/code :db/id :client/locations]))
|
||||
(:user/clients user))
|
||||
:user/role (name (:user/role user))
|
||||
:user/profile-image-url (:picture profile)
|
||||
:user/name (:name profile)}
|
||||
]
|
||||
(log/info "authenticated as user" user)
|
||||
_ (mu/log ::logged-in-as
|
||||
:auth auth)]
|
||||
;; TODO - these namespaces are not being transmitted/deserialized properly
|
||||
|
||||
(if (and token user)
|
||||
|
||||
@@ -37,6 +37,8 @@
|
||||
:owns-state {:single ::page}
|
||||
:query-obj {:venia/queries [[:user
|
||||
[:name
|
||||
:profile_image_url
|
||||
:email
|
||||
:id
|
||||
:role
|
||||
[:clients [:id :name]]]]]}
|
||||
|
||||
@@ -29,6 +29,7 @@
|
||||
[grid/header
|
||||
[grid/row {}
|
||||
[grid/header-cell {} "User"]
|
||||
[grid/header-cell {} "Email"]
|
||||
[grid/header-cell {} "Role"]
|
||||
[grid/header-cell {} "Clients"]
|
||||
[grid/header-cell {:style {:width (action-cell-width 1)}} ]]]
|
||||
@@ -36,7 +37,16 @@
|
||||
(for [{:keys [id name role clients] :as c} (:data page)]
|
||||
^{:key (str name "-" id )}
|
||||
[grid/row {:class (:class c) :id id}
|
||||
[grid/cell {} name]
|
||||
[grid/cell {}
|
||||
[:div.level
|
||||
[:div.level-left
|
||||
(when-let [url (:profile-image-url c)]
|
||||
[:div.level-item
|
||||
[:figure.image.is-24x24
|
||||
[:img.is-rounded {:src url
|
||||
:referrer-policy= "no-referrer" }]]])
|
||||
[:div.level-item name]]]]
|
||||
[grid/cell {} (:email c)]
|
||||
[grid/cell {} role]
|
||||
[grid/cell {} (str/join ", " (map :name clients))]
|
||||
[grid/cell {}
|
||||
|
||||
Reference in New Issue
Block a user