This a Elixir Library implementation for Auth0 JWT Authentication. It brings plugs for Authentication, ResourceLoading and EnforcingAuthentication.
If available in Hex, the package can be installed as:
- Add
authex
to your list of dependencies inmix.exs
:
```elixir
def deps do
[{:authex, "~> 0.1.0"}]
end
```
- Ensure
authex
is started before your application:
```elixir
def application do
[applications: [:authex]]
end
```
To use Authex simply plug the different implementations to your pipeline. Note: Remember to add the Auth0 keys in the configuration of your app.
The TokenAuth Plug simply checks for a token in the connection and decodes it to get the subject data.
Example:
plug MyApp.PlugOne
plug Authex.TokenAuth
The LoadCurrentResource Plug loads the subject of the token authenticatedby the TokenAuth Plug and makes it available through the Authex.current_resource/1 method.
This module plug takes some required parameters and some options.
Required:
- Model: Refers to the model used for autheticantion. (often User model)
- Repo: Refers to the Repo module used to interact with the Database
Optional:
- Field: Refers to the field used to fetch and load the current resource
Examples:
plug Authex.TokenAuth \n
plug Authex.LoadCurrentResource, model: MyApp.User, repo: MyApp.Repo
plug Authex.TokenAuth \n
plug Authex.LoadCurrentResource, model: MyApp.User, repo: MyApp.Repo, field: :email
The Enforce Authentication plug requires an error handler module implementing an :authentication_error/1 that takes a Plug connection. This module will be used by the EnforceAuthentication plug when it encounters an authentication error. This module plug is designed to be used in controllers.
Example:
plug Authex.LoadCurrentResource, handler: __MODULE__
def authentication_error(conn) do
conn
|> put_status(401)
|> render("error.json", message: "authentication error")
end
- Fork it ( https://github.com/[my-github-username]/authex/fork )
- Create your feature branch (
git checkout -b feature/my_new_feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request
- All the contributors to the Elixir Library Guardian