-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merge develop to staging #133
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
eb5b6b0
Implement limit and scope together
aonomike 4de431d
Implement limit on trending causes
aonomike 8daa24b
Refactor to set up Causes
aonomike d569f7a
Refactor to simplify the assert
aonomike ac087c8
Refactor to remove case statement for better readability
aonomike 96138d3
Refactor to have query composed
aonomike 7a80160
Make failing test pass
aonomike 4024213
Fix causes ending soon to causes with end date less than 90 days from…
aonomike 413a55b
Refactor the naming of the conn object
aonomike aac93a3
Merge pull request #126 from aonomike/123_implement-limit-and-scope-v…
javpet 7daa836
Run mix gen to generate Accounts context
aonomike 0a4bc28
Start implementing signup schema
aonomike 3e28de4
Implemet user signup
aonomike 883be25
Add SetCurrentUser plug
aonomike d8a6c48
Fix failing tests
aonomike 5b1a73d
Fix linting issues
aonomike 100a595
Merge pull request #131 from aonomike/128_user_schema
javpet File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
defmodule SingForNeeds.Accounts do | ||
@moduledoc """ | ||
The Accounts context. | ||
""" | ||
|
||
import Ecto.Query, warn: false | ||
alias SingForNeeds.Repo | ||
|
||
alias SingForNeeds.Accounts.User | ||
|
||
@doc """ | ||
Returns the list of users. | ||
|
||
## Examples | ||
|
||
iex> list_users() | ||
[%User{}, ...] | ||
|
||
""" | ||
def list_users do | ||
Repo.all(User) | ||
end | ||
|
||
@doc """ | ||
Gets a single user. | ||
|
||
Raises `Ecto.NoResultsError` if the User does not exist. | ||
|
||
## Examples | ||
|
||
iex> get_user!(123) | ||
%User{} | ||
|
||
iex> get_user!(456) | ||
** (Ecto.NoResultsError) | ||
|
||
""" | ||
def get_user!(id), do: Repo.get!(User, id) | ||
|
||
@doc """ | ||
returns {:ok, user} | ||
""" | ||
def get_user(id), do: Repo.get!(User, id) | ||
|
||
@doc """ | ||
Creates a user. | ||
|
||
## Examples | ||
|
||
iex> create_user(%{field: value}) | ||
{:ok, %User{}} | ||
|
||
iex> create_user(%{field: bad_value}) | ||
{:error, %Ecto.Changeset{}} | ||
|
||
""" | ||
def create_user(attrs \\ %{}) do | ||
%User{} | ||
|> User.changeset(attrs) | ||
|> Repo.insert() | ||
end | ||
|
||
@doc """ | ||
Updates a user. | ||
|
||
## Examples | ||
|
||
iex> update_user(user, %{field: new_value}) | ||
{:ok, %User{}} | ||
|
||
iex> update_user(user, %{field: bad_value}) | ||
{:error, %Ecto.Changeset{}} | ||
|
||
""" | ||
def update_user(%User{} = user, attrs) do | ||
user | ||
|> User.changeset(attrs) | ||
|> Repo.update() | ||
end | ||
|
||
@doc """ | ||
Deletes a User. | ||
|
||
## Examples | ||
|
||
iex> delete_user(user) | ||
{:ok, %User{}} | ||
|
||
iex> delete_user(user) | ||
{:error, %Ecto.Changeset{}} | ||
|
||
""" | ||
def delete_user(%User{} = user) do | ||
Repo.delete(user) | ||
end | ||
|
||
@doc """ | ||
Returns an `%Ecto.Changeset{}` for tracking user changes. | ||
|
||
## Examples | ||
|
||
iex> change_user(user) | ||
%Ecto.Changeset{source: %User{}} | ||
|
||
""" | ||
def change_user(%User{} = user) do | ||
User.changeset(user, %{}) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
defmodule SingForNeeds.Accounts.User do | ||
@moduledoc """ | ||
User schema | ||
""" | ||
use Ecto.Schema | ||
import Ecto.Changeset | ||
|
||
schema "users" do | ||
field :avatar_url, :string | ||
field :email, :string | ||
field :password, :string, virtual: true | ||
field :password_hash, :string | ||
field :username, :string | ||
|
||
timestamps() | ||
end | ||
|
||
@doc false | ||
def changeset(user, attrs) do | ||
user | ||
|> cast(attrs, [:username, :email, :password, :password_hash, :avatar_url]) | ||
|> validate_required([:username, :email, :password]) | ||
|> validate_length(:username, min: 2) | ||
|> validate_length(:password, min: 6) | ||
|> unique_constraint(:username) | ||
|> unique_constraint(:email) | ||
|> hash_password() | ||
end | ||
|
||
defp hash_password(changeset) do | ||
case changeset do | ||
%Ecto.Changeset{valid?: true, changes: %{password: password}} -> | ||
put_change(changeset, :password_hash, Pbkdf2.hash_pwd_salt(password)) | ||
|
||
_ -> | ||
changeset | ||
end | ||
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
defmodule SingForNeedsWeb.AuthToken do | ||
@moduledoc """ | ||
Implements assignment of a token to a user requesting access and verification | ||
wheter the token provided by a user is valid | ||
""" | ||
@user_salt "user salt" | ||
@doc """ | ||
Encodes a given user id, signs it returning a token | ||
Clients can use as identification when using the API | ||
""" | ||
def sign(user) do | ||
Phoenix.Token.sign(SingForNeedsWeb.Endpoint, @user_salt, %{id: user.id}) | ||
end | ||
|
||
@doc """ | ||
Decodes original data from the given token, | ||
and verifies its integrity | ||
""" | ||
def verify(token) do | ||
Phoenix.Token.verify(SingForNeedsWeb.Endpoint, @user_salt, token, max_age: 365 * 24 * 3600) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
defmodule SingForNeedsWeb.Resolvers.Accounts do | ||
@moduledoc """ | ||
Resolvers for User | ||
""" | ||
alias SingForNeeds.Accounts | ||
|
||
def signup(_parent, args, _resolution) do | ||
case Accounts.create_user(args) do | ||
{:error, _changeset} -> | ||
{ | ||
:error, | ||
message: "Could not create account" | ||
# details: SingForNeedsWeb.Schema.ChangesetErrors.error_details(changeset) | ||
} | ||
|
||
{:ok, user} -> | ||
token = SingForNeedsWeb.AuthToken.sign(user) | ||
{:ok, %{token: token, user: user}} | ||
end | ||
end | ||
|
||
def me(_parent, _args, %{context: %{current_user: user}}) do | ||
{:ok, user} | ||
end | ||
|
||
def me(_parent, _args, _resolution) do | ||
{:ok, nil} | ||
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
defmodule SingForNeedsWeb.Schema.SessionTypes do | ||
@moduledoc """ | ||
All types for user | ||
""" | ||
use Absinthe.Schema.Notation | ||
|
||
object :session do | ||
field :token, non_null(:string) | ||
field :user, non_null(:user) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
defmodule SingForNeedsWeb.Schema.UserTypes do | ||
@moduledoc """ | ||
All types for user | ||
""" | ||
use Absinthe.Schema.Notation | ||
|
||
object :user do | ||
field :id, :id | ||
field :avatar_url, :string | ||
field :email, :string | ||
field :username, :string | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Adds an Absinthe execution context to the Phoenix connection. | ||
# If a valid auth token is in the request header, the corresponding | ||
# user is added to the context which is then available to all | ||
# resolvers. Otherwise, the context is empty. | ||
# | ||
# This plug runs prior to `Absinthe.Plug` in the `:api` router | ||
# so that the context is set up and `Absinthe.Plug` can extract | ||
# the context from the connection. | ||
|
||
defmodule SingForNeedsWeb.Plugs.SetCurrentUser do | ||
@moduledoc """ | ||
Plug to set the current user | ||
""" | ||
@behaviour Plug | ||
import Plug.Conn | ||
|
||
def init(opts), do: opts | ||
|
||
def call(conn, _) do | ||
context = build_context(conn) | ||
Absinthe.Plug.put_options(conn, context: context) | ||
end | ||
|
||
defp build_context(conn) do | ||
with ["Bearer " <> token] <- get_req_header(conn, "authorization"), | ||
{:ok, %{id: id}} <- SingForNeedsWeb.AuthToken.verify(token), | ||
%{} = user <- SingForNeeds.Accounts.get_user(id) do | ||
%{current_user: user} | ||
else | ||
_ -> %{} | ||
end | ||
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Most of the time you are using the multiple single line alias/require/import/use directives but here you are using the multi-alias/require/import/use syntax