adds session authentication
This commit is contained in:
101
src/Main.elm
101
src/Main.elm
@@ -3,23 +3,24 @@ module Main exposing (..)
|
||||
import Api exposing (Claim, Claims, Item, Loot, Player, Wealth)
|
||||
import Browser
|
||||
import Browser.Navigation as Nav
|
||||
import Page.Chest as Chest exposing (Msg)
|
||||
import Html exposing (..)
|
||||
import Html.Attributes exposing (..)
|
||||
import Html.Events exposing (..)
|
||||
import Json.Encode as E
|
||||
import Page.Chest as Chest exposing (Msg)
|
||||
import Route exposing (..)
|
||||
import Session exposing (..)
|
||||
import Set exposing (Set)
|
||||
import Svg.Attributes
|
||||
import Url
|
||||
import Utils exposing (..)
|
||||
import Session exposing (..)
|
||||
|
||||
|
||||
|
||||
-- Main
|
||||
|
||||
|
||||
main : Program (Maybe Int) Model Msg
|
||||
main : Program () Model Msg
|
||||
main =
|
||||
Browser.application
|
||||
{ init = init
|
||||
@@ -34,10 +35,14 @@ main =
|
||||
|
||||
-- Model
|
||||
|
||||
|
||||
type Model
|
||||
= Chest Chest.Model
|
||||
-- | Admin Admin.Model
|
||||
-- | Admin Admin.Model
|
||||
| About
|
||||
| Loading Nav.Key
|
||||
|
||||
|
||||
|
||||
-- This is not what we really want.
|
||||
-- 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
|
||||
-- - just loggend in -> See Loading page then Chest
|
||||
-- - 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
|
||||
---
|
||||
|
||||
|
||||
view : Model -> Browser.Document Msg
|
||||
view model =
|
||||
let
|
||||
(title, content) =
|
||||
( title, content ) =
|
||||
case model of
|
||||
Chest chest ->
|
||||
("Loot-a-lot", List.map (Html.map GotChestMsg) (Chest.view chest))
|
||||
-- Admin admin ->
|
||||
-- ("Administration", Admin.view admin)
|
||||
( "Loot-a-lot", List.map (Html.map GotChestMsg) (Chest.view chest) )
|
||||
|
||||
-- Admin admin ->
|
||||
-- ("Administration", Admin.view admin)
|
||||
About ->
|
||||
("A propos", [ p [] [text "A propos"] ])
|
||||
( "A propos", [ p [] [ text "A propos" ] ] )
|
||||
|
||||
Loading _ ->
|
||||
( "Chargement...", [ p [] [ text "Chargement" ] ] )
|
||||
in
|
||||
{ title = title
|
||||
, body = content }
|
||||
|
||||
{ title = title
|
||||
, body = content
|
||||
}
|
||||
|
||||
|
||||
type Msg
|
||||
= UrlChanged Url.Url
|
||||
| LinkClicked Browser.UrlRequest
|
||||
| SessionLoaded (Maybe Session)
|
||||
| GotChestMsg Chest.Msg
|
||||
|
||||
|
||||
|
||||
-- | GotAdminMsg Admin.Msg
|
||||
|
||||
|
||||
@@ -98,15 +121,27 @@ update msg model =
|
||||
case model of
|
||||
Chest chest ->
|
||||
let
|
||||
(newChest, cmd) =
|
||||
( newChest, cmd ) =
|
||||
Chest.update chestMsg chest
|
||||
in
|
||||
(Chest newChest, Cmd.map GotChestMsg cmd)
|
||||
|
||||
_ -> (About, Cmd.none)
|
||||
( Chest newChest, Cmd.map GotChestMsg cmd )
|
||||
|
||||
_ ->
|
||||
( About, Cmd.none )
|
||||
in
|
||||
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 ->
|
||||
case model of
|
||||
Chest chestModel ->
|
||||
@@ -115,9 +150,10 @@ update msg model =
|
||||
( model, Nav.pushUrl chestModel.navKey (Url.toString url) )
|
||||
|
||||
Browser.External href ->
|
||||
( model, Cmd.none)
|
||||
( model, Cmd.none )
|
||||
|
||||
_ -> (model, Cmd.none)
|
||||
_ ->
|
||||
( model, Cmd.none )
|
||||
|
||||
UrlChanged url ->
|
||||
let
|
||||
@@ -127,12 +163,15 @@ update msg model =
|
||||
case route of
|
||||
Just (Route.Home content) ->
|
||||
updateChest (Chest.SetContent content)
|
||||
|
||||
_ ->
|
||||
(About, Cmd.none)
|
||||
( About, Cmd.none )
|
||||
|
||||
GotChestMsg chestMsg ->
|
||||
updateChest chestMsg
|
||||
|
||||
|
||||
|
||||
-- STATE Utils
|
||||
-- SUBSCRIPTIONS
|
||||
--
|
||||
@@ -141,5 +180,3 @@ update msg model =
|
||||
subscriptions : Model -> Sub Msg
|
||||
subscriptions _ =
|
||||
Sub.none
|
||||
|
||||
|
||||
|
||||
@@ -1,13 +1,41 @@
|
||||
module Session exposing (Session(..), playerSession)
|
||||
module Session exposing (Session(..), init, playerSession)
|
||||
|
||||
import Browser.Navigation as Nav
|
||||
import Api
|
||||
import Http
|
||||
import Json.Decode as D
|
||||
|
||||
|
||||
type Session
|
||||
= Player Nav.Key Int
|
||||
|
||||
|
||||
|
||||
-- | Admin Nav.Key
|
||||
|
||||
|
||||
playerSession 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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user