SimpleJWTAuthentication
Authorizes user requests by verifying a JWT against a supplied secret key.
Currently only supports using the Authorization header with the value in the
format Bearer YOUR_JWT. Can also accept required fields as opts, where the
key is the field name (and what it will be assigned as on the connection), and
the value is the path (that will be passed in to Kernel.get_in/2) to the
field.
Usage
Phoenix Integration
-
Inside
web/router.exfile, add plug to your pipeline like so:
defmodule MyApp.Router
use Phoenix.Router
pipeline :api do
plug SimpleJWTAuthentication
end
scope "/", MyApp do
pipe_through :api
get "/hello", HelloController, :hello
end
end- With required fields
pipeline :api do
plug SimpleJWTAuthentication, user_id: ["metadata", "user_id"]
end
Installation
-
Add
simple_jwt_authenticationto your list of dependencies inmix.exs:
```elixir
def deps do
[{:simple_jwt_authentication, "~> 0.1.0"}]
end
```Ensure will need to supply the
:josepackage with a JSON Encoder/Decoder, which can be either jiffy, jsone, jsx, or PoisonEnsure
simple_jwt_authenticationis started before your application:
```elixir
def application do
[applications: [:simple_jwt_authentication]]
end
```-
Configure your token in
config.exs:config :simple_jwt_authentication, secret: "your-secret-here"