mirror of
https://github.com/heyarne/airsonic-ui.git
synced 2026-05-06 18:33:38 +02:00
Implement authentication
This commit is contained in:
parent
9f5ff05101
commit
d782b49b94
7 changed files with 70 additions and 8 deletions
|
|
@ -5,6 +5,7 @@
|
|||
:dependencies
|
||||
[[reagent "0.7.0"]
|
||||
[re-frame "0.10.5"]
|
||||
[day8.re-frame/http-fx "0.1.6"]
|
||||
;; debugging
|
||||
[day8.re-frame/re-frame-10x "0.3.2-react16"]
|
||||
;; for CIDER
|
||||
|
|
|
|||
|
|
@ -1,4 +1,6 @@
|
|||
(ns airsonic-ui.config)
|
||||
|
||||
(def server "https://londe.arnes.space")
|
||||
|
||||
(def debug?
|
||||
^boolean goog.DEBUG)
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
(ns airsonic-ui.core
|
||||
(:require [reagent.core :as reagent]
|
||||
[re-frame.core :as re-frame]
|
||||
[day8.re-frame.http-fx]
|
||||
[airsonic-ui.events :as events]
|
||||
[airsonic-ui.views :as views]
|
||||
[airsonic-ui.config :as config]))
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
(ns airsonic-ui.db)
|
||||
|
||||
(def default-db
|
||||
{:name "re-frame"})
|
||||
{:active-requests 0})
|
||||
|
|
|
|||
|
|
@ -1,6 +1,44 @@
|
|||
(ns airsonic-ui.events
|
||||
(:require [re-frame.core :as re-frame]
|
||||
[airsonic-ui.db :as db]))
|
||||
[ajax.core :as ajax]
|
||||
[airsonic-ui.config :as config]
|
||||
[airsonic-ui.db :as db]
|
||||
[clojure.string :as string]))
|
||||
|
||||
(defn ^:private uri-escape [s]
|
||||
(js/encodeURIComponent s))
|
||||
|
||||
(defn api-url [endpoint params]
|
||||
(let [query (->> (assoc params
|
||||
:f "json"
|
||||
:c "airsonic-ui-cljs"
|
||||
:v "1.15.0")
|
||||
(map (fn [[k v]]
|
||||
(str (uri-escape (name k)) "=" (uri-escape v))))
|
||||
(string/join "&"))]
|
||||
(str config/server "/rest/" endpoint "?" query)))
|
||||
|
||||
(re-frame/reg-event-fx
|
||||
::authenticate
|
||||
(fn [{:keys [db]} [_ {:keys [user pass]}]]
|
||||
{:db (update db :active-requests inc)
|
||||
:http-xhrio {:method :get
|
||||
:uri (api-url "ping" {:u user :p pass})
|
||||
:response-format (ajax/text-response-format)
|
||||
:on-success [::auth-successful user pass]
|
||||
:on-failure [::auth-gone-bad]}}))
|
||||
|
||||
(re-frame/reg-event-db
|
||||
::auth-successful
|
||||
(fn [db [_ user pass]]
|
||||
(-> (update db :active-requests dec)
|
||||
(assoc :login {:u user
|
||||
:p pass}))))
|
||||
|
||||
(re-frame/reg-event-db
|
||||
::auth-gone-bad
|
||||
(fn [db event]
|
||||
(println "auth gone bad" event)))
|
||||
|
||||
(re-frame/reg-event-db
|
||||
::initialize-db
|
||||
|
|
|
|||
|
|
@ -2,6 +2,6 @@
|
|||
(:require [re-frame.core :as re-frame]))
|
||||
|
||||
(re-frame/reg-sub
|
||||
::name
|
||||
::login
|
||||
(fn [db]
|
||||
(:name db)))
|
||||
(:login db)))
|
||||
|
|
|
|||
|
|
@ -1,8 +1,28 @@
|
|||
(ns airsonic-ui.views
|
||||
(:require [re-frame.core :as re-frame]
|
||||
[airsonic-ui.subs :as subs]
|
||||
))
|
||||
[airsonic-ui.config :as config]
|
||||
[airsonic-ui.subs :as subs]))
|
||||
|
||||
(defn login-form []
|
||||
[:form {:method "get"
|
||||
:action config/server
|
||||
:on-click #(js/alert "bang bang! TODO: implement login via form")}
|
||||
[:div
|
||||
[:span "User"]
|
||||
[:input {:type "text" :name "user"}]]
|
||||
[:div
|
||||
[:span "Password"]
|
||||
[:input {:type "password" :name "pass"}]]
|
||||
[:div
|
||||
[:input {:type "submit" :value "submit"}]]])
|
||||
|
||||
(defn app [user]
|
||||
[:div
|
||||
[:h2 (str "Currently logged in as " user)]])
|
||||
|
||||
(defn main-panel []
|
||||
(let [name (re-frame/subscribe [::subs/name])]
|
||||
[:div "Hello from " @name]))
|
||||
[:div
|
||||
[:h1 "Airsonic"]
|
||||
(if-let [login @(re-frame/subscribe [::subs/login])]
|
||||
[app (:u login)]
|
||||
[login-form])])
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue