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.
decodeToken converts the token content to an Elm record structure.
decoderToken dec tokenIn the event of success, decodeToken returns an Elm record structure using the JSON Decoder.
send is a replacement for Http.send that includes a Jwt token
get is a replacement for Http.get that attaches a provided Jwt token
to the headers of the GET request.
Task.perform
PostFail PostSucess
(get token dataDecoder "/api/data")post is a replacement for Http.post that attaches a provided Jwt token
to the headers, and sets the Content-type to 'application/json'.
getWithJwt is an alias for get, provided for backwards compatibility.
isExpired checks whether a token remains valid, i.e. it has not expired.
Note: This function assumes that the expiry was set in seconds and thus multiplies by 100 to compare with javascript time (in milliseconds). You may need to write a custom version if your Jwt provide works differently.
promote401 promotes a 401 Unauthorized Http reponse to the JwtError TokenExpired.
Jwt.get token dataDecoder "/api/data"
`Task.onError` (promote401 token)
|> Task.perform PostFail PostSucessThe three errors that can emerge are:
authenticateis 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.