Update part3

This commit is contained in:
Richard Feldman
2018-08-13 05:49:56 -04:00
parent 50da3881b1
commit bd72768e6f
21 changed files with 954 additions and 593 deletions

View File

@@ -1,4 +1,20 @@
module Viewer.Cred exposing (Cred, addHeader, addHeaderIfAvailable, decoder, encodeToken)
module Viewer.Cred exposing (Cred, addHeader, addHeaderIfAvailable, decoder, encodeToken, username)
{-| The authentication credentials for the Viewer (that is, the currently logged-in user.)
This includes:
- The cred's Username
- The cred's authentication token
By design, there is no way to access the token directly as a String.
It can be encoded for persistence, and it can be added to a header
to a HttpBuilder for a request, but that's it.
This token should never be rendered to the end user, and with this API, it
can't be!
-}
import HttpBuilder exposing (RequestBuilder, withHeader)
import Json.Decode as Decode exposing (Decoder)
@@ -7,14 +23,30 @@ import Json.Encode as Encode exposing (Value)
import Username exposing (Username)
{-| The authentication token for the currently logged-in user.
The token records the username associated with this token, which you can ask it for.
By design, there is no way to access the token directly as a String. You can encode it for persistence, and you can add it to a header to a HttpBuilder for a request, but that's it.
-}
-- TYPES
type alias Cred =
{ username : Username
, token : String
}
type Cred
= Cred Username String
-- INFO
username : Cred -> Username
username (Cred val _) =
val
@@ -33,14 +65,14 @@ decoder =
encodeToken : Cred -> Value
encodeToken cred =
Encode.string cred.token
encodeToken (Cred _ str) =
Encode.string str
addHeader : Cred -> RequestBuilder a -> RequestBuilder a
addHeader cred builder =
addHeader (Cred _ str) builder =
builder
|> withHeader "authorization" ("Token " ++ cred.token)
|> withHeader "authorization" ("Token " ++ str)
addHeaderIfAvailable : Maybe Cred -> RequestBuilder a -> RequestBuilder a