From cf2ada432940b85d47bc8b07fe456d4ee5113993 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Mon, 17 Dec 2018 16:18:47 -0800 Subject: [PATCH] Add code to retrieve login token after login. --- src/Main.elm | 20 +++++++++++++++++--- src/Scylla/Login.elm | 18 +++++++----------- src/Scylla/Model.elm | 3 +++ src/Scylla/Storage.elm | 7 ++++++- static/js/storage.js | 2 +- 5 files changed, 34 insertions(+), 16 deletions(-) diff --git a/src/Main.elm b/src/Main.elm index ce5f018..0a0eed1 100644 --- a/src/Main.elm +++ b/src/Main.elm @@ -47,7 +47,7 @@ init flags url key = } cmd = case flags.token of Just _ -> Cmd.none - Nothing -> Nav.pushUrl key <| Url.Builder.absolute [ "login" ] [] + Nothing -> getStoreValuePort "scylla.loginInfo" in (model, cmd) @@ -76,6 +76,17 @@ update msg model = case msg of SendRoomText r -> updateSendRoomText model r SendRoomTextResponse r -> (model, Cmd.none) ReceiveCompletedReadMarker r -> (model, Cmd.none) + ReceiveStoreData d -> updateStoreData model d + +updateStoreData : Model -> StoreData -> (Model, Cmd Msg) +updateStoreData m d = case d.key of + "scylla.loginInfo" -> updateLoginInfo m d.value + _ -> (m, Cmd.none) + +updateLoginInfo : Model -> String -> (Model, Cmd Msg) +updateLoginInfo m s = case decodeLoginInfo s of + Just (t,a,u) -> ({ m | token = Just t, apiUrl = a, loginUsername = u}, firstSync a t) + Nothing -> (m, Nav.pushUrl m.key <| Url.Builder.absolute [ "login" ] []) updateChangeRoute : Model -> Route -> (Model, Cmd Msg) updateChangeRoute m r = @@ -125,7 +136,7 @@ updateLoginResponse model a r = case r of Ok lr -> ( { model | token = Just lr.accessToken, loginUsername = lr.userId, apiUrl = a }, Cmd.batch [ firstSync model.apiUrl lr.accessToken , Nav.pushUrl model.key <| Url.Builder.absolute [] [] - , setStoreValuePort ("scylla.loginInfo", encodeLoginInfo (lr.accessToken, model.apiUrl, lr.userId)) + , setStoreValuePort ("scylla.loginInfo", Json.Encode.string <| encodeLoginInfo (lr.accessToken, model.apiUrl, lr.userId)) ] ) Err e -> (model, Cmd.none) @@ -182,7 +193,10 @@ updateSyncResponse model r notify = _ -> (model, syncCmd) subscriptions : Model -> Sub Msg -subscriptions m = onNotificationClickPort OpenRoom +subscriptions m = Sub.batch + [ onNotificationClickPort OpenRoom + , receiveStoreValuePort ReceiveStoreData + ] onUrlRequest : Browser.UrlRequest -> Msg onUrlRequest = TryUrl diff --git a/src/Scylla/Login.elm b/src/Scylla/Login.elm index 784d190..0fba6fd 100644 --- a/src/Scylla/Login.elm +++ b/src/Scylla/Login.elm @@ -9,21 +9,17 @@ type alias Password = String type alias LoginInfo = (ApiToken, ApiUrl, Username) -encodeLoginInfo : LoginInfo -> Decode.Value -encodeLoginInfo (t,a,u) = Encode.string (t ++ "," ++ a ++ "," ++ u) +encodeLoginInfo : LoginInfo -> String +encodeLoginInfo (t,a,u) = t ++ "," ++ a ++ "," ++ u -parseLoginInfoString : String -> Decoder LoginInfo -parseLoginInfoString s = case String.indexes "," s of - [ fst, snd ] -> Decode.succeed +decodeLoginInfo : String -> Maybe LoginInfo +decodeLoginInfo s = case String.indexes "," s of + [ fst, snd ] -> Just ( (String.slice 0 fst s) , (String.slice (fst + 1) snd s) - , (String.dropLeft snd s) + , (String.dropLeft (snd + 1) s) ) - _ -> Decode.fail "Incorrectly formatted Login Info string" - -loginInfoDecoder : Decoder LoginInfo -loginInfoDecoder = string - |> Decode.andThen parseLoginInfoString + _ -> Nothing type alias LoginResponse = { userId : String diff --git a/src/Scylla/Model.elm b/src/Scylla/Model.elm index db33cc3..7f2f951 100644 --- a/src/Scylla/Model.elm +++ b/src/Scylla/Model.elm @@ -4,10 +4,12 @@ import Scylla.Sync exposing (SyncResponse, JoinedRoom, senderName) import Scylla.Login exposing (LoginResponse, Username, Password) import Scylla.UserData exposing (UserData) import Scylla.Route exposing (Route(..), RoomId) +import Scylla.Storage exposing (..) import Browser.Navigation as Nav import Browser.Dom exposing (Viewport) import Url.Builder import Dict exposing (Dict) +import Json.Decode import Browser import Http import Url exposing (Url) @@ -44,6 +46,7 @@ type Msg = | ReceiveLoginResponse ApiUrl (Result Http.Error LoginResponse) -- HTTP, Login has finished | ReceiveUserData Username (Result Http.Error UserData) | ReceiveCompletedReadMarker (Result Http.Error ()) + | ReceiveStoreData StoreData displayName : Model -> Username -> String displayName m s = Maybe.withDefault (senderName s) <| Maybe.andThen .displayName <| Dict.get s m.userData diff --git a/src/Scylla/Storage.elm b/src/Scylla/Storage.elm index 945bd22..0479eee 100644 --- a/src/Scylla/Storage.elm +++ b/src/Scylla/Storage.elm @@ -1,6 +1,11 @@ port module Scylla.Storage exposing (..) import Json.Encode +type alias StoreData = + { key : String + , value: String + } + port setStoreValuePort : (String, Json.Encode.Value) -> Cmd msg port getStoreValuePort : (String) -> Cmd msg -port receiveStoreValuePort : (Json.Encode.Value -> msg) -> Sub msg +port receiveStoreValuePort : (StoreData -> msg) -> Sub msg diff --git a/static/js/storage.js b/static/js/storage.js index ac3110f..c3f971c 100644 --- a/static/js/storage.js +++ b/static/js/storage.js @@ -5,6 +5,6 @@ function setupStorage(app) { localStorage.setItem(key, value); }); app.ports.getStoreValuePort.subscribe(function(data) { - app.ports.receiveStoreValuePort.send(localStorage.getItem(data)); + app.ports.receiveStoreValuePort.send({ "key" : data, "value" : localStorage.getItem(data) }); }); }