Skip to content

Commit

Permalink
Client Auth
Browse files Browse the repository at this point in the history
  • Loading branch information
lsocrate committed Sep 23, 2021
1 parent 06e7058 commit 02f77bc
Show file tree
Hide file tree
Showing 9 changed files with 194 additions and 154 deletions.
22 changes: 13 additions & 9 deletions client/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,35 @@
return fetch("/cards.json").then((res) => res.json());
}

async function handleAuthcallback(auth0) {
const query = window.location.search;
if (query.includes("code=") && query.includes("state=")) {
await auth0.handleRedirectCallback();
}
window.history.replaceState({}, document.title, "/");
}

Promise.all([fetchCards(), configureClient()]).then(
async ([cards, auth0]) => {
const query = window.location.search;
if (query.includes("code=") && query.includes("state=")) {
await auth0.handleRedirectCallback();
}
window.history.replaceState({}, document.title, "/");
await handleAuthcallback(auth0);

const app = Elm.Main.init({ flags: cards });

app.ports.initiateLogin.subscribe((redirectUri) => {
auth0.loginWithRedirect({ redirect_uri: window.location.origin });
});
app.ports.signOut.subscribe((redirectUri) => {
auth0.logout({ returnTo: window.location.origin });
});

const isAuthenticated = await auth0.isAuthenticated();
if (isAuthenticated) {
const [token, user] = await Promise.all([
auth0.getTokenSilently(),
auth0.getUser(),
]);
window.setTimeout(() => {
window.app = app

app.ports.signIn.send({ token, user });
},1000)
app.ports.signInReceiver.send({ token, user });
}
}
);
Expand Down
9 changes: 1 addition & 8 deletions client/src/Auth.elm
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
port module Auth exposing
( User
, beforeProtectedInit
, initiateLogin
)
module Auth exposing (User, beforeProtectedInit)

import ElmSpa.Page as ElmSpa
import Gen.Route exposing (Route)
Expand All @@ -14,9 +10,6 @@ type alias User =
Shared.User


port initiateLogin : String -> Cmd msg


beforeProtectedInit : Shared.Model -> Request -> ElmSpa.Protected User Route
beforeProtectedInit shared _ =
case shared.user of
Expand Down
71 changes: 70 additions & 1 deletion client/src/Deck.elm
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
module Deck exposing (Deck, Faction, copiesInDeck, empty, isLeader, isLegal, isValidFaction, isValidLibrary, leader, setCard, setLeader)
module Deck exposing (Deck, Faction, copiesInDeck, demoDeck, empty, isLeader, isLegal, isValidFaction, isValidLibrary, leader, setCard, setLeader)

import Cards
import Dict exposing (Dict)
import Shared exposing (Collection)


type alias Deck =
Expand Down Expand Up @@ -147,3 +148,71 @@ isValidLibrary deck =
List.foldl (\( _, n ) sum -> sum + n) 0 <| Dict.values deck.library
in
(deckSize == 0) || (deckSize >= 40 && deckSize <= 60)


demoDeck : Collection -> Deck
demoDeck collection =
let
agenda =
Dict.get "core-base-of-power" collection
|> Maybe.andThen
(\c ->
case c of
Cards.AgendaCard a ->
Just a

_ ->
Nothing
)

haven =
Dict.get "core-artist-lofts" collection
|> Maybe.andThen
(\c ->
case c of
Cards.HavenCard a ->
Just a

_ ->
Nothing
)

toCardCount n _ c =
( c, n )

faction =
collection
|> Dict.toList
|> List.filterMap
(\( k, c ) ->
case c of
Cards.FactionCard f ->
Just ( k, ( f, f.name == "Aurora Nix" ) )

_ ->
Nothing
)
|> List.take 7
|> Dict.fromList

library =
collection
|> Dict.toList
|> List.filterMap
(\( k, c ) ->
case c of
Cards.LibraryCard l ->
Just ( k, l )

_ ->
Nothing
)
|> List.take 14
|> Dict.fromList
|> Dict.map (toCardCount 3)
in
{ agenda = agenda
, haven = haven
, faction = faction
, library = library
}
110 changes: 22 additions & 88 deletions client/src/Pages/Build.elm
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module Pages.Build exposing (Model, Msg, page)
import Cards exposing (Card)
import Deck exposing (Deck)
import Dict
import Effect exposing (Effect)
import Gen.Params.Build exposing (Params)
import Html exposing (..)
import Html.Attributes exposing (..)
Expand All @@ -22,10 +23,10 @@ import View exposing (View)

page : Shared.Model -> Request.With Params -> Page.With Model Msg
page shared req =
Page.element
Page.advanced
{ init = init shared.collection req
, update = update
, view = view
, view = view shared.user
, subscriptions = always Sub.none
}

Expand All @@ -50,7 +51,7 @@ type alias Model =
}


init : Collection -> Request.With Params -> ( Model, Cmd Msg )
init : Collection -> Request.With Params -> ( Model, Effect Msg )
init collection req =
( { collection = collection
, header = UI.Layout.Header.init req
Expand All @@ -65,78 +66,10 @@ init collection req =
, showCollectionImages = False
, deck = Deck.empty
}
, Cmd.none
, Effect.none
)


demoDeck : Collection -> Deck
demoDeck collection =
let
agenda =
Dict.get "core-base-of-power" collection
|> Maybe.andThen
(\c ->
case c of
Cards.AgendaCard a ->
Just a

_ ->
Nothing
)

haven =
Dict.get "core-artist-lofts" collection
|> Maybe.andThen
(\c ->
case c of
Cards.HavenCard a ->
Just a

_ ->
Nothing
)

toCardCount n _ c =
( c, n )

faction =
collection
|> Dict.toList
|> List.filterMap
(\( k, c ) ->
case c of
Cards.FactionCard f ->
Just ( k, ( f, f.name == "Aurora Nix" ) )

_ ->
Nothing
)
|> List.take 7
|> Dict.fromList

library =
collection
|> Dict.toList
|> List.filterMap
(\( k, c ) ->
case c of
Cards.LibraryCard l ->
Just ( k, l )

_ ->
Nothing
)
|> List.take 14
|> Dict.fromList
|> Dict.map (toCardCount 3)
in
{ agenda = agenda
, haven = haven
, faction = faction
, library = library
}


type Msg
= FromHeader UI.Layout.Header.Msg
| FromStacksFilter (UI.FilterSelection.Msg Cards.CardStack)
Expand All @@ -153,7 +86,7 @@ type Msg
| ChoseLeader Cards.Faction


update : Msg -> Model -> ( Model, Cmd Msg )
update : Msg -> Model -> ( Model, Effect Msg )
update msg model =
case msg of
TextFilterChanged text ->
Expand All @@ -169,30 +102,30 @@ update msg model =
else
Just cleanText
}
, Cmd.none
, Effect.none
)

FromHeader subMsg ->
UI.Layout.Header.update subMsg model.header
|> Tuple.mapFirst (\newHeader -> { model | header = newHeader })

FromStacksFilter subMsg ->
( { model | stackFilters = UI.FilterSelection.update subMsg model.stackFilters }, Cmd.none )
( { model | stackFilters = UI.FilterSelection.update subMsg model.stackFilters }, Effect.none )

FromPrimaryFilter subMsg ->
( { model | primaryFilters = UI.FilterSelection.update subMsg model.primaryFilters }, Cmd.none )
( { model | primaryFilters = UI.FilterSelection.update subMsg model.primaryFilters }, Effect.none )

FromSecondaryFilter subMsg ->
( { model | secondaryFilters = UI.FilterSelection.update subMsg model.secondaryFilters }, Cmd.none )
( { model | secondaryFilters = UI.FilterSelection.update subMsg model.secondaryFilters }, Effect.none )

FromAttackTypesFilter subMsg ->
( { model | attackTypeFilters = UI.FilterSelection.update subMsg model.attackTypeFilters }, Cmd.none )
( { model | attackTypeFilters = UI.FilterSelection.update subMsg model.attackTypeFilters }, Effect.none )

FromClansFilter subMsg ->
( { model | clansFilters = UI.FilterSelection.update subMsg model.clansFilters }, Cmd.none )
( { model | clansFilters = UI.FilterSelection.update subMsg model.clansFilters }, Effect.none )

FromDisciplinesFilter subMsg ->
( { model | disciplineFilters = UI.FilterSelection.update subMsg model.disciplineFilters }, Cmd.none )
( { model | disciplineFilters = UI.FilterSelection.update subMsg model.disciplineFilters }, Effect.none )

ClearFilters ->
( { model
Expand All @@ -204,33 +137,34 @@ update msg model =
, disciplineFilters = UI.FilterSelection.disciplines
, textFilter = Nothing
}
, Cmd.none
, Effect.none
)

ToggleShowAllFilters ->
( { model | showAllFilters = not model.showAllFilters }, Cmd.none )
( { model | showAllFilters = not model.showAllFilters }, Effect.none )

ToggleShowCollectionImages ->
( { model | showCollectionImages = not model.showCollectionImages }, Cmd.none )
( { model | showCollectionImages = not model.showCollectionImages }, Effect.none )

ChangedDecklist change ->
( { model | deck = Deck.setCard model.deck change }, Cmd.none )
( { model | deck = Deck.setCard model.deck change }, Effect.none )

ChoseLeader leader ->
( { model | deck = Deck.setLeader model.deck leader }, Cmd.none )
( { model | deck = Deck.setLeader model.deck leader }, Effect.none )


view : Model -> View Msg
view model =
view : Maybe Shared.User -> Model -> View Msg
view user model =
UI.Layout.Template.view FromHeader
user
[ div [ class "deckbldr" ]
[ div [ class "deckbldr-actions" ]
[ div [] [ p [] [ text "Save" ] ]
]
, div [ class "deckbldr-decklist" ]
[ div [ class "decklist" ]
[ div [ class "decklist-title" ] [ text "Decklist name" ]
, div [ class "decklist-byline" ] [ text "By: Xyz." ]
, div [ class "decklist-byline" ] [ user |> Maybe.map (\{ id } -> text ("By: " ++ id)) |> Maybe.withDefault (text "By: unknown") ]
, div [ class "decklist-core", class "decklist-core--agenda" ]
[ p [ class "decklist-section_header" ]
[ text "Agenda: "
Expand Down
Loading

0 comments on commit 02f77bc

Please sign in to comment.