Skip to content

Instantly share code, notes, and snippets.

use Mix.Config
config :sling, Sling.Repo,
username: "your_postgres_user",
password: "your_postgres_password"
# contents above
import_config "dev.secret.exs"
defmodule Sling.Repo.Migrations.CreateUser do
use Ecto.Migration
def change do
create table(:users) do
add :username, :string, null: false
add :email, :string, null: false
add :password_hash, :string, null: false
timestamps()
@bnhansn
bnhansn / user.ex
Last active October 20, 2016 20:03
defmodule Sling.User do
use Sling.Web, :model
schema "users" do
field :username, :string
field :email, :string
field :password_hash, :string
timestamps()
end
# content above
def application do
[mod: {Sling, []},
applications: [:phoenix, :phoenix_pubsub, :phoenix_html, :cowboy, :logger, :gettext,
:phoenix_ecto, :postgrex, :comeonin]] # :comeonin added here
end
# ...
defmodule Sling.User do
use Sling.Web, :model
schema "users" do
field :username, :string
field :email, :string
field :password_hash, :string
field :password, :string, virtual: true
timestamps()
# content above
config :guardian, Guardian,
issuer: "Sling",
ttl: {30, :days},
verify_issuer: true,
serializer: Sling.GuardianSerializer
import_config "#{Mix.env}.exs"
@bnhansn
bnhansn / dev.exs
Last active October 20, 2016 22:07
config :guardian, Guardian,
secret_key: "LG17BzmhBeq81Yyyn6vH7GVdrCkQpLktol2vdXlBzkRRHpYsZwluKMG9r6fnu90m"
config :guardian, Guardian,
secret_key: System.get_env("GUARDIAN_SECRET_KEY")
defmodule Sling.GuardianSerializer do
@behaviour Guardian.Serializer
alias Sling.Repo
alias Sling.User
def for_token(user = %User{}), do: {:ok, "User:#{user.id}"}
def for_token(_), do: {:error, "Unknown resource type"}
def from_token("User:" <> id), do: {:ok, Repo.get(User, String.to_integer(id))}