Swap part1 and part2

This commit is contained in:
Richard Feldman
2018-08-13 05:04:35 -04:00
parent 353623f108
commit 493b5681d4
16 changed files with 156 additions and 156 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,21 +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 =
-- 👉 TODO make Cred an opaque type, then fix the resulting compiler errors.
-- Afterwards, it should no longer be possible for any other module to access
-- this `token` vale directly!
--
-- 💡 HINT: Other modules still depend on being able to access the
-- `username` value. Expand this module's API to expose a new way for them
-- to access the `username` without also giving them access to `token`.
{ username : Username
, token : String
}
type Cred
= Cred Username String
-- INFO
username : Cred -> Username
username (Cred val _) =
val
@@ -40,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