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
... | |
@derive {Inspect, except: [:password]} | |
schema "users" do | |
field :email, :string | |
field :password, :string, virtual: true | |
field :hashed_password, :string | |
field :confirmed_at, :naive_datetime | |
timestamps() |
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 deliver_user_confirmation_instructions(%User{} = user, confirmation_url_fun) | |
when is_function(confirmation_url_fun, 1) do | |
if user.confirmed_at do | |
{:error, :already_confirmed} | |
else | |
{encoded_token, user_token} = UserToken.build_email_token(user, "confirm") | |
Repo.insert!(user_token) | |
UserNotifier.deliver_confirmation_instructions(user, confirmation_url_fun.(encoded_token)) | |
end | |
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
... | |
schema "users_tokens" do | |
field :token, :binary | |
field :context, :string | |
field :sent_to, :string | |
belongs_to :user, Cam.Accounts.User | |
timestamps(updated_at: false) | |
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 get_user_by_email_and_password(email, password) | |
when is_binary(email) and is_binary(password) do | |
user = Repo.get_by(User, email: email) | |
if User.valid_password?(user, password), do: user | |
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 valid_password?(%Cam.Accounts.User{hashed_password: hashed_password}, password) | |
when is_binary(hashed_password) and byte_size(password) > 0 do | |
Bcrypt.verify_pass(password, hashed_password) | |
end | |
def valid_password?(_, _) do | |
Bcrypt.no_user_verify() | |
false | |
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 log_in_user(conn, user, params \\ %{}) do | |
token = Accounts.generate_user_session_token(user) | |
user_return_to = get_session(conn, :user_return_to) | |
conn | |
|> renew_session() | |
|> put_session(:user_token, token) | |
|> put_session(:live_socket_id, "users_sessions:#{Base.url_encode64(token)}") | |
|> maybe_write_remember_me_cookie(token, params) | |
|> redirect(to: user_return_to || signed_in_path(conn)) |
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 build_session_token(user) do | |
token = :crypto.strong_rand_bytes(@rand_size) | |
{token, %Cam.Accounts.UserToken{token: token, context: "session", user_id: user.id}} | |
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 generate_user_session_token(user) do | |
{token, user_token} = UserToken.build_session_token(user) | |
Repo.insert!(user_token) | |
token | |
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 require_authenticated_user(conn, _opts) do | |
if conn.assigns[:current_user] do | |
conn | |
else | |
conn | |
|> put_flash(:error, "You must log in to access this page.") | |
|> maybe_store_return_to() | |
|> redirect(to: Routes.user_session_path(conn, :new)) | |
|> halt() | |
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 fetch_current_user(conn, _opts) do | |
{user_token, conn} = ensure_user_token(conn) | |
user = user_token && Accounts.get_user_by_session_token(user_token) | |
assign(conn, :current_user, user) | |
end | |
defp ensure_user_token(conn) do | |
if user_token = get_session(conn, :user_token) do | |
{user_token, conn} | |
else |