Add code to retrieve login token after login.

This commit is contained in:
Danila Fedorin 2018-12-17 16:18:47 -08:00
parent 128430b38f
commit cf2ada4329
5 changed files with 34 additions and 16 deletions

View File

@ -47,7 +47,7 @@ init flags url key =
} }
cmd = case flags.token of cmd = case flags.token of
Just _ -> Cmd.none Just _ -> Cmd.none
Nothing -> Nav.pushUrl key <| Url.Builder.absolute [ "login" ] [] Nothing -> getStoreValuePort "scylla.loginInfo"
in in
(model, cmd) (model, cmd)
@ -76,6 +76,17 @@ update msg model = case msg of
SendRoomText r -> updateSendRoomText model r SendRoomText r -> updateSendRoomText model r
SendRoomTextResponse r -> (model, Cmd.none) SendRoomTextResponse r -> (model, Cmd.none)
ReceiveCompletedReadMarker 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 : Model -> Route -> (Model, Cmd Msg)
updateChangeRoute m r = 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 Ok lr -> ( { model | token = Just lr.accessToken, loginUsername = lr.userId, apiUrl = a }, Cmd.batch
[ firstSync model.apiUrl lr.accessToken [ firstSync model.apiUrl lr.accessToken
, Nav.pushUrl model.key <| Url.Builder.absolute [] [] , 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) Err e -> (model, Cmd.none)
@ -182,7 +193,10 @@ updateSyncResponse model r notify =
_ -> (model, syncCmd) _ -> (model, syncCmd)
subscriptions : Model -> Sub Msg subscriptions : Model -> Sub Msg
subscriptions m = onNotificationClickPort OpenRoom subscriptions m = Sub.batch
[ onNotificationClickPort OpenRoom
, receiveStoreValuePort ReceiveStoreData
]
onUrlRequest : Browser.UrlRequest -> Msg onUrlRequest : Browser.UrlRequest -> Msg
onUrlRequest = TryUrl onUrlRequest = TryUrl

View File

@ -9,21 +9,17 @@ type alias Password = String
type alias LoginInfo = (ApiToken, ApiUrl, Username) type alias LoginInfo = (ApiToken, ApiUrl, Username)
encodeLoginInfo : LoginInfo -> Decode.Value encodeLoginInfo : LoginInfo -> String
encodeLoginInfo (t,a,u) = Encode.string (t ++ "," ++ a ++ "," ++ u) encodeLoginInfo (t,a,u) = t ++ "," ++ a ++ "," ++ u
parseLoginInfoString : String -> Decoder LoginInfo decodeLoginInfo : String -> Maybe LoginInfo
parseLoginInfoString s = case String.indexes "," s of decodeLoginInfo s = case String.indexes "," s of
[ fst, snd ] -> Decode.succeed [ fst, snd ] -> Just
( (String.slice 0 fst s) ( (String.slice 0 fst s)
, (String.slice (fst + 1) snd s) , (String.slice (fst + 1) snd s)
, (String.dropLeft snd s) , (String.dropLeft (snd + 1) s)
) )
_ -> Decode.fail "Incorrectly formatted Login Info string" _ -> Nothing
loginInfoDecoder : Decoder LoginInfo
loginInfoDecoder = string
|> Decode.andThen parseLoginInfoString
type alias LoginResponse = type alias LoginResponse =
{ userId : String { userId : String

View File

@ -4,10 +4,12 @@ import Scylla.Sync exposing (SyncResponse, JoinedRoom, senderName)
import Scylla.Login exposing (LoginResponse, Username, Password) import Scylla.Login exposing (LoginResponse, Username, Password)
import Scylla.UserData exposing (UserData) import Scylla.UserData exposing (UserData)
import Scylla.Route exposing (Route(..), RoomId) import Scylla.Route exposing (Route(..), RoomId)
import Scylla.Storage exposing (..)
import Browser.Navigation as Nav import Browser.Navigation as Nav
import Browser.Dom exposing (Viewport) import Browser.Dom exposing (Viewport)
import Url.Builder import Url.Builder
import Dict exposing (Dict) import Dict exposing (Dict)
import Json.Decode
import Browser import Browser
import Http import Http
import Url exposing (Url) import Url exposing (Url)
@ -44,6 +46,7 @@ type Msg =
| ReceiveLoginResponse ApiUrl (Result Http.Error LoginResponse) -- HTTP, Login has finished | ReceiveLoginResponse ApiUrl (Result Http.Error LoginResponse) -- HTTP, Login has finished
| ReceiveUserData Username (Result Http.Error UserData) | ReceiveUserData Username (Result Http.Error UserData)
| ReceiveCompletedReadMarker (Result Http.Error ()) | ReceiveCompletedReadMarker (Result Http.Error ())
| ReceiveStoreData StoreData
displayName : Model -> Username -> String displayName : Model -> Username -> String
displayName m s = Maybe.withDefault (senderName s) <| Maybe.andThen .displayName <| Dict.get s m.userData displayName m s = Maybe.withDefault (senderName s) <| Maybe.andThen .displayName <| Dict.get s m.userData

View File

@ -1,6 +1,11 @@
port module Scylla.Storage exposing (..) port module Scylla.Storage exposing (..)
import Json.Encode import Json.Encode
type alias StoreData =
{ key : String
, value: String
}
port setStoreValuePort : (String, Json.Encode.Value) -> Cmd msg port setStoreValuePort : (String, Json.Encode.Value) -> Cmd msg
port getStoreValuePort : (String) -> Cmd msg port getStoreValuePort : (String) -> Cmd msg
port receiveStoreValuePort : (Json.Encode.Value -> msg) -> Sub msg port receiveStoreValuePort : (StoreData -> msg) -> Sub msg

View File

@ -5,6 +5,6 @@ function setupStorage(app) {
localStorage.setItem(key, value); localStorage.setItem(key, value);
}); });
app.ports.getStoreValuePort.subscribe(function(data) { app.ports.getStoreValuePort.subscribe(function(data) {
app.ports.receiveStoreValuePort.send(localStorage.getItem(data)); app.ports.receiveStoreValuePort.send({ "key" : data, "value" : localStorage.getItem(data) });
}); });
} }