Skip to content

Commit

Permalink
Merge pull request #77 from dwyl/downgrade-plug-version-for-phoenix-c…
Browse files Browse the repository at this point in the history
…ompatibility-issue#76

PR: Downgrade plug version for phoenix compatibility issue #76
  • Loading branch information
SimonLab authored Nov 17, 2022
2 parents 1834b53 + 67781a5 commit 0598657
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 37 deletions.
48 changes: 19 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ all your authentication/authorization needs.
![GitHub Workflow Status](https://img.shields.io/github/workflow/status/dwyl/auth_plug/Elixir%20CI?label=build&style=flat-square)
[![codecov.io](https://img.shields.io/codecov/c/github/dwyl/auth_plug/master.svg?style=flat-square)](http://codecov.io/github/dwyl/auth_plug?branch=main)
[![Hex.pm](https://img.shields.io/hexpm/v/auth_plug?color=brightgreen&style=flat-square)](https://hex.pm/packages/auth_plug)
[![Libraries.io dependency status](https://img.shields.io/librariesio/release/hex/auth_plug?logoColor=brightgreen&style=flat-square)](https://libraries.io/hex/auth_plug)
[![HitCount](http://hits.dwyl.com/dwyl/auth_plug.svg)](http://hits.dwyl.com/dwyl/auth_plug)
[![contributions welcome](https://img.shields.io/badge/contributions-welcome-brightgreen.svg?style=flat-square)](https://github.com/dwyl/auth_plug/issues)

<!--
[![Libraries.io dependency status](https://img.shields.io/librariesio/release/hex/auth_plug?logoColor=brightgreen&style=flat-square)](https://libraries.io/hex/auth_plug)
-->
</div>
<br />

Expand All @@ -22,12 +26,12 @@ in the fewest steps and least code.
We did too. So we built `auth_plug`.
-->

***Frustrated*** by the **complexity**
**_Frustrated_** by the **complexity**
and **incomplete docs/tests**
in **_existing_ auth solutions**,
we built **`auth_plug`** to **simplify** our lives. <br />

We needed a way to ***minimise***
We needed a way to **_minimise_**
the steps
and **code** required
to add auth to our app(s).
Expand All @@ -39,9 +43,6 @@ and **_one_ environment variable**.

![true](https://user-images.githubusercontent.com/194400/80473192-b1f73500-893d-11ea-87c1-edf4fec53da2.jpg)




<!-- revisit or remove this section
### Pain 😧
Expand Down Expand Up @@ -101,9 +102,7 @@ to help _everyone understand_ how it works.
As with _all_ our code,
it's meant to be as beginner-friendly as possible.
If you get stuck or have any questions,
please [***ask***!](https://github.com/dwyl/auth_plug/issues)


please [**_ask_**!](https://github.com/dwyl/auth_plug/issues)

## Who? 👥

Expand All @@ -115,13 +114,10 @@ that _anyone_ can use.
If after reading through this you feel that
this is something you would like to have
in your own Elixir/Phoenix project,
[***tell us***!](https://github.com/dwyl/auth_plug/issues)

[**_tell us_**!](https://github.com/dwyl/auth_plug/issues)

# How? 💡



_Before_ you attempt to use the **`auth_plug`**,
try the Heroku example version so you know what to expect: <br />
https://auth-plug-example.herokuapp.com/admin
Expand All @@ -140,8 +136,6 @@ with a JWT session.
For more detail on how the `Auth` service works,
please see: https://github.com/dwyl/auth



If you get stuck during setup,
clone and run our fully working example:
https://github.com/dwyl/auth_plug_example#how
Expand All @@ -156,10 +150,11 @@ to your list of dependencies in `mix.exs`:
```elixir
def deps do
[
{:auth_plug, "~> 1.4.14"}
{:auth_plug, "~> 1.4"}
]
end
```

Once you've saved the `mix.exs` file,
download the dependency with:

Expand Down Expand Up @@ -189,14 +184,16 @@ export AUTH_API_KEY=2cfxNaWUwJBq1F4nPndoEHZJ5Y/2cfxNadrhMZk3iaT1L5k6Wt67c9ScbGNP
```

The run the following command in your terminal:

```
source .env
```
That will export the environment variable AUTH_API_KEY.

That will export the environment variable AUTH_API_KEY.

Remember to add `.env` to your [`.gitignore`](https://github.com/dwyl/auth_plug/blob/1ebb60938487da7e740a79b2a4639b29f2ba44ac/.gitignore#L52) file.
e.g:

```
echo ".env" >> .gitignore
```
Expand Down Expand Up @@ -228,21 +225,20 @@ Immediately below this add the following lines of code:
```

> E.g:
[`/lib/app_web/router.ex#L23-L29`](https://github.com/dwyl/auth_plug_example/blob/8ce0f10e656b94a93b8f02af240b3897ce23c006/lib/app_web/router.ex#L23-L29)

> [`/lib/app_web/router.ex#L23-L29`](https://github.com/dwyl/auth_plug_example/blob/8ce0f10e656b94a93b8f02af240b3897ce23c006/lib/app_web/router.ex#L23-L29)
#### _Explanation_

There are two parts to this code:

1. Create a new pipeline called `:auth` which will execute the `AuthPlug`.
2. Create a new scope where we `pipe_through`
both the `:browser` and `:auth` pipelines.
both the `:browser` and `:auth` pipelines.

This means that the `"/admin"` route is protected by `AuthPlug`.

> **Note**: Ensure the route you are protecting works _without_ `AuthPlug`.
If in doubt simply comment out the line `pipe_through :auth` to check.
> If in doubt simply comment out the line `pipe_through :auth` to check.
<br />

Expand Down Expand Up @@ -293,7 +289,6 @@ pipeline :authoptional, do: plug(AuthPlugOptional, %{})
e.g:
[`/lib/app_web/router.ex#L13`](https://github.com/dwyl/auth_plug_example/blob/f4c79e540ed8ef2d4c587647b31e93fec9855f59/lib/app_web/router.ex#L13)


Then add the following line to your main router scope:

```elixir
Expand All @@ -303,24 +298,20 @@ pipe_through :authoptional
e.g:
[`/lib/app_web/router.ex#L17`](https://github.com/dwyl/auth_plug_example/blob/f4c79e540ed8ef2d4c587647b31e93fec9855f59/lib/app_web/router.ex#L17)


That's it now you can check for `conn.assigns.person` in your templates
and display relevant info/actions to the person if they are logged in.

```html
<%= if Map.has_key?(@conn.assigns, :person) do %>
Hello <%= @conn.assigns.person.givenName %>!
<% end %>
<%= if Map.has_key?(@conn.assigns, :person) do %> Hello <%=
@conn.assigns.person.givenName %>! <% end %>
```

e.g:
[`/lib/app_web/templates/page/optional.html.eex#L2-L3`](https://github.com/dwyl/auth_plug_example/blob/f4c79e540ed8ef2d4c587647b31e93fec9855f59/lib/app_web/templates/page/optional.html.eex#L2-L3)


<br />
Try it: http://auth-plug-example.herokuapp.com/optional


<br />

## Documentation
Expand Down Expand Up @@ -352,7 +343,6 @@ person :: %{
}
```


## Recommended / Relevant Reading

If you are new to Elixir Plug,
Expand Down
6 changes: 3 additions & 3 deletions lib/auth_plug.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ defmodule AuthPlug do
require Logger

# Moch HTTPoison requests in Dev/Test, see: https://github.com/dwyl/elixir-auth-google/issues/35
@httpoison (Application.get_env(:auth_plug, :httpoison_mock) && AuthPlug.HTTPoisonMock) ||
@httpoison (Application.compile_env(:auth_plug, :httpoison_mock) && AuthPlug.HTTPoisonMock) ||
HTTPoison

@doc """
Expand Down Expand Up @@ -106,11 +106,11 @@ defmodule AuthPlug do
end

@doc """
`end_session/1` makes an HTTP Request to the auth_url
`end_session/1` makes an HTTP Request to the auth_url
to end the session. This in turn makes the update on the auth app
to update the session.end so the owner of the "consumer" app
knows when the person logged out.
`end_session/1` is invoked by `AuthPlug.logout/1` (above)
`end_session/1` is invoked by `AuthPlug.logout/1` (above)
which will likely be the function called in practice.
"""
def end_session(conn) do
Expand Down
11 changes: 6 additions & 5 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule AuthPlug.MixProject do
def project do
[
app: :auth_plug,
version: "1.4.14",
version: "1.4.21",
elixir: "~> 1.12",
elixirc_paths: elixirc_paths(Mix.env()),
start_permanent: Mix.env() == :prod,
Expand All @@ -16,6 +16,7 @@ defmodule AuthPlug.MixProject do
preferred_cli_env: [
coveralls: :test,
"coveralls.detail": :test,
"coveralls.json": :test,
"coveralls.post": :test,
"coveralls.html": :test,
c: :test
Expand Down Expand Up @@ -45,13 +46,13 @@ defmodule AuthPlug.MixProject do
{:httpoison, "~> 1.8.0"},

# JWT sign/verify: github.com/joken-elixir/joken
{:joken, "~> 2.5.0"},
{:joken, "~> 2.5"},

# Plug helper functions: github.com/elixir-plug/plug
{:plug, "~> 1.14.0"},
{:plug, "~> 1.10"},

# Decoding JSON data: https://hex.pm/packages/jason
{:jason, "~> 1.3"},
{:jason, "~> 1.0"},

# Check/get Environment Variables: https://github.com/dwyl/envar
{:envar, "~> 1.0.8"},
Expand All @@ -77,7 +78,7 @@ defmodule AuthPlug.MixProject do
files:
~w(lib/auth_plug.ex lib/auth_plug_optional.ex lib/helpers.ex lib/httpoison_mock.ex lib/token.ex LICENSE mix.exs README.md),
name: "auth_plug",
licenses: ["GNU GPL v2.0"],
licenses: ["GPL-2.0-or-later"],
maintainers: ["dwyl"],
links: %{"GitHub" => "https://github.com/dwyl/auth_plug"}
]
Expand Down

0 comments on commit 0598657

Please sign in to comment.