0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-03-15 17:21:17 -05:00

Improved login process and profile loading.

This commit is contained in:
Andrey Antukh 2016-03-31 19:14:06 +03:00
parent 3875780440
commit f837835be4
7 changed files with 61 additions and 34 deletions

View file

@ -17,6 +17,35 @@
[uxbox.locales :refer (tr)]
[uxbox.ui.messages :as uum]))
;; --- Profile Fetched
(defrecord ProfileFetched [data]
rs/UpdateEvent
(-apply-update [this state]
(assoc state :profile data)))
(defn profile-fetched
[data]
(ProfileFetched. data))
;; --- Fetch Profile
(defrecord FetchProfile []
rs/WatchEvent
(-apply-watch [_ state s]
(println "FetchProfile")
(letfn [(on-error [err]
(uum/error (tr "errors.profile-fetch"))
(rx/empty))]
(->> (rp/do :fetch/profile)
(rx/catch on-error)
(rx/map :payload)
(rx/map profile-fetched)))))
(defn fetch-profile
[]
(FetchProfile.))
;; --- Logged In
(defrecord LoggedIn [data]
@ -32,6 +61,10 @@
(-apply-effect [this state]
(assoc! local-storage :uxbox/auth data)))
(defn logged-in?
[v]
(instance? LoggedIn v))
(defn logged-in
[data]
(LoggedIn. data))
@ -47,10 +80,12 @@
(let [params {:username username
:password password
:scope "webapp"}]
(->> (rp/do :login params)
(->> (rp/do :fetch/token params)
(rx/catch on-error)
(rx/map :payload)
(rx/map logged-in)
(rx/catch on-error))))))
(rx/mapcat #(rx/of (logged-in %)
(fetch-profile))))))))
(def ^:const ^:private +login-schema+
{:username [sc/required sc/string]

View file

@ -11,30 +11,15 @@
[uxbox.repo.core :refer (-do url send!)]
[uxbox.state :as ust]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Login
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defmethod -do :fetch/profile
[type _]
(let [url (str url "/profile/me")]
(send! {:method :get :url url})))
(defn- request-token
[params]
(defmethod -do :fetch/token
[type data]
(let [url (str url "/auth/token")]
(send! {:url url
:method :post
:auth false
:body params})))
(defn- request-profile
[]
(rx/of {:fullname "Cirilla Fiona"
:photo "/images/favicon.png"
:username "cirilla"
:email "cirilla@uxbox.io"}))
(defmethod -do :login
[type data]
(->> (rx/zip (request-token data)
(request-profile))
(rx/map (fn [[authdata profile]]
(println authdata profile)
(println authdata profile)
(merge authdata profile)))))
:body data})))

View file

@ -18,6 +18,7 @@
:project-filter ""}
:route nil
:auth (:uxbox/auth local-storage)
:profile nil
:workspace nil
:shapes-by-id {}
:elements-by-id {}

View file

@ -15,6 +15,7 @@
[uxbox.router :as r]
[uxbox.rstore :as rs]
[uxbox.data.projects :as dp]
[uxbox.data.auth :as uda]
[uxbox.ui.lightbox :as ui-lightbox]
[uxbox.ui.auth :as ui-auth]
[uxbox.ui.dashboard :as ui-dashboard]
@ -56,9 +57,16 @@
nil
))))
(defn app-will-mount
[own]
(rs/emit! (uda/fetch-profile)
(dp/fetch-projects))
own)
(def app
(mx/component
{:render app-render
:will-mount app-will-mount
:mixins [rum/reactive]
:name "app"}))

View file

@ -32,8 +32,7 @@
(defn projects-page-will-mount
[own]
(rs/emit! (dd/initialize :dashboard/projects)
(dp/fetch-projects))
(rs/emit! (dd/initialize :dashboard/projects))
own)
(defn projects-page-transfer-state

View file

@ -47,20 +47,21 @@
;; User Widget
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(def ^:static user-l
(as-> (l/in [:auth]) $
(def ^:static profile-l
(as-> (l/key :profile) $
(l/focus-atom $ s/state)))
(defn user-render
[own]
(let [user (rum/react user-l)
(let [profile (rum/react profile-l)
local (:rum/local own)]
(println "user-render" profile)
(html
[:div.user-zone {:on-mouse-enter #(swap! local assoc :open true)
:on-mouse-leave #(swap! local assoc :open false)}
[:span (:fullname user)]
[:span (:fullname profile)]
[:img {:border "0"
:src (:photo user)}]
:src (:photo profile "/images/favicon.png")}]
(user-menu (:open @local))])))
(def user

View file

@ -34,11 +34,9 @@
[own]
(let [[projectid pageid] (:rum/props own)]
(rs/emit! (dw/initialize projectid pageid)
(dp/fetch-projects)
(udp/fetch-pages projectid)
(udh/fetch-page-history pageid)
(udh/fetch-pinned-page-history pageid))
own))
(defn- workspace-did-mount