simonh1000/elm-jwt/1.0.2

NOTE: this package is not compatible with Elm 0.19.1

Jwt

Helper functions for Jwt token authentication.

A Jwt Token comprises 3 elements: a header and footer and the content. This package includes a function to send an authentication request, a function to read the content of a token; and a function to send GET requests with the token attached.

API functions

Decoder String
-> String
-> String
-> Task never (Result JwtError String)

authenticate is a custom Http POST method that sends a stringified Json object containing the login credentials. It then extracts the token from the json response from the server and returns it.

authenticate
    ("token" := Json.string)
    "http://localhost:5000/auth"
    ("{\"username\":\"" ++ model.uname ++ "\",\"password\":\""++ model.pword ++ "\"}")
        |> Task.map Token
        |> Effects.task
decodeToken : Decoder a -> String -> Result JwtError a

decodeToken converts the token content to an Elm record structure.

decoderToken dec token

In the event of success, decodeToken returns an Elm record structure using the JSON Decoder.

getWithJwt : String -> Decoder a -> String -> Task Error a

getWithJwt is a replacement for Http.get that attaches a provided Jwt token to the headers of the GET request.

getWithJwt model.token "http://localhost:5000/api/restos/test"
    |> Task.toResult
    |> Task.map AuthData
    |> Effects.tasks

Errors

= HttpError String
| TokenProcessingError String
| TokenDecodeError String

The three errors that can emerge are:

  • network errors,
  • issues with processing (e.g. base 64 decoding) the token, and
  • problems decoding the json data within the content of the token