Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add custom page auth example, fix bug with asking for a view and not …
…using it
- Loading branch information
1 parent
7e9819e
commit a080254
Showing
17 changed files
with
361 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/dist | ||
/.elm-land | ||
/.env | ||
/elm-stuff | ||
/node_modules | ||
.DS_Store | ||
*.pem |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# 20-auth-error-page | ||
> Built with [Elm Land](https://elm.land) 🌈 | ||
## Local development | ||
|
||
```bash | ||
# Requires Node.js v18+ (https://nodejs.org) | ||
npx elm-land server | ||
``` | ||
|
||
## Deploying to production | ||
|
||
Elm Land projects are most commonly deployed as static websites. | ||
|
||
Please visit [the "Deployment" guide](https://elm.land/guide/deploying) to learn more | ||
about deploying your app for free using Netlify or Vercel. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"app": { | ||
"elm": { | ||
"development": { "debugger": true }, | ||
"production": { "debugger": false } | ||
}, | ||
"env": [], | ||
"html": { | ||
"attributes": { | ||
"html": { "lang": "en" }, | ||
"head": {} | ||
}, | ||
"title": "Elm Land", | ||
"meta": [ | ||
{ "charset": "UTF-8" }, | ||
{ "http-equiv": "X-UA-Compatible", "content": "IE=edge" }, | ||
{ "name": "viewport", "content": "width=device-width, initial-scale=1.0" } | ||
], | ||
"link": [], | ||
"script": [] | ||
}, | ||
"router": { | ||
"useHashRouting": false | ||
}, | ||
"proxy": null | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"type": "application", | ||
"source-directories": [ | ||
"src", | ||
".elm-land/src" | ||
], | ||
"elm-version": "0.19.1", | ||
"dependencies": { | ||
"direct": { | ||
"elm/browser": "1.0.2", | ||
"elm/core": "1.0.5", | ||
"elm/html": "1.0.0", | ||
"elm/json": "1.1.3", | ||
"elm/url": "1.0.0" | ||
}, | ||
"indirect": { | ||
"elm/time": "1.0.0", | ||
"elm/virtual-dom": "1.0.3" | ||
} | ||
}, | ||
"test-dependencies": { | ||
"direct": {}, | ||
"indirect": {} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
module Auth exposing (User, onPageLoad, viewCustomPage) | ||
|
||
import Auth.Action | ||
import Dict | ||
import Html | ||
import Route exposing (Route) | ||
import Route.Path | ||
import Shared | ||
import Shared.Model | ||
import User | ||
import View exposing (View) | ||
|
||
|
||
type alias User = | ||
User.User | ||
|
||
|
||
{-| Called before an auth-only page is loaded. | ||
-} | ||
onPageLoad : Shared.Model -> Route () -> Auth.Action.Action User | ||
onPageLoad shared route = | ||
case shared.authStatus of | ||
Shared.Model.NotLoggedIn -> | ||
Auth.Action.loadCustomPage | ||
|
||
Shared.Model.LoggedInAs user -> | ||
Auth.Action.loadPageWithUser user | ||
|
||
Shared.Model.TokenExpired -> | ||
Auth.Action.loadCustomPage | ||
|
||
|
||
{-| Renders whenever `Auth.Action.showCustomView` is returned from `onPageLoad`. | ||
-} | ||
viewCustomPage : Shared.Model -> Route () -> View Never | ||
viewCustomPage shared route = | ||
case shared.authStatus of | ||
Shared.Model.NotLoggedIn -> | ||
{ title = "Permission denied" | ||
, body = [ Html.text "You need to be logged in to see this!" ] | ||
} | ||
|
||
Shared.Model.LoggedInAs user -> | ||
{ title = "Loading..." | ||
, body = [] | ||
} | ||
|
||
Shared.Model.TokenExpired -> | ||
{ title = "Token expired" | ||
, body = [ Html.text "Your token expired, here's an error page!" ] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
module Pages.Home_ exposing (Model, Msg, page) | ||
|
||
import Auth | ||
import Effect exposing (Effect) | ||
import Html | ||
import Page exposing (Page) | ||
import Route exposing (Route) | ||
import Shared | ||
import View exposing (View) | ||
|
||
|
||
page : Auth.User -> Shared.Model -> Route () -> Page Model Msg | ||
page user shared route = | ||
Page.new | ||
{ init = init | ||
, update = update | ||
, subscriptions = subscriptions | ||
, view = view | ||
} | ||
|
||
|
||
|
||
-- INIT | ||
|
||
|
||
type alias Model = | ||
{} | ||
|
||
|
||
init : () -> ( Model, Effect Msg ) | ||
init () = | ||
( {} | ||
, Effect.none | ||
) | ||
|
||
|
||
|
||
-- UPDATE | ||
|
||
|
||
type Msg | ||
= NoOp | ||
|
||
|
||
update : Msg -> Model -> ( Model, Effect Msg ) | ||
update msg model = | ||
case msg of | ||
NoOp -> | ||
( model | ||
, Effect.none | ||
) | ||
|
||
|
||
|
||
-- SUBSCRIPTIONS | ||
|
||
|
||
subscriptions : Model -> Sub Msg | ||
subscriptions model = | ||
Sub.none | ||
|
||
|
||
|
||
-- VIEW | ||
|
||
|
||
view : Model -> View Msg | ||
view model = | ||
{ title = "Pages.Home_" | ||
, body = [ Html.text "/" ] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
module Shared exposing | ||
( Flags, decoder | ||
, Model, Msg | ||
, init, update, subscriptions | ||
) | ||
|
||
{-| | ||
@docs Flags, decoder | ||
@docs Model, Msg | ||
@docs init, update, subscriptions | ||
-} | ||
|
||
import Effect exposing (Effect) | ||
import Json.Decode | ||
import Route exposing (Route) | ||
import Route.Path | ||
import Shared.Model | ||
import Shared.Msg | ||
|
||
|
||
|
||
-- FLAGS | ||
|
||
|
||
type alias Flags = | ||
{} | ||
|
||
|
||
decoder : Json.Decode.Decoder Flags | ||
decoder = | ||
Json.Decode.succeed {} | ||
|
||
|
||
|
||
-- INIT | ||
|
||
|
||
type alias Model = | ||
Shared.Model.Model | ||
|
||
|
||
init : Result Json.Decode.Error Flags -> Route () -> ( Model, Effect Msg ) | ||
init flagsResult route = | ||
( { authStatus = Shared.Model.TokenExpired -- 👈 Change this to see the different custom pages | ||
} | ||
, Effect.none | ||
) | ||
|
||
|
||
|
||
-- UPDATE | ||
|
||
|
||
type alias Msg = | ||
Shared.Msg.Msg | ||
|
||
|
||
update : Route () -> Msg -> Model -> ( Model, Effect Msg ) | ||
update route msg model = | ||
case msg of | ||
Shared.Msg.NoOp -> | ||
( model | ||
, Effect.none | ||
) | ||
|
||
|
||
|
||
-- SUBSCRIPTIONS | ||
|
||
|
||
subscriptions : Route () -> Model -> Sub Msg | ||
subscriptions route model = | ||
Sub.none |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
module Shared.Model exposing (AuthStatus(..), Model) | ||
|
||
import User exposing (User) | ||
|
||
|
||
type alias Model = | ||
{ authStatus : AuthStatus | ||
} | ||
|
||
|
||
type AuthStatus | ||
= NotLoggedIn | ||
| LoggedInAs User | ||
| TokenExpired |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
module Shared.Msg exposing (Msg(..)) | ||
|
||
{-| -} | ||
|
||
|
||
{-| Normally, this value would live in "Shared.elm" | ||
but that would lead to a circular dependency import cycle. | ||
For that reason, both `Shared.Model` and `Shared.Msg` are in their | ||
own file, so they can be imported by `Effect.elm` | ||
-} | ||
type Msg | ||
= NoOp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module User exposing (User) | ||
|
||
|
||
type alias User = | ||
{ id : Int | ||
, email : String | ||
} |
Oops, something went wrong.