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
// Use Gists to store code you would like to remember later on | |
console.log(window); // log the "window" object to the console |
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
mix phx.new —-no-webpack —-no-html myApi |
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
cd myApi | |
mix ecto.migrate |
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
config :myApi, MyApi.Repo, | |
adapter: Ecto.Adapters.Postgres, | |
username: "postgres", | |
password: "postgres", | |
database: "myapi_dev", | |
hostname: "localhost", | |
pool_size: 10 |
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
CREATE USER postgres WITH PASSWORD 'postgres' SUPERUSER; |
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
defp deps do | |
[ | |
# ...default dependencies omitted for brevity | |
{:comeonin, "~> 4.0"}, | |
{:bcrypt_elixir, "~> 1.0"}, | |
{:guardian, "~> 1.0"} | |
] | |
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
defmodule MyApi.Accounts.User do | |
use Ecto.Schema | |
import Ecto.Changeset | |
alias MyApi.Accounts.User | |
schema "users" do | |
field :email, :string | |
field :password_hash, :string |
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
schema "users" do | |
field :email, :string | |
field :password_hash, :string | |
# Virtual fields: | |
field :password, :string, virtual: true | |
field :password_confirmation, :string, virtual: true | |
timestamps() | |
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
def changeset(%User{} = user, attrs) do | |
user | |
|> cast(attrs, [:email, :password, :password_confirmation]) # Remove hash, add pw + pw confirmation | |
|> validate_required([:email, :password, :password_confirmation]) # Remove hash, add pw + pw confirmation | |
|> validate_format(:email, ~r/@/) # Check that email is valid | |
|> validate_length(:password, min: 8) # Check that password length is >= 8 | |
|> validate_confirmation(:password) # Check that password === password_confirmation | |
|> unique_constraint(:email) | |
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
defmodule MyApi.Accounts.User do | |
# code omitted | |
def changeset(%User{} = user, attrs) do | |
user | |
# Rest of pipe code omitted for brevity | |
|> unique_constraint(:email) | |
|> put_password_hash # Add put_password_hash to changeset pipeline | |
end |
OlderNewer