adds session authentication

This commit is contained in:
2019-11-14 12:33:07 +01:00
parent b20a582a42
commit 0d5cc365fc
3 changed files with 99 additions and 35 deletions

View File

@@ -20,7 +20,6 @@
<script> <script>
var app = Elm.Main.init({ var app = Elm.Main.init({
node: document.getElementById('app'), node: document.getElementById('app'),
flags: 0,
}); });
</script> </script>
</body> </body>

View File

@@ -3,23 +3,24 @@ module Main exposing (..)
import Api exposing (Claim, Claims, Item, Loot, Player, Wealth) import Api exposing (Claim, Claims, Item, Loot, Player, Wealth)
import Browser import Browser
import Browser.Navigation as Nav import Browser.Navigation as Nav
import Page.Chest as Chest exposing (Msg)
import Html exposing (..) import Html exposing (..)
import Html.Attributes exposing (..) import Html.Attributes exposing (..)
import Html.Events exposing (..) import Html.Events exposing (..)
import Json.Encode as E import Json.Encode as E
import Page.Chest as Chest exposing (Msg)
import Route exposing (..) import Route exposing (..)
import Session exposing (..)
import Set exposing (Set) import Set exposing (Set)
import Svg.Attributes import Svg.Attributes
import Url import Url
import Utils exposing (..) import Utils exposing (..)
import Session exposing (..)
-- Main -- Main
main : Program (Maybe Int) Model Msg main : Program () Model Msg
main = main =
Browser.application Browser.application
{ init = init { init = init
@@ -34,10 +35,14 @@ main =
-- Model -- Model
type Model type Model
= Chest Chest.Model = Chest Chest.Model
-- | Admin Admin.Model -- | Admin Admin.Model
| About | About
| Loading Nav.Key
-- This is not what we really want. -- This is not what we really want.
-- The flags will be a Maybe Int (id of logged in player), so -- The flags will be a Maybe Int (id of logged in player), so
@@ -50,45 +55,63 @@ type Model
-- - not be logged in -> See About page -- - not be logged in -> See About page
-- - just loggend in -> See Loading page then Chest -- - just loggend in -> See Loading page then Chest
-- - coming back being still logged in -> See Chest (or same as above) -- - coming back being still logged in -> See Chest (or same as above)
init : Maybe Int -> Url.Url -> Nav.Key -> ( Model, Cmd Msg )
init flags _ key =
case flags of
Just id ->
let
session = Session.playerSession key id
(chest, cmd) = Chest.init session
in
(Chest chest, Cmd.map GotChestMsg cmd)
Nothing ->
(About, Cmd.none) init : () -> Url.Url -> Nav.Key -> ( Model, Cmd Msg )
init _ _ key =
( Loading key, Session.init SessionLoaded key )
{-
case flags of
Just id ->
let
session =
Session.playerSession key id
( chest, cmd ) =
Chest.init session
in
( Chest chest, Cmd.map GotChestMsg cmd )
Nothing ->
( About, Cmd.none )
-}
--- ---
-- VIEWS -- VIEWS
--- ---
view : Model -> Browser.Document Msg view : Model -> Browser.Document Msg
view model = view model =
let let
(title, content) = ( title, content ) =
case model of case model of
Chest chest -> Chest chest ->
("Loot-a-lot", List.map (Html.map GotChestMsg) (Chest.view chest)) ( "Loot-a-lot", List.map (Html.map GotChestMsg) (Chest.view chest) )
-- Admin admin ->
-- ("Administration", Admin.view admin) -- Admin admin ->
-- ("Administration", Admin.view admin)
About -> About ->
("A propos", [ p [] [text "A propos"] ]) ( "A propos", [ p [] [ text "A propos" ] ] )
Loading _ ->
( "Chargement...", [ p [] [ text "Chargement" ] ] )
in in
{ title = title { title = title
, body = content } , body = content
}
type Msg type Msg
= UrlChanged Url.Url = UrlChanged Url.Url
| LinkClicked Browser.UrlRequest | LinkClicked Browser.UrlRequest
| SessionLoaded (Maybe Session)
| GotChestMsg Chest.Msg | GotChestMsg Chest.Msg
-- | GotAdminMsg Admin.Msg -- | GotAdminMsg Admin.Msg
@@ -98,15 +121,27 @@ update msg model =
case model of case model of
Chest chest -> Chest chest ->
let let
(newChest, cmd) = ( newChest, cmd ) =
Chest.update chestMsg chest Chest.update chestMsg chest
in in
(Chest newChest, Cmd.map GotChestMsg cmd) ( Chest newChest, Cmd.map GotChestMsg cmd )
_ -> (About, Cmd.none)
_ ->
( About, Cmd.none )
in in
case msg of case msg of
SessionLoaded session ->
case session of
Just logged ->
let
( chest, cmd ) =
Chest.init logged
in
( Chest chest, Cmd.map GotChestMsg cmd )
Nothing ->
( About, Cmd.none )
LinkClicked urlRequest -> LinkClicked urlRequest ->
case model of case model of
Chest chestModel -> Chest chestModel ->
@@ -115,9 +150,10 @@ update msg model =
( model, Nav.pushUrl chestModel.navKey (Url.toString url) ) ( model, Nav.pushUrl chestModel.navKey (Url.toString url) )
Browser.External href -> Browser.External href ->
( model, Cmd.none) ( model, Cmd.none )
_ -> (model, Cmd.none) _ ->
( model, Cmd.none )
UrlChanged url -> UrlChanged url ->
let let
@@ -127,12 +163,15 @@ update msg model =
case route of case route of
Just (Route.Home content) -> Just (Route.Home content) ->
updateChest (Chest.SetContent content) updateChest (Chest.SetContent content)
_ -> _ ->
(About, Cmd.none) ( About, Cmd.none )
GotChestMsg chestMsg -> GotChestMsg chestMsg ->
updateChest chestMsg updateChest chestMsg
-- STATE Utils -- STATE Utils
-- SUBSCRIPTIONS -- SUBSCRIPTIONS
-- --
@@ -141,5 +180,3 @@ update msg model =
subscriptions : Model -> Sub Msg subscriptions : Model -> Sub Msg
subscriptions _ = subscriptions _ =
Sub.none Sub.none

View File

@@ -1,13 +1,41 @@
module Session exposing (Session(..), playerSession) module Session exposing (Session(..), init, playerSession)
import Browser.Navigation as Nav import Browser.Navigation as Nav
import Api import Http
import Json.Decode as D
type Session type Session
= Player Nav.Key Int = Player Nav.Key Int
-- | Admin Nav.Key -- | Admin Nav.Key
playerSession navKey playerId = playerSession navKey playerId =
Player navKey playerId Player navKey playerId
init : (Maybe Session -> msg) -> Nav.Key -> Cmd msg
init toMsg navKey =
let
toSession : Result Http.Error String -> msg
toSession response =
case Debug.log "got session:" response of
Ok value ->
case String.toInt value of
Just id ->
toMsg <| Just (Player navKey id)
Nothing ->
toMsg
Nothing
Err _ ->
toMsg Nothing
in
Http.get
{ url = "http://localhost:8088/session"
, expect = Http.expectJson toSession D.string
}