|
92 | 92 | (assoc :message (tr "errors.email-invalid")))))) |
93 | 93 |
|
94 | 94 | (mf/defc login-form |
95 | | - [{:keys [params on-success-callback] :as props}] |
| 95 | + [{:keys [params on-success] :as props}] |
96 | 96 | (let [initial (mf/use-memo (mf/deps params) (constantly params)) |
97 | 97 |
|
98 | 98 | totp* (mf/use-state false) |
|
104 | 104 | :initial initial) |
105 | 105 |
|
106 | 106 | on-error |
107 | | - (fn [cause] |
108 | | - (when (map? cause) |
109 | | - (err/print-trace! cause) |
110 | | - (err/print-data! cause) |
111 | | - (err/print-explain! cause)) |
112 | | - |
113 | | - (cond |
114 | | - (and (= :totp (:code cause)) |
115 | | - (= :negotiation (:type cause))) |
116 | | - (do |
117 | | - (reset! totp* true) |
118 | | - (reset! error (tr "errors.missing-totp")) |
119 | | - (swap! form (fn [form] |
120 | | - (-> form |
121 | | - (update :errors assoc :totp {:message (tr "errors.missing-totp")}) |
122 | | - (update :touched assoc :totp true))))) |
123 | | - |
124 | | - |
125 | | - (and (= :restriction (:type cause)) |
126 | | - (= :passkey-disabled (:code cause))) |
127 | | - (reset! error (tr "errors.wrong-credentials")) |
128 | | - |
129 | | - (and (= :restriction (:type cause)) |
130 | | - (= :profile-blocked (:code cause))) |
131 | | - (reset! error (tr "errors.profile-blocked")) |
132 | | - |
133 | | - (and (= :restriction (:type cause)) |
134 | | - (= :admin-only-profile (:code cause))) |
135 | | - (reset! error (tr "errors.profile-blocked")) |
136 | | - |
137 | | - (and (= :validation (:type cause)) |
138 | | - (= :wrong-credentials (:code cause))) |
139 | | - (reset! error (tr "errors.wrong-credentials")) |
140 | | - |
141 | | - (and (= :validation (:type cause)) |
142 | | - (= :account-without-password (:code cause))) |
143 | | - (reset! error (tr "errors.wrong-credentials")) |
144 | | - |
145 | | - :else |
146 | | - (reset! error (tr "errors.generic")))) |
| 107 | + (mf/use-fn |
| 108 | + (fn [form cause] |
| 109 | + (when (map? cause) |
| 110 | + (err/print-trace! cause) |
| 111 | + (err/print-data! cause) |
| 112 | + (err/print-explain! cause)) |
| 113 | + |
| 114 | + (cond |
| 115 | + |
| 116 | + (and (= :totp (:code cause)) |
| 117 | + (= :negotiation (:type cause))) |
| 118 | + (reset! totp* true) |
| 119 | + |
| 120 | + (and (= :invalid-totp (:code cause)) |
| 121 | + (= :negotiation (:type cause))) |
| 122 | + (do |
| 123 | + ;; (reset! error (tr "errors.invalid-totp")) |
| 124 | + (swap! form (fn [form] |
| 125 | + (-> form |
| 126 | + (update :errors assoc :totp {:message (tr "errors.invalid-totp")}) |
| 127 | + (update :touched assoc :totp true))))) |
| 128 | + |
| 129 | + |
| 130 | + (and (= :restriction (:type cause)) |
| 131 | + (= :passkey-disabled (:code cause))) |
| 132 | + (reset! error (tr "errors.wrong-credentials")) |
| 133 | + |
| 134 | + (and (= :restriction (:type cause)) |
| 135 | + (= :profile-blocked (:code cause))) |
| 136 | + (reset! error (tr "errors.profile-blocked")) |
| 137 | + |
| 138 | + (and (= :restriction (:type cause)) |
| 139 | + (= :admin-only-profile (:code cause))) |
| 140 | + (reset! error (tr "errors.profile-blocked")) |
| 141 | + |
| 142 | + (and (= :validation (:type cause)) |
| 143 | + (= :wrong-credentials (:code cause))) |
| 144 | + (reset! error (tr "errors.wrong-credentials")) |
| 145 | + |
| 146 | + (and (= :validation (:type cause)) |
| 147 | + (= :account-without-password (:code cause))) |
| 148 | + (reset! error (tr "errors.wrong-credentials")) |
| 149 | + |
| 150 | + :else |
| 151 | + (reset! error (tr "errors.generic"))))) |
147 | 152 |
|
148 | 153 | on-success-default |
149 | | - (fn [data] |
150 | | - (when-let [token (:invitation-token data)] |
151 | | - (st/emit! (rt/nav :auth-verify-token {} {:token token})))) |
| 154 | + (mf/use-fn |
| 155 | + (fn [data] |
| 156 | + (when-let [token (:invitation-token data)] |
| 157 | + (st/emit! (rt/nav :auth-verify-token {} {:token token}))))) |
152 | 158 |
|
153 | 159 | on-success |
154 | | - (fn [data] |
155 | | - (if (nil? on-success-callback) |
156 | | - (on-success-default data) |
157 | | - (on-success-callback))) |
| 160 | + (mf/use-fn |
| 161 | + (mf/deps on-success) |
| 162 | + (fn [data] |
| 163 | + (if (fn? on-success) |
| 164 | + (on-success data) |
| 165 | + (on-success-default data)))) |
158 | 166 |
|
159 | 167 | on-submit |
160 | 168 | (mf/use-fn |
161 | 169 | (fn [form event] |
162 | 170 | (let [event (dom/event->native-event event) |
163 | 171 | submitter (unchecked-get event "submitter") |
164 | | - submitter (dom/get-data submitter "role")] |
| 172 | + submitter (dom/get-data submitter "role") |
| 173 | + on-error (partial on-error form) |
| 174 | + on-success (partial on-success form)] |
165 | 175 |
|
166 | 176 | (case submitter |
167 | 177 | "login-with-passkey" |
|
175 | 185 | {:on-error on-error |
176 | 186 | :on-success on-success})] |
177 | 187 | (st/emit! (du/login-with-password params))) |
| 188 | + |
178 | 189 | nil)))) |
179 | 190 |
|
180 | 191 | on-submit-ldap |
|
284 | 295 | (tr "auth.login-with-oidc-submit")]])) |
285 | 296 |
|
286 | 297 | (mf/defc login-methods |
287 | | - [{:keys [params on-success-callback] :as props}] |
| 298 | + [{:keys [params on-success] :as props}] |
288 | 299 | [:* |
289 | 300 | (when show-alt-login-buttons? |
290 | 301 | [:* |
|
306 | 317 | (when (or (contains? cf/flags :login) |
307 | 318 | (contains? cf/flags :login-with-password) |
308 | 319 | (contains? cf/flags :login-with-ldap)) |
309 | | - [:& login-form {:params params :on-success-callback on-success-callback}])]) |
| 320 | + [:& login-form {:params params :on-success on-success}])]) |
310 | 321 |
|
311 | 322 | (mf/defc login-page |
312 | | - [{:keys [params] :as props}] |
313 | | - [:div.generic-form.login-form |
314 | | - [:div.form-container |
315 | | - [:h1 {:data-test "login-title"} (tr "auth.login-title")] |
316 | | - |
317 | | - [:& login-methods {:params params}] |
318 | | - |
319 | | - [:div.links |
320 | | - (when (or (contains? cf/flags :login) |
321 | | - (contains? cf/flags :login-with-password)) |
322 | | - [:div.link-entry |
323 | | - [:& lk/link {:action #(st/emit! (rt/nav :auth-recovery-request)) |
324 | | - :data-test "forgot-password"} |
325 | | - (tr "auth.forgot-password")]]) |
326 | | - |
327 | | - (when (contains? cf/flags :registration) |
328 | | - [:div.link-entry |
329 | | - [:span (tr "auth.register") " "] |
330 | | - [:& lk/link {:action #(st/emit! (rt/nav :auth-register {} params)) |
331 | | - :data-test "register-submit"} |
332 | | - (tr "auth.register-submit")]])] |
333 | | - |
334 | | - (when (contains? cf/flags :demo-users) |
335 | | - [:div.links.demo |
336 | | - [:div.link-entry |
337 | | - [:span (tr "auth.create-demo-profile") " "] |
338 | | - [:& lk/link {:action #(st/emit! (du/create-demo-profile)) |
339 | | - :data-test "demo-account-link"} |
340 | | - (tr "auth.create-demo-account")]]])]]) |
| 323 | + {::mf/wrap-props false} |
| 324 | + [{:keys [params]}] |
| 325 | + (let [nav-to-recovery (mf/use-fn #(st/emit! (rt/nav :auth-recovery-request))) |
| 326 | + nav-to-register (mf/use-fn (mf/deps params) #(st/emit! (rt/nav :auth-register {} params))) |
| 327 | + create-demo (mf/use-fn #(st/emit! (du/create-demo-profile)))] |
| 328 | + |
| 329 | + [:div.generic-form.login-form |
| 330 | + [:div.form-container |
| 331 | + [:h1 {:data-test "login-title"} (tr "auth.login-title")] |
| 332 | + |
| 333 | + [:& login-methods {:params params}] |
| 334 | + |
| 335 | + [:div.links |
| 336 | + (when (or (contains? cf/flags :login) |
| 337 | + (contains? cf/flags :login-with-password)) |
| 338 | + [:div.link-entry |
| 339 | + [:& lk/link {:on-click nav-to-recovery |
| 340 | + :data-test "forgot-password"} |
| 341 | + (tr "auth.forgot-password")]]) |
| 342 | + |
| 343 | + (when (contains? cf/flags :registration) |
| 344 | + [:div.link-entry |
| 345 | + [:span (tr "auth.register") " "] |
| 346 | + [:& lk/link {:on-click nav-to-register |
| 347 | + :data-test "register-submit"} |
| 348 | + (tr "auth.register-submit")]])] |
| 349 | + |
| 350 | + (when (contains? cf/flags :demo-users) |
| 351 | + [:div.links.demo |
| 352 | + [:div.link-entry |
| 353 | + [:span (tr "auth.create-demo-profile") " "] |
| 354 | + [:& lk/link {:on-click create-demo |
| 355 | + :data-test "demo-account-link"} |
| 356 | + (tr "auth.create-demo-account")]]])]])) |
0 commit comments