1
0
mirror of https://github.com/mgerb/classic-wow-forums synced 2026-01-12 01:52:49 +00:00
This commit is contained in:
2017-12-31 20:19:44 -06:00
commit e856cc5172
37 changed files with 1196 additions and 0 deletions

33
lib/myapp/auth/auth.ex Normal file
View File

@@ -0,0 +1,33 @@
defmodule MyApp.Guardian do
use Guardian, otp_app: :myapp
def subject_for_token(resource, _claims) do
# You can use any value for the subject of your token but
# it should be useful in retrieving the resource later, see
# how it being used on `resource_from_claims/1` function.
# A unique `id` is a good subject, a non-unique email address
# is a poor subject.
sub = to_string(resource["id"])
{:ok, sub}
end
# def subject_for_token(_, _) do
# {:error, :reason_for_error}
# end
def resource_from_claims(claims) do
# Here we'll look up our resource from the claims, the subject can be
# found in the `"sub"` key. In `above subject_for_token/2` we returned
# the resource id so here we'll rely on that to look it up.
id = claims["sub"]
# resource = MyApp.get_resource_by_id(id)
IO.inspect(claims)
resource = id
{:ok, resource}
end
# def resource_from_claims(_claims) do
# {:error, :reason_for_error}
# end
end

10
lib/myapp/auth/handler.ex Normal file
View File

@@ -0,0 +1,10 @@
defmodule MyApp.Auth.ErrorHandler do
import Plug.Conn
alias MyAppWeb.Response
def auth_error(conn, {type, _reason}, _opts) do
conn
|> put_status(401)
|> Response.json(to_string(type))
end
end

View File

@@ -0,0 +1,9 @@
defmodule MyApp.Guardian.AuthPipeline.JSON do
use Guardian.Plug.Pipeline, otp_app: :MyApp,
module: MyApp.Guardian,
error_handler: MyApp.Auth.ErrorHandler
plug Guardian.Plug.VerifyHeader, realm: "Bearer"
plug Guardian.Plug.EnsureAuthenticated
plug Guardian.Plug.LoadResource, allow_blank: true
end