From 6f1e3da27b82f4fe6e48b74a642343d2b8c93781 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Mon, 17 Dec 2018 02:34:46 -0800 Subject: [PATCH] Add storing login token (but not retrieving it) --- src/Main.elm | 3 +++ src/Scylla/Storage.elm | 6 ++++++ static/js/storage.js | 10 ++++++++++ 3 files changed, 19 insertions(+) create mode 100644 src/Scylla/Storage.elm create mode 100644 static/js/storage.js diff --git a/src/Main.elm b/src/Main.elm index f54dfb1..64a2f34 100644 --- a/src/Main.elm +++ b/src/Main.elm @@ -9,9 +9,11 @@ import Scylla.Views exposing (viewFull) import Scylla.Route exposing (Route(..)) import Scylla.UserData exposing (..) import Scylla.Notification exposing (..) +import Scylla.Storage exposing (..) import Url exposing (Url) import Url.Parser exposing (parse) import Url.Builder +import Json.Encode import Html exposing (div, text) import Http import Dict @@ -122,6 +124,7 @@ updateLoginResponse model r = case r of Ok lr -> ( { model | token = Just lr.accessToken, loginUsername = lr.userId } , Cmd.batch [ firstSync model.apiUrl lr.accessToken , Nav.pushUrl model.key <| Url.Builder.absolute [] [] + , setStoreValuePort ("scylla.loginInfo", Json.Encode.string (lr.accessToken ++ "\n" ++ model.apiUrl)) ] ) Err e -> (model, Cmd.none) diff --git a/src/Scylla/Storage.elm b/src/Scylla/Storage.elm new file mode 100644 index 0000000..945bd22 --- /dev/null +++ b/src/Scylla/Storage.elm @@ -0,0 +1,6 @@ +port module Scylla.Storage exposing (..) +import Json.Encode + +port setStoreValuePort : (String, Json.Encode.Value) -> Cmd msg +port getStoreValuePort : (String) -> Cmd msg +port receiveStoreValuePort : (Json.Encode.Value -> msg) -> Sub msg diff --git a/static/js/storage.js b/static/js/storage.js new file mode 100644 index 0000000..ac3110f --- /dev/null +++ b/static/js/storage.js @@ -0,0 +1,10 @@ +function setupStorage(app) { + app.ports.setStoreValuePort.subscribe(function(data) { + key = data[0]; + value = data[1]; + localStorage.setItem(key, value); + }); + app.ports.getStoreValuePort.subscribe(function(data) { + app.ports.receiveStoreValuePort.send(localStorage.getItem(data)); + }); +}