Rupert, here is a high level overview of how we currently structure our elm 

   1. Top layer: This layer manages routing, page changes, and page 
   resource loading, and current user state
   2. Page layer: These are individual pages - typically one page for each 
   url. There may be deeper levels of widgets/views within each page, but 
   we don't have pages nested within pages.

When the top layer is notified of a page change (this mechanism can vary 
from app to app), it first communicates with the backend server to load any 
resources that are needed to display that page. If the resource load 
succeeds, the page is displayed by changing the 'currentRoute' to the new 
routing state, and providing the loaded resources to the new page. If the 
load fails, however, you can check the http error to determine why. If 
you're received a 401 failure, you can quickly change to your "login route" 
and ask the user to login.

Keep in mind that method allows you to catch and mange http errors that 
occur during page transitions, and not from http requests made from within 
a page. We're fine with that tradeoff, because 99% of the time, you'll only 
need to catch 401 during page transitions anyways.

Hopefully that helps.

On Friday, September 16, 2016 at 11:00:39 AM UTC-4, Rupert Smith wrote:
> On Friday, September 16, 2016 at 2:49:25 PM UTC+1, Rupert Smith wrote:
>> I think I am going to try the solution from the "Inter-Component 
>> Communication in 0.17" thread. Which is to create pairs of messaging ports.
> The module I came with is below. I can import it anywhere and make use of 
> these auth functions - this will come in handy for setting the not authed 
> state on any http request that fails with a 401.
> I just need to think about how to add functions to query the auth state on 
> this module too, as it would also be nice to be able to query that all over 
> the place too. Something like
> isLoggedIn : Bool
> hasPermission : String -> Bool
> would cover my needs. Anyway, here is the Auth module:
> port module Auth exposing (..)
> type alias Credentials =
>     { username : String
>     , password : String
>     }
> login : Credentials -> Cmd msg
> login authRequest =
>     sendLogin authRequest
> logout : Cmd msg
> logout =
>     sendLogout ()
> unauthed : Cmd msg
> unauthed =
>     sendUnauthed ()
> port sendLogin : Credentials -> Cmd msg
> port sendLogout : () -> Cmd msg
> port sendUnauthed : () -> Cmd msg

You received this message because you are subscribed to the Google Groups "Elm 
Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
For more options, visit

Reply via email to