Packages
guard
0.10.0
0.19.1
0.19.0
0.18.0
0.17.2
0.17.1
0.17.0
0.16.3
0.16.2
0.16.1
0.16.0
0.15.3
0.15.2
0.15.1
0.15.0
0.14.9
0.14.8
0.14.7
0.14.6
0.14.5
0.14.4
0.14.3
0.14.2
0.14.1
0.14.0
0.13.1
0.13.0
0.12.9
0.12.8
0.12.7
0.12.6
0.12.5
0.12.4
0.12.3
0.12.2
0.12.1
0.12.0
0.11.3
0.11.2
0.11.1
0.11.0
0.10.4
0.10.3
0.10.2
0.10.1
0.10.0
0.9.0
0.8.0
0.7.1
0.7.0
0.6.1
0.6.0
0.5.0
0.4.1
0.4.0
0.3.0
0.2.0
0.1.0
Useful package for dealing with user authentication and signup
Current section
Files
Jump to
Current section
Files
lib/guard/controller/active_session.ex
defmodule Guard.Controller.ActiveSession do
use Phoenix.Controller
alias Guard.{Controller, Authenticator}
require Logger
import Guard.Controller, only: [send_error: 3]
plug Guardian.Plug.EnsureAuthenticated, claims: %{"typ" => "access"}
def call(conn, opts) do
try do
super(conn, opts)
rescue
error -> send_error(conn, error, :internal_server_error)
end
end
defp generate_response(resp, conn) do
case resp do
{ :ok, claims } ->
perms = Guard.Guardian.decode_permissions_from_claims(claims)
user = Guardian.Plug.current_resource(conn)
root_user = claims["usr"]
extra = if root_user do
%{root_user: root_user}
else
%{}
end
conn
|> put_status(:ok)
|> json(Map.merge(%{jwt: Guardian.Plug.current_token(conn), perms: perms, user: user}, extra))
{ :error, _reason } ->
conn
|> put_status(:not_found)
|> Controller.send_error(:not_found)
end
end
def show(conn, _) do
Authenticator.current_claims(conn) |> generate_response(conn)
end
end