Swap part1 and part2
This commit is contained in:
@@ -1,20 +1,4 @@
|
||||
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!
|
||||
|
||||
-}
|
||||
module Viewer.Cred exposing (Cred, addHeader, addHeaderIfAvailable, decoder, encodeToken)
|
||||
|
||||
import HttpBuilder exposing (RequestBuilder, withHeader)
|
||||
import Json.Decode as Decode exposing (Decoder)
|
||||
@@ -23,30 +7,21 @@ 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 Cred
|
||||
= Cred Username String
|
||||
|
||||
|
||||
|
||||
-- INFO
|
||||
|
||||
|
||||
username : Cred -> Username
|
||||
username (Cred val _) =
|
||||
val
|
||||
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
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -65,14 +40,14 @@ decoder =
|
||||
|
||||
|
||||
encodeToken : Cred -> Value
|
||||
encodeToken (Cred _ str) =
|
||||
Encode.string str
|
||||
encodeToken cred =
|
||||
Encode.string cred.token
|
||||
|
||||
|
||||
addHeader : Cred -> RequestBuilder a -> RequestBuilder a
|
||||
addHeader (Cred _ str) builder =
|
||||
addHeader cred builder =
|
||||
builder
|
||||
|> withHeader "authorization" ("Token " ++ str)
|
||||
|> withHeader "authorization" ("Token " ++ cred.token)
|
||||
|
||||
|
||||
addHeaderIfAvailable : Maybe Cred -> RequestBuilder a -> RequestBuilder a
|
||||
|
||||
Reference in New Issue
Block a user