summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLLLL Colonq <llll@colonq>2024-11-18 05:34:38 -0500
committerLLLL Colonq <llll@colonq>2024-11-18 05:34:38 -0500
commitd86e0ab8de6cd558568301c470bd4c1e163177ba (patch)
tree235c8efc82e278662e40b8239ba3feaec75ebcda /src
parent07adfbdcb959e2a789551e516bd389ce0002c6f8 (diff)
Add logout
Diffstat (limited to 'src')
-rw-r--r--src/Auth.js14
-rw-r--r--src/Auth.purs8
-rw-r--r--src/Main.purs42
-rw-r--r--src/UI.js3
-rw-r--r--src/UI.purs4
5 files changed, 55 insertions, 16 deletions
diff --git a/src/Auth.js b/src/Auth.js
index 81ddad9..3c50658 100644
--- a/src/Auth.js
+++ b/src/Auth.js
@@ -44,3 +44,17 @@ export const _getToken = (Just) => (Nothing) => (pair) => () => {
if (token && authnonce) return Just(pair(token)(authnonce));
return Nothing;
};
+
+export const _getSessionCookie = (Just) => (Nothing) => () => {
+ let cookie = null;
+ for (let c of document.cookie.split("; ")) {
+ const [k, v] = c.split("=");
+ if (k === "authelia_session") cookie = v;
+ }
+ if (cookie) return Just(cookie);
+ return Nothing;
+};
+
+export const _clearSessionCookie = () => {
+ document.cookie = "authelia_session=; expires=Thu, 01 Jan 1970 00:00:00 GMT; SameSite=None; Secure";
+};
diff --git a/src/Auth.purs b/src/Auth.purs
index 2a53629..2a87b6a 100644
--- a/src/Auth.purs
+++ b/src/Auth.purs
@@ -27,3 +27,11 @@ authHeader (Tuple t n) =
, n
, "\""
]
+
+foreign import _getSessionCookie :: forall a. (a -> Maybe a) -> Maybe a -> Effect (Maybe String)
+getSessionCookie :: forall m. MonadEffect m => m (Maybe String)
+getSessionCookie = liftEffect $ _getSessionCookie Just Nothing
+
+foreign import _clearSessionCookie :: Effect Unit
+clearSessionCookie :: forall m. MonadEffect m => m Unit
+clearSessionCookie = liftEffect _clearSessionCookie
diff --git a/src/Main.purs b/src/Main.purs
index 02d1b08..912a977 100644
--- a/src/Main.purs
+++ b/src/Main.purs
@@ -3,7 +3,7 @@ module Main where
import Prelude
import Audio as Audio
-import Auth (AuthInfo, authHeader, getToken, startTwitchAuth)
+import Auth (AuthInfo, authHeader, getToken, startTwitchAuth, getSessionCookie, clearSessionCookie)
import Config as Config
import Data.String as String
import Data.Array (head)
@@ -232,21 +232,31 @@ mainMenu = launchAff_ do
mainAuth :: Effect Unit
mainAuth = launchAff_ do
liftEffect $ log "hello from auth"
- form <- byId "lcolonq-auth-form"
- listen form "submit" \ev -> launchAff_ do
- liftEffect $ Ev.preventDefault ev
- usernameInp <- byId "lcolonq-auth-username"
- passwordInp <- byId "lcolonq-auth-password"
- username <- getValue usernameInp
- password <- getValue passwordInp
- { text: resp } <- fetch ("/api/firstfactor")
- { method: POST
- , headers: { "Content-Type": "application/json" }
- , body: UI.toJSON { username, password }
- }
- res <- resp
- liftEffect $ log res
- pure unit
+ getSessionCookie >>= case _ of
+ Nothing -> do
+ container <- byId "lcolonq-auth-login"
+ removeClass "lcolonq-invisible" container
+ form <- byId "lcolonq-auth-form"
+ listen form "submit" \ev -> launchAff_ do
+ liftEffect $ Ev.preventDefault ev
+ usernameInp <- byId "lcolonq-auth-username"
+ passwordInp <- byId "lcolonq-auth-password"
+ username <- getValue usernameInp
+ password <- getValue passwordInp
+ { text: resp } <- fetch ("/api/firstfactor")
+ { method: POST
+ , headers: { "Content-Type": "application/json" }
+ , body: UI.toJSON { username, password }
+ }
+ _ <- resp
+ UI.reload
+ Just _ -> do
+ container <- byId "lcolonq-auth-logout"
+ removeClass "lcolonq-invisible" container
+ logout <- byId "lcolonq-auth-logout-link"
+ listen logout "click" \_ev -> do
+ clearSessionCookie
+ UI.reload
main :: Effect Unit
main = case Config.mode of
diff --git a/src/UI.js b/src/UI.js
index 5cdc5d0..5003e21 100644
--- a/src/UI.js
+++ b/src/UI.js
@@ -1,3 +1,6 @@
export const _cheatLog = (a) => () => console.log(a);
export const _setInterval = (delay) => (f) => () => setInterval(f, delay);
export const _toJSON = (x) => JSON.stringify(x);
+export const _reload = () => {
+ window.location.reload();
+};
diff --git a/src/UI.purs b/src/UI.purs
index 17d1ca1..d9a01bc 100644
--- a/src/UI.purs
+++ b/src/UI.purs
@@ -15,3 +15,7 @@ setInterval d f = liftEffect $ _setInterval d f
foreign import _toJSON :: forall a. a -> String
toJSON :: forall a. a -> String
toJSON = _toJSON
+
+foreign import _reload :: Effect Unit
+reload :: forall m. MonadEffect m => m Unit
+reload = liftEffect _reload