From e06c4a8772102fcf4135978e991b91b93d11e5f6 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Sat, 8 Dec 2018 15:06:14 -0800 Subject: [PATCH] Add logging in. --- src/Main.elm | 26 ++++++++++++++++++++++++-- src/Scylla/Http.elm | 7 +++++-- src/Scylla/Model.elm | 16 +++++++++++----- src/Scylla/Views.elm | 21 +++++++++++++++++++++ 4 files changed, 61 insertions(+), 9 deletions(-) create mode 100644 src/Scylla/Views.elm diff --git a/src/Main.elm b/src/Main.elm index 82a1ce5..d549abd 100644 --- a/src/Main.elm +++ b/src/Main.elm @@ -1,10 +1,13 @@ import Browser exposing (application) import Browser.Navigation as Nav import Scylla.Sync exposing (..) +import Scylla.Login exposing (..) import Scylla.Model exposing (..) import Scylla.Http exposing (..) +import Scylla.Views exposing (viewFull) import Url exposing (Url) import Html exposing (div, text) +import Http type alias Flags = { token : Maybe String @@ -16,6 +19,8 @@ init flags url key = model = { key = key , token = flags.token + , loginUsername = "" + , loginPassword = "" , apiUrl = "https://matrix.org" } cmd = case flags.token of @@ -27,11 +32,28 @@ init flags url key = view : Model -> Browser.Document Msg view m = { title = "Scylla" - , body = [] + , body = [ viewFull m ] } update : Msg -> Model -> (Model, Cmd Msg) -update msg model = (model, Cmd.none) +update msg model = case msg of + ChangeApiUrl u -> ({ model | apiUrl = u }, Cmd.none) + ChangeLoginUsername u -> ({ model | loginUsername = u }, Cmd.none) + ChangeLoginPassword p -> ({ model | loginPassword = p }, Cmd.none) + AttemptLogin -> (model, Scylla.Http.login model.apiUrl model.loginUsername model.loginPassword) -- TODO + ReceiveLoginResponse r -> updateLoginResponse model r + ReceiveSyncResponse r -> updateSyncResponse model r + _ -> (model, Cmd.none) + +updateLoginResponse : Model -> Result Http.Error LoginResponse -> (Model, Cmd Msg) +updateLoginResponse model r = case r of + Ok lr -> ( { model | token = Just lr.accessToken } , firstSync model.apiUrl lr.accessToken ) + Err e -> (model, Cmd.none) + +updateSyncResponse : Model -> Result Http.Error SyncResponse -> (Model, Cmd Msg) +updateSyncResponse model r = case r of + Ok sr -> (model, Cmd.none) + Err e -> (model, Cmd.none) subscriptions : Model -> Sub Msg subscriptions m = Sub.none diff --git a/src/Scylla/Http.elm b/src/Scylla/Http.elm index 613978e..113e335 100644 --- a/src/Scylla/Http.elm +++ b/src/Scylla/Http.elm @@ -6,12 +6,15 @@ import Scylla.Login exposing (loginResponseDecoder, Username, Password) import Json.Encode exposing (object, string) import Http exposing (request, jsonBody, expectJson) +fullUrl : ApiUrl -> ApiUrl +fullUrl s = s ++ "/_matrix/client/r0" + -- Http Requests firstSync : ApiUrl -> ApiToken -> Cmd Msg firstSync apiUrl token = request { method = "GET" , headers = authenticatedHeaders token - , url = apiUrl ++ "/sync" + , url = (fullUrl apiUrl) ++ "/sync" , body = jsonBody <| object [] , expect = expectJson ReceiveSyncResponse syncResponseDecoder , timeout = Nothing @@ -22,7 +25,7 @@ login : ApiUrl -> Username -> Password -> Cmd Msg login apiUrl username password = request { method = "POST" , headers = basicHeaders - , url = apiUrl ++ "/login" + , url = (fullUrl apiUrl) ++ "/login" , body = jsonBody <| object [ ("type", string "m.login.password") , ("identifier", object diff --git a/src/Scylla/Model.elm b/src/Scylla/Model.elm index ecac1dc..b7fa78b 100644 --- a/src/Scylla/Model.elm +++ b/src/Scylla/Model.elm @@ -1,7 +1,7 @@ module Scylla.Model exposing (..) import Scylla.Api exposing (..) import Scylla.Sync exposing (SyncResponse) -import Scylla.Login exposing (LoginResponse) +import Scylla.Login exposing (LoginResponse, Username, Password) import Browser.Navigation as Nav import Browser import Http @@ -10,12 +10,18 @@ import Url exposing (Url) type alias Model = { key : Nav.Key , token : Maybe ApiToken + , loginUsername : Username + , loginPassword : Password , apiUrl : ApiUrl } type Msg = - TryUrl Browser.UrlRequest - | ChangeUrl Url - | ReceiveSyncResponse (Result Http.Error SyncResponse) - | ReceiveLoginResponse (Result Http.Error LoginResponse) + ChangeApiUrl ApiUrl -- During login screen: the API URL (homeserver) + | ChangeLoginUsername Username -- During login screen: the username + | ChangeLoginPassword Password -- During login screen: the password + | AttemptLogin -- During login screen, login button presed + | TryUrl Browser.UrlRequest -- User attempts to change URL + | ChangeUrl Url -- URL changes + | ReceiveSyncResponse (Result Http.Error SyncResponse) -- HTTP, Sync has finished + | ReceiveLoginResponse (Result Http.Error LoginResponse) -- HTTP, Login has finished diff --git a/src/Scylla/Views.elm b/src/Scylla/Views.elm new file mode 100644 index 0000000..0870527 --- /dev/null +++ b/src/Scylla/Views.elm @@ -0,0 +1,21 @@ +module Scylla.Views exposing (..) +import Scylla.Model exposing (..) +import Html exposing (Html, div, input, text, button) +import Html.Attributes exposing (type_, value) +import Html.Events exposing (onInput, onClick) + +viewFull : Model -> Html Msg +viewFull model = case model.token of + Just _ -> normalView model + Nothing -> loginView model + +normalView : Model -> Html Msg +normalView m = div [] [ text "You are logged in!" ] + +loginView : Model -> Html Msg +loginView m = div [] + [ input [ type_ "text", value m.loginUsername, onInput ChangeLoginUsername] [] + , input [ type_ "password", value m.loginPassword, onInput ChangeLoginPassword ] [] + , input [ type_ "text", value m.apiUrl, onInput ChangeApiUrl ] [] + , button [ onClick AttemptLogin ] [ text "Log In" ] + ]