From 128430b38f2bd3ab813b293f341c5f66fe8a109c Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Mon, 17 Dec 2018 15:34:21 -0800 Subject: [PATCH] Add encoding and decoding of login datas a function. --- src/Main.elm | 2 +- src/Scylla/Login.elm | 21 ++++++++++++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/Main.elm b/src/Main.elm index eb07b07..ce5f018 100644 --- a/src/Main.elm +++ b/src/Main.elm @@ -125,7 +125,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", Json.Encode.string (lr.accessToken ++ "\n" ++ model.apiUrl)) + , setStoreValuePort ("scylla.loginInfo", encodeLoginInfo (lr.accessToken, model.apiUrl, lr.userId)) ] ) Err e -> (model, Cmd.none) diff --git a/src/Scylla/Login.elm b/src/Scylla/Login.elm index 6758950..784d190 100644 --- a/src/Scylla/Login.elm +++ b/src/Scylla/Login.elm @@ -1,11 +1,30 @@ module Scylla.Login exposing (..) -import Scylla.Api exposing (ApiToken) +import Scylla.Api exposing (ApiUrl, ApiToken) import Json.Decode as Decode exposing (Decoder, int, string, float, list, value, dict, bool) import Json.Decode.Pipeline exposing (required, optional) +import Json.Encode as Encode type alias Username = String type alias Password = String +type alias LoginInfo = (ApiToken, ApiUrl, Username) + +encodeLoginInfo : LoginInfo -> Decode.Value +encodeLoginInfo (t,a,u) = Encode.string (t ++ "," ++ a ++ "," ++ u) + +parseLoginInfoString : String -> Decoder LoginInfo +parseLoginInfoString s = case String.indexes "," s of + [ fst, snd ] -> Decode.succeed + ( (String.slice 0 fst s) + , (String.slice (fst + 1) snd s) + , (String.dropLeft snd s) + ) + _ -> Decode.fail "Incorrectly formatted Login Info string" + +loginInfoDecoder : Decoder LoginInfo +loginInfoDecoder = string + |> Decode.andThen parseLoginInfoString + type alias LoginResponse = { userId : String , accessToken : ApiToken