Add logging in.

This commit is contained in:
2018-12-08 15:06:14 -08:00
parent 3656b0f3f0
commit e06c4a8772
4 changed files with 61 additions and 9 deletions

View File

@@ -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

View File

@@ -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

21
src/Scylla/Views.elm Normal file
View File

@@ -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" ]
]