mirror of
https://github.com/heyarne/airsonic-ui.git
synced 2026-05-06 18:33:38 +02:00
Implement logout (closes #9)
This commit is contained in:
parent
ab7519f289
commit
fee9793934
3 changed files with 43 additions and 20 deletions
|
|
@ -24,7 +24,9 @@
|
||||||
(fn [_]
|
(fn [_]
|
||||||
db/default-db))
|
db/default-db))
|
||||||
|
|
||||||
|
;; ---
|
||||||
;; auth logic
|
;; auth logic
|
||||||
|
;; ---
|
||||||
|
|
||||||
(defn authenticate
|
(defn authenticate
|
||||||
"Tries to authenticate a user by pinging the server with credentials, saving
|
"Tries to authenticate a user by pinging the server with credentials, saving
|
||||||
|
|
@ -50,8 +52,7 @@
|
||||||
[::credentials-verified user pass])})
|
[::credentials-verified user pass])})
|
||||||
|
|
||||||
(re-frame/reg-event-fx
|
(re-frame/reg-event-fx
|
||||||
::verify-auth-response
|
::verify-auth-response verify-auth-response)
|
||||||
verify-auth-response)
|
|
||||||
|
|
||||||
(defn try-remember-user
|
(defn try-remember-user
|
||||||
"Enables skipping the auth request when credentials are saved in the
|
"Enables skipping the auth request when credentials are saved in the
|
||||||
|
|
@ -97,9 +98,20 @@
|
||||||
{:routes/navigate [::routes/main]
|
{:routes/navigate [::routes/main]
|
||||||
:show-nav-bar nil}))
|
:show-nav-bar nil}))
|
||||||
|
|
||||||
;; TODO: Test that credentials are actually taken
|
(defn logout
|
||||||
;; TODO: Move these in the future? events.cljs should just do wiring. We could
|
"Clears all credentials and redirects the user to the login page"
|
||||||
;; implement api.cljs as a completely independent module.
|
[_ _]
|
||||||
|
{:routes/navigate [::routes/login]
|
||||||
|
:routes/unset-credentials nil
|
||||||
|
:store nil
|
||||||
|
:db db/default-db})
|
||||||
|
|
||||||
|
(re-frame/reg-event-fx
|
||||||
|
::logout logout)
|
||||||
|
|
||||||
|
;; ---
|
||||||
|
;; api interaction
|
||||||
|
;; ---
|
||||||
|
|
||||||
(defn- api-url [db endpoint params]
|
(defn- api-url [db endpoint params]
|
||||||
(let [creds (:credentials db)]
|
(let [creds (:credentials db)]
|
||||||
|
|
@ -113,8 +125,7 @@
|
||||||
:on-failure [:api/bad-response]}})
|
:on-failure [:api/bad-response]}})
|
||||||
|
|
||||||
(re-frame/reg-event-fx
|
(re-frame/reg-event-fx
|
||||||
:api/request
|
:api/request api-request)
|
||||||
api-request)
|
|
||||||
|
|
||||||
(defn good-api-response [fx [_ response]]
|
(defn good-api-response [fx [_ response]]
|
||||||
(try
|
(try
|
||||||
|
|
@ -123,8 +134,7 @@
|
||||||
{:dispatch [:notification/show :error (api/error-msg e)]})))
|
{:dispatch [:notification/show :error (api/error-msg e)]})))
|
||||||
|
|
||||||
(re-frame/reg-event-fx
|
(re-frame/reg-event-fx
|
||||||
:api/good-response
|
:api/good-response good-api-response)
|
||||||
good-api-response)
|
|
||||||
|
|
||||||
(re-frame/reg-event-fx
|
(re-frame/reg-event-fx
|
||||||
:api/bad-response
|
:api/bad-response
|
||||||
|
|
@ -132,7 +142,9 @@
|
||||||
{:log ["API call gone bad; are CORS headers missing? check for :status 0" event]
|
{:log ["API call gone bad; are CORS headers missing? check for :status 0" event]
|
||||||
:dispatch [:notification/show :error "Communication with server failed. Check browser logs for details."]}))
|
:dispatch [:notification/show :error "Communication with server failed. Check browser logs for details."]}))
|
||||||
|
|
||||||
|
;; ---
|
||||||
;; musique
|
;; musique
|
||||||
|
;; ---
|
||||||
|
|
||||||
; TODO: Make play, next and previous a bit prettier and more DRY
|
; TODO: Make play, next and previous a bit prettier and more DRY
|
||||||
|
|
||||||
|
|
@ -180,7 +192,9 @@
|
||||||
; we receive this from the player once it's playing
|
; we receive this from the player once it's playing
|
||||||
(assoc-in db [:currently-playing :status] status)))
|
(assoc-in db [:currently-playing :status] status)))
|
||||||
|
|
||||||
|
;; ---
|
||||||
;; routing
|
;; routing
|
||||||
|
;; ---
|
||||||
|
|
||||||
(re-frame/reg-event-fx
|
(re-frame/reg-event-fx
|
||||||
:routes/navigation
|
:routes/navigation
|
||||||
|
|
@ -192,13 +206,12 @@
|
||||||
|
|
||||||
(re-frame/reg-event-fx
|
(re-frame/reg-event-fx
|
||||||
:routes/unauthorized
|
:routes/unauthorized
|
||||||
(fn [fx _]
|
(fn [_ _]
|
||||||
;; log out on 403
|
{:dispatch [::logout]}))
|
||||||
{:routes/navigate [routes/default-route]
|
|
||||||
:routes/unset-credentials nil
|
|
||||||
:db db/default-db}))
|
|
||||||
|
|
||||||
|
;; ---
|
||||||
;; user messages
|
;; user messages
|
||||||
|
;; ---
|
||||||
|
|
||||||
(def notification-duration
|
(def notification-duration
|
||||||
{:info 2500
|
{:info 2500
|
||||||
|
|
@ -222,13 +235,11 @@
|
||||||
(assoc :dispatch-later (hide-later level))))))
|
(assoc :dispatch-later (hide-later level))))))
|
||||||
|
|
||||||
(re-frame/reg-event-fx
|
(re-frame/reg-event-fx
|
||||||
:notification/show
|
:notification/show show-notification)
|
||||||
show-notification)
|
|
||||||
|
|
||||||
(defn hide-notification
|
(defn hide-notification
|
||||||
[db [_ notification-id]]
|
[db [_ notification-id]]
|
||||||
(update db :notifications dissoc notification-id))
|
(update db :notifications dissoc notification-id))
|
||||||
|
|
||||||
(re-frame/reg-event-db
|
(re-frame/reg-event-db
|
||||||
:notification/hide
|
:notification/hide hide-notification)
|
||||||
hide-notification)
|
|
||||||
|
|
|
||||||
|
|
@ -42,9 +42,8 @@
|
||||||
[:p.menu-label "User area"]
|
[:p.menu-label "User area"]
|
||||||
[:ul.menu-list
|
[:ul.menu-list
|
||||||
[:li [:a "Settings"]]
|
[:li [:a "Settings"]]
|
||||||
;; FIXME: Create proper logout event
|
|
||||||
[:li [:a
|
[:li [:a
|
||||||
{:on-click #(dispatch [::events/initialize-db]) :href "#"}
|
{:on-click #(dispatch [::events/logout]) :href "#"}
|
||||||
(str "Logout (" (:name user) ")")]]]])
|
(str "Logout (" (:name user) ")")]]]])
|
||||||
|
|
||||||
;; putting everything together
|
;; putting everything together
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,8 @@
|
||||||
(:require [cljs.test :refer [deftest testing is]]
|
(:require [cljs.test :refer [deftest testing is]]
|
||||||
[clojure.string :as str]
|
[clojure.string :as str]
|
||||||
[airsonic-ui.fixtures :refer [responses]]
|
[airsonic-ui.fixtures :refer [responses]]
|
||||||
|
[airsonic-ui.db :as db]
|
||||||
|
[airsonic-ui.routes :as routes]
|
||||||
[airsonic-ui.events :as events]))
|
[airsonic-ui.events :as events]))
|
||||||
|
|
||||||
(enable-console-print!)
|
(enable-console-print!)
|
||||||
|
|
@ -51,6 +53,17 @@
|
||||||
(testing "remembering has no effect"
|
(testing "remembering has no effect"
|
||||||
(is (nil? (events/try-remember-user {} [:_]))))))
|
(is (nil? (events/try-remember-user {} [:_]))))))
|
||||||
|
|
||||||
|
(deftest logout
|
||||||
|
(let [fx (events/logout {} [:_])]
|
||||||
|
(testing "Should clear all stored data"
|
||||||
|
(is (nil? (:store fx))))
|
||||||
|
(testing "Should redirect to the login screen"
|
||||||
|
(is (= [::routes/login] (:routes/navigate fx))))
|
||||||
|
(testing "Should unset authentication in the router"
|
||||||
|
(is (contains? fx :routes/unset-credentials)))
|
||||||
|
(testing "Should reset the app-db"
|
||||||
|
(is (= db/default-db (:db fx))))))
|
||||||
|
|
||||||
(defn- first-notification [fx]
|
(defn- first-notification [fx]
|
||||||
(-> (get-in fx [:db :notifications]) vals first))
|
(-> (get-in fx [:db :notifications]) vals first))
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue