diff --git a/frontend/src/app/main/data/users.cljs b/frontend/src/app/main/data/users.cljs index b4eae4da6..f66c0af21 100644 --- a/frontend/src/app/main/data/users.cljs +++ b/frontend/src/app/main/data/users.cljs @@ -170,13 +170,15 @@ (get-redirect-event)) (rx/observe-on :async))))))) +(s/def ::invitation-token ::us/not-empty-string) (s/def ::login-params - (s/keys :req-un [::email ::password])) + (s/keys :req-un [::email ::password] + :opt-un [::invitation-token])) (declare login-from-register) (defn login - [{:keys [email password] :as data}] + [{:keys [email password invitation-token] :as data}] (us/verify ::login-params data) (ptk/reify ::login ptk/WatchEvent @@ -184,9 +186,10 @@ (let [{:keys [on-error on-success] :or {on-error rx/throw on-success identity}} (meta data) + params {:email email :password password - :scope "webapp"}] + :invitation-token invitation-token}] ;; NOTE: We can't take the profile value from login because ;; there are cases when login is successfull but the cookie is @@ -197,31 +200,32 @@ ;; the returned profile is an NOT authenticated profile, we ;; proceed to logout and show an error message. - (rx/merge - (->> (rp/mutation :login params) - (rx/map fetch-profile) - (rx/catch on-error)) + (->> (rp/mutation :login (d/without-nils params)) + (rx/merge-map (fn [data] + (rx/merge + (rx/of (fetch-profile)) + (->> stream + (rx/filter profile-fetched?) + (rx/take 1) + (rx/map deref) + (rx/filter (complement is-authenticated?)) + (rx/tap on-error) + (rx/map #(ex/raise :type :authentication)) + (rx/observe-on :async)) - (->> stream - (rx/filter profile-fetched?) - (rx/take 1) - (rx/map deref) - (rx/filter (complement is-authenticated?)) - (rx/tap on-error) - (rx/map #(ex/raise :type :authentication)) - (rx/observe-on :async)) + (->> stream + (rx/filter profile-fetched?) + (rx/take 1) + (rx/map deref) + (rx/filter is-authenticated?) + (rx/map (fn [profile] + (with-meta (merge data profile) + {::ev/source "login"}))) + (rx/tap on-success) + (rx/map logged-in) + (rx/observe-on :async))))) + (rx/catch on-error)))))) - (->> stream - (rx/filter profile-fetched?) - (rx/take 1) - (rx/map deref) - (rx/filter is-authenticated?) - (rx/map (fn [profile] - (with-meta profile - {::ev/source "login"}))) - (rx/tap on-success) - (rx/map logged-in) - (rx/observe-on :async))))))) (defn login-from-token [{:keys [profile] :as tdata}] diff --git a/frontend/src/app/main/ui/auth/login.cljs b/frontend/src/app/main/ui/auth/login.cljs index 58d4f97ec..936d5ebb0 100644 --- a/frontend/src/app/main/ui/auth/login.cljs +++ b/frontend/src/app/main/ui/auth/login.cljs @@ -30,9 +30,11 @@ (s/def ::email ::us/email) (s/def ::password ::us/not-empty-string) +(s/def ::invitation-token ::us/not-empty-string) (s/def ::login-form - (s/keys :req-un [::email ::password])) + (s/keys :req-un [::email ::password] + :opt-un [::invitation-token])) (defn- login-with-oauth [event provider params] @@ -62,29 +64,39 @@ (mf/defc login-form [{:keys [params] :as props}] - (let [error (mf/use-state false) - form (fm/use-form :spec ::login-form - :inital {}) + (let [initial (mf/use-memo (mf/deps params) (constantly params)) + + error (mf/use-state false) + form (fm/use-form :spec ::login-form :initial initial) on-error (fn [_] (reset! error (tr "errors.wrong-credentials"))) + on-succes + (fn [data] + (prn "SUCCESS" data) + (when-let [token (:invitation-token data)] + (st/emit! (rt/nav :auth-verify-token {} {:token token})))) + on-submit (mf/use-callback - (mf/deps form) - (fn [_] + (fn [form _event] (reset! error nil) (let [params (with-meta (:clean-data @form) - {:on-error on-error})] + {:on-error on-error + :on-success on-succes})] (st/emit! (du/login params))))) on-submit-ldap (mf/use-callback (mf/deps form) (fn [event] - (let [params (merge (:clean-data @form) params)] - (login-with-ldap event (with-meta params {:on-error on-error})))))] + (reset! error nil) + (let [params (:clean-data @form)] + (login-with-ldap event (with-meta params + {:on-error on-error + :on-success on-succes})))))] [:* (when-let [message @error] diff --git a/frontend/src/app/main/ui/auth/register.cljs b/frontend/src/app/main/ui/auth/register.cljs index c9a2d89a8..84ed3de24 100644 --- a/frontend/src/app/main/ui/auth/register.cljs +++ b/frontend/src/app/main/ui/auth/register.cljs @@ -60,7 +60,7 @@ :email-already-exists (swap! form assoc-in [:errors :email] {:message "errors.email-already-exists"}) - + :email-as-password (swap! form assoc-in [:errors :password] {:message "errors.email-as-password"}) diff --git a/frontend/src/app/main/ui/components/forms.cljs b/frontend/src/app/main/ui/components/forms.cljs index 8d744090b..54022b3fa 100644 --- a/frontend/src/app/main/ui/components/forms.cljs +++ b/frontend/src/app/main/ui/components/forms.cljs @@ -174,7 +174,6 @@ [{:keys [options label form default data-test] :as props :or {default ""}}] (let [input-name (get props :name) - form (or form (mf/use-ctx form-ctx)) value (or (get-in @form [:data input-name]) default) cvalue (d/seek #(= value (:value %)) options)