This repository has been archived by the owner on Sep 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Implements RealWorldWeb.Guardian * Implements RealWorldWeb.GuardedController * Changes all the function call as defined on the new version * Upgrade Guardian to 1.0.0 (was 0.14.5), Mime to 1.2.0 (was 1.1.0) and Uuid to 1.1.8 (was 1.1.7)
- Loading branch information
Showing
18 changed files
with
108 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
defmodule RealWorldWeb.GuardedController do | ||
@moduledoc """ | ||
Use this module in a controller to take the advantage of having | ||
the subject of authentication (eg.: an authenticated user) injected | ||
in the action as the third argument. | ||
## Usage example | ||
defmodule RealWorldWeb.MyController do | ||
use RealWorldWeb, :controller | ||
use RealWorldWeb.GuardedController | ||
plug Guardian.Plug.EnsureAuthenticated | ||
def index(conn, params, current_user) do | ||
# ..code.. | ||
end | ||
end | ||
""" | ||
|
||
defmacro __using__(_opts \\ []) do | ||
quote do | ||
def action(conn, _opts) do | ||
apply( | ||
__MODULE__, | ||
action_name(conn), | ||
[ | ||
conn, | ||
conn.params, | ||
RealWorldWeb.Guardian.Plug.current_resource(conn) | ||
] | ||
) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
defmodule RealWorldWeb.Guardian do | ||
@moduledoc """ | ||
This module is required by Guardian, to implements all type or configuration for | ||
the auth token. | ||
Also, is used to retrieve and deliver the authentication subject to Guardian. | ||
More details here: https://github.com/ueberauth/guardian#installation | ||
""" | ||
|
||
use Guardian, otp_app: :real_world | ||
|
||
alias RealWorld.{Repo, Accounts.User} | ||
|
||
def subject_for_token(%User{} = user, _claims), do: {:ok, to_string(user.id)} | ||
def subject_for_token(_, _), do: {:error, "Unknown resource type"} | ||
|
||
def resource_from_claims(%{"sub" => user_id}), do: {:ok, Repo.get(User, user_id)} | ||
def resource_from_claims(_claims), do: {:error, "Unknown resource type"} | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.