- Phoenix - Web Framework
- Ecto - ORM
- Absinthe - GraphQL
- Bamboo - Email handling
- ExMachina - Test factories
To start your Phoenix server:
- Install dependencies with
mix deps.get
- Create and migrate your database with
mix ecto.create && mix ecto.migrate
- Swap out secret keys by generating new ones using
mix guardian.gen.secret
- Start Phoenix endpoint with
mix phx.server
Now you can visit localhost:4000
from your browser.
By default Ash uses a hybrid authentication setup. User's aren't required to use a password if they don't want. It is up to the front-end to make the call on if it should request a token to be sent to the user or if they're trying to login with a email/password combination.
To request a token sent to a user's email we can use the emailLogin
mutation.
mutation {
emailLogin(email: "[email protected]") {
success
}
}
# {data: {
# emailLogin: { success: true }
# }}
In dev, emails can be intercepted at localhost:4000/outbox
or you can grab the token from the logged params. Once they recieve the token via email it can be authenticated with the login
mutation.
mutation {
login(token: "auth_token_from_the_email") {
token
user {
id
email
}
}
}
# ... login: {
# token: "jwt_to_be_sent_with_requests",
# user: {
# id: 1
# email: [email protected]
# }
# }} ...
Alternatively we can use the login
mutation to authenticate a email/password combination if they've setup a password.
mutation {
login(email: "[email protected]", password: "some_pw") {
token
user {
id
email
}
}
}
# ... login: {
# token: "jwt_to_be_sent_with_requests",
# user: {
# id: 1
# email: [email protected]
# }
# }} ...
In order to rip out the password auth you can make the following changes:
- Remove references to
password
,password_hash
andput_password_hash
fromlib/ash/accounts/user.ex
. Also killpassword_hash
inpriv/repo/migrations/XXXXXXXX_create_users.exs
- Remove
authenticate_password
andcheck_password
definitions fromlib/ash/accounts.ex
- Remove related
login
andencode_and_sign
definitions fromlib/ash_web/schema/auth_resolver.ex
comeonin
andargon2_elixir
can be removed frommix.exs
.
This is a little more invovled as there are entire schemas created to handle email auth. In order to rip out the passwordless auth you can make the following changes:
- Delete migrations and context files related to
auth_tokens
andauth_requests
. - Remove references to any
[action]_auth_token
and[action]_auth_token
functions inlib/ash/accounts.ex
. Also remove theprovide_token
,verify_token_value
,send_token
andcreate_token
function definitions. - Remove
has_many(:auth_tokens, AuthToken)
fromlib/ash/accounts/user.ex
. - Remove related
email_login
andlogin
definitions fromlib/ash_web/schema/auth_resolver.ex
- Remove
Bamboo
config from./config/[env].exs
files. - Delete Bamboo related templates located at
lib/ash_web/templates
, email view atlib/ash_web/views/email_view.ex
andlib/ash/mailer{.ex}
module files. bamboo
can be removed frommix.exs
.
Included in the boilerplate is a generator for GraphQL resources. It's usage is nearly identical to phx.gen.json
.
mix ash.gen.gql Context Model models [schema]
In addition to the typical schema and context files, this command will create the following files:
lib/ash_web/schema/model/model_resolver.ex
lib/ash_web/schema/model/model_types.ex
test/support/factories/model_factory.ex
test/ash_web/schema/model_resolver_test.exs
It will also prompt you to add the following lines to your schema.ex
import_types(AshWeb.Schema.ModelTypes)
query do
import_field(:model_queries)
end
mutation do
import_field(:model_mutations)
end