Add logging in.

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

View File

@ -1,10 +1,13 @@
import Browser exposing (application) import Browser exposing (application)
import Browser.Navigation as Nav import Browser.Navigation as Nav
import Scylla.Sync exposing (..) import Scylla.Sync exposing (..)
import Scylla.Login exposing (..)
import Scylla.Model exposing (..) import Scylla.Model exposing (..)
import Scylla.Http exposing (..) import Scylla.Http exposing (..)
import Scylla.Views exposing (viewFull)
import Url exposing (Url) import Url exposing (Url)
import Html exposing (div, text) import Html exposing (div, text)
import Http
type alias Flags = type alias Flags =
{ token : Maybe String { token : Maybe String
@ -16,6 +19,8 @@ init flags url key =
model = model =
{ key = key { key = key
, token = flags.token , token = flags.token
, loginUsername = ""
, loginPassword = ""
, apiUrl = "https://matrix.org" , apiUrl = "https://matrix.org"
} }
cmd = case flags.token of cmd = case flags.token of
@ -27,11 +32,28 @@ init flags url key =
view : Model -> Browser.Document Msg view : Model -> Browser.Document Msg
view m = view m =
{ title = "Scylla" { title = "Scylla"
, body = [] , body = [ viewFull m ]
} }
update : Msg -> Model -> (Model, Cmd Msg) 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 : Model -> Sub Msg
subscriptions m = Sub.none subscriptions m = Sub.none

View File

@ -6,12 +6,15 @@ import Scylla.Login exposing (loginResponseDecoder, Username, Password)
import Json.Encode exposing (object, string) import Json.Encode exposing (object, string)
import Http exposing (request, jsonBody, expectJson) import Http exposing (request, jsonBody, expectJson)
fullUrl : ApiUrl -> ApiUrl
fullUrl s = s ++ "/_matrix/client/r0"
-- Http Requests -- Http Requests
firstSync : ApiUrl -> ApiToken -> Cmd Msg firstSync : ApiUrl -> ApiToken -> Cmd Msg
firstSync apiUrl token = request firstSync apiUrl token = request
{ method = "GET" { method = "GET"
, headers = authenticatedHeaders token , headers = authenticatedHeaders token
, url = apiUrl ++ "/sync" , url = (fullUrl apiUrl) ++ "/sync"
, body = jsonBody <| object [] , body = jsonBody <| object []
, expect = expectJson ReceiveSyncResponse syncResponseDecoder , expect = expectJson ReceiveSyncResponse syncResponseDecoder
, timeout = Nothing , timeout = Nothing
@ -22,7 +25,7 @@ login : ApiUrl -> Username -> Password -> Cmd Msg
login apiUrl username password = request login apiUrl username password = request
{ method = "POST" { method = "POST"
, headers = basicHeaders , headers = basicHeaders
, url = apiUrl ++ "/login" , url = (fullUrl apiUrl) ++ "/login"
, body = jsonBody <| object , body = jsonBody <| object
[ ("type", string "m.login.password") [ ("type", string "m.login.password")
, ("identifier", object , ("identifier", object

View File

@ -1,7 +1,7 @@
module Scylla.Model exposing (..) module Scylla.Model exposing (..)
import Scylla.Api exposing (..) import Scylla.Api exposing (..)
import Scylla.Sync exposing (SyncResponse) import Scylla.Sync exposing (SyncResponse)
import Scylla.Login exposing (LoginResponse) import Scylla.Login exposing (LoginResponse, Username, Password)
import Browser.Navigation as Nav import Browser.Navigation as Nav
import Browser import Browser
import Http import Http
@ -10,12 +10,18 @@ import Url exposing (Url)
type alias Model = type alias Model =
{ key : Nav.Key { key : Nav.Key
, token : Maybe ApiToken , token : Maybe ApiToken
, loginUsername : Username
, loginPassword : Password
, apiUrl : ApiUrl , apiUrl : ApiUrl
} }
type Msg = type Msg =
TryUrl Browser.UrlRequest ChangeApiUrl ApiUrl -- During login screen: the API URL (homeserver)
| ChangeUrl Url | ChangeLoginUsername Username -- During login screen: the username
| ReceiveSyncResponse (Result Http.Error SyncResponse) | ChangeLoginPassword Password -- During login screen: the password
| ReceiveLoginResponse (Result Http.Error LoginResponse) | 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" ]
]