Skip to content

Instantly share code, notes, and snippets.

View njwest's full-sized avatar
🎹
Groovin

Nick West 韋羲 njwest

🎹
Groovin
View GitHub Profile
@njwest
njwest / 0_reuse_code.js
Created June 7, 2016 22:33
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
@njwest
njwest / gist:0a53900a8ea88fd6af588aac62c0b7ae
Last active November 11, 2018 06:11
Generate a Phoenix API Project
mix phx.new —-no-webpack —-no-html myApi
@njwest
njwest / gist:5055929a995f98b0481505de4d6e771d
Created February 14, 2018 09:20
Create your Phoenix App's Database
cd myApi
mix ecto.migrate
@njwest
njwest / dev.exs
Last active February 24, 2018 06:57
config/dev.exs
config :myApi, MyApi.Repo,
adapter: Ecto.Adapters.Postgres,
username: "postgres",
password: "postgres",
database: "myapi_dev",
hostname: "localhost",
pool_size: 10
@njwest
njwest / create_superuser_postgres.sql
Last active July 21, 2018 06:45
SQL command for creating PostgreSQL Superuser
CREATE USER postgres WITH PASSWORD 'postgres' SUPERUSER;
@njwest
njwest / mix.exs
Last active February 24, 2018 06:58
AppRoot/mix.exs
defp deps do
[
# ...default dependencies omitted for brevity
{:comeonin, "~> 4.0"},
{:bcrypt_elixir, "~> 1.0"},
{:guardian, "~> 1.0"}
]
end
@njwest
njwest / user.ex
Created February 24, 2018 04:29
lib/myApi/accounts/user.ex
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
@njwest
njwest / user.ex
Created February 24, 2018 04:34
Add virtual fields
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
@njwest
njwest / user.ex
Last active February 24, 2018 04:48
lib/myApi/accounts/user.ex
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
@njwest
njwest / user.ex
Last active February 28, 2018 08:24
lib/myApi/accounts/user.ex
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