Created
March 18, 2017 16:43
-
-
Save akshatgoel/a5bae7fdb169bee3f72cde52d0019d8f to your computer and use it in GitHub Desktop.
JaResource from_attributes
This file contains hidden or 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 is responsible for configuring your application | |
# and its dependencies with the aid of the Mix.Config module. | |
# | |
# This configuration file is loaded before any dependency and | |
# is restricted to this project. | |
use Mix.Config | |
# General application configuration | |
config :starlit_server, | |
ecto_repos: [StarlitServer.Repo] | |
# Configures the endpoint | |
config :starlit_server, StarlitServer.Endpoint, | |
url: [host: "localhost"], | |
secret_key_base: "keJVG/4GiUY/4Vq8FX1BJP+c+D91a+BKIdvm+LVX3SDCjFSsW8TRPkqkRGisgDk0", | |
render_errors: [view: StarlitServer.ErrorView, accepts: ~w(html json)], | |
pubsub: [name: StarlitServer.PubSub, | |
adapter: Phoenix.PubSub.PG2] | |
# Configures Elixir's Logger | |
config :logger, :console, | |
format: "$time $metadata[$level] $message\n", | |
metadata: [:request_id] | |
# Import environment specific config. This must remain at the bottom | |
# of this file so it overrides the configuration defined above. | |
import_config "#{Mix.env}.exs" | |
config :guardian, Guardian, | |
issuer: "StarlitServer", | |
ttl: { 30, :days }, | |
allowed_drift: 2000, | |
verify_issuer: true, # optional | |
secret_key: System.get_env("GUARDIAN_SECRET_KEY"), | |
serializer: StarlitServer.GuardianSerializer | |
config :canary, repo: StarlitServer.Repo | |
config :canary, unauthorized_handler: {StarlitServer.AuthenticationHelpers, :handle_unauthorized} | |
config :canary, not_found_handler: {StarlitServer.AuthenticationHelpers, :handle_not_found} | |
config :ja_resource, | |
repo: StarlitServer.Repo |
This file contains hidden or 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
defmodule StarlitServer.Mixfile do | |
use Mix.Project | |
def project do | |
[app: :starlit_server, | |
version: "0.0.1", | |
elixir: "~> 1.2", | |
elixirc_paths: elixirc_paths(Mix.env), | |
compilers: [:phoenix, :gettext] ++ Mix.compilers, | |
build_embedded: Mix.env == :prod, | |
start_permanent: Mix.env == :prod, | |
aliases: aliases(), | |
deps: deps()] | |
end | |
# Configuration for the OTP application. | |
# | |
# Type `mix help compile.app` for more information. | |
def application do | |
[mod: {StarlitServer, []}, | |
applications: [:phoenix, :phoenix_pubsub, :phoenix_html, :cowboy, :logger, :gettext, | |
:phoenix_ecto, :postgrex, :ja_resource]] | |
end | |
# Specifies which paths to compile per environment. | |
defp elixirc_paths(:test), do: ["lib", "web", "test/support"] | |
defp elixirc_paths(_), do: ["lib", "web"] | |
# Specifies your project dependencies. | |
# | |
# Type `mix help deps` for examples and options. | |
defp deps do | |
[ | |
{:bamboo, "~> 0.7"}, # emails | |
{:bamboo_postmark, "~> 0.2.0"}, # postmark adapter for emails | |
{:phoenix, "~> 1.2.1"}, | |
{:phoenix_pubsub, "~> 1.0"}, | |
{:phoenix_ecto, "~> 3.0"}, | |
{:postgrex, ">= 0.0.0"}, | |
{:phoenix_html, "~> 2.6"}, | |
{:phoenix_live_reload, "~> 1.0", only: :dev}, | |
{:gettext, "~> 0.11"}, | |
{:cowboy, "~> 1.0"}, | |
{:canary, "~> 1.1"}, # Authorization | |
{:cloudex, "~> 0.1.10"}, | |
{:comeonin, "~> 2.0"}, | |
{:corsica, "~> 0.4"}, # CORS | |
{:guardian, "~> 0.14"}, # Authentication (JWT) | |
{:ja_resource, "~> 0.2"}, | |
{:ja_serializer, "~> 0.11.0"}, # JSON API | |
] | |
end | |
# Aliases are shortcuts or tasks specific to the current project. | |
# For example, to create, migrate and run the seeds file at once: | |
# | |
# $ mix ecto.setup | |
# | |
# See the documentation for `Mix` for more info on aliases. | |
defp aliases do | |
["ecto.setup": ["ecto.create", "ecto.migrate", "run priv/repo/seeds.exs"], | |
"ecto.reset": ["ecto.drop", "ecto.setup"], | |
"ecto.migrate": ["ecto.migrate", "ecto.dump"], | |
"test": ["ecto.create --quiet", "ecto.migrate", "test"]] | |
end | |
end |
This file contains hidden or 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
(exit) an exception was raised: | |
** (FunctionClauseError) no function clause matching in JaResource.Attributes.from_params/1 | |
(ja_resource) lib/ja_resource/attributes.ex:51: JaResource.Attributes.from_params(%{}) | |
(ja_resource) lib/ja_resource/create.ex:93: JaResource.Create.call/2 | |
(ja_resource) lib/ja_resource/plug.ex:61: JaResource.Plug.call/2 | |
(starlit_server) web/controllers/user_controller.ex:1: StarlitServer.UserController.phoenix_controller_pipeline/2 | |
(starlit_server) lib/starlit_server/endpoint.ex:1: StarlitServer.Endpoint.instrument/4 | |
(starlit_server) lib/phoenix/router.ex:261: StarlitServer.Router.dispatch/2 | |
(starlit_server) web/router.ex:1: StarlitServer.Router.do_call/2 | |
(starlit_server) lib/starlit_server/endpoint.ex:1: StarlitServer.Endpoint.phoenix_pipeline/1 | |
(starlit_server) lib/plug/debugger.ex:123: StarlitServer.Endpoint."call (overridable 3)"/2 | |
(starlit_server) lib/starlit_server/endpoint.ex:1: StarlitServer.Endpoint.call/2 | |
(plug) lib/plug/adapters/cowboy/handler.ex:15: Plug.Adapters.Cowboy.Handler.upgrade/4 | |
(cowboy) /Users/akshat.goel/projects/starlit_server/deps/cowboy/src/cowboy_protocol.erl:442: :cowboy_protocol.execute/4 |
This file contains hidden or 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
defmodule StarlitServer.UserController do | |
use StarlitServer.Web, :controller | |
use JaResource | |
import StarlitServer.Helpers.Query, only: [id_filter: 2, user_filter: 2, limit_filter: 2] | |
alias StarlitServer.User | |
alias StarlitServer.Services.UserService | |
alias StarlitServer.Mailer | |
plug :load_and_authorize_resource, model: User, only: [:update] | |
plug JaResource | |
plug :login, only: [:create] | |
def filter(_conn, query, "id", id_list) do | |
query |> id_filter(id_list) | |
end | |
def handle_index(_conn, params) do | |
User | |
|> user_filter(params) | |
|> limit_filter(params) | |
end | |
def handle_create(_conn, attributes) do | |
%User{} |> User.registration_changeset(attributes) | |
end | |
def handle_update(_conn, record, attributes) do | |
with {:ok, user, _, _} <- UserService.update(record, attributes) | |
do | |
{:ok, user} | |
else | |
{:error, changeset} -> {:error, changeset} | |
end | |
end | |
def email_available(conn, %{"email" => email}) do | |
hash = User.check_email_availability(email) | |
conn |> json(hash) | |
end | |
defp login(conn, _opts) do | |
Plug.Conn.register_before_send(conn, &do_login(&1)) | |
end | |
defp do_login(conn), do: Plug.Conn.assign(conn, :current_user, conn.assigns[:data]) | |
def email_verification(user = %User{} ) do | |
if ( user.email != nil ) do | |
claims = Guardian.Claims.app_claims | |
|> Map.put("email", user.email) | |
|> Guardian.Claims.ttl({1, :hours}) | |
{ :ok, jwt, full_claims } = Guardian.encode_and_sign(user, :email_verification, claims) | |
Mailer.send_verification_email( user.email, jwt ) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment