The latest version of Openmaize depends on Elixir 1.2.
Example of using Openmaize authentication library in a Phoenix web application.
These are the step-by-step instructions to how to run this example for the first time:
-
Adapt the configuration files in
config/
and the filelib/welcome/mailer.ex
to reflect your local environment. Hints:- set the correct PostgreSQL host, username and password
in
config/dev.exs
and inconfig/test.exs
)(see note 1) - customize urls and the
from
mail address inlib/welcome/mailer.ex
file, and take note of (or modify) thetest_file_path
path (that's where you can read the mail sent to the user when testing) - add the mailgun configuration to
config/config.exs
(see note 2)
- set the correct PostgreSQL host, username and password
in
-
(Optional step) Add/modify some sample users in
priv/repo/seeds.exs
(At least one user with roleadmin
is needed to be able to try all the application functionalities) -
Run
mix do deps.get, compile
(If you get the error** (Mix) Registry checksum mismatch against lock (openmaize x.x.x)
please remove the filemix.lock
and try again); -
Run
npm install
-
Run
mix ecto.create
-
Run
mix ecto.migrate
-
Run
mix run priv/repo/seeds.exs
-
Run
mix phoenix.server
-
Open the url
http://localhost:4000
on your preferred browser -
Referring to the
priv/repo/seeds.exs
file, try to login with different identities. (Note: for users registered with anadmin
role, a Verification Code is required to complete the login) (see note 3)
Notes:
-
I.e. you can either:
- Change the database username from
dev
topostgres
- Add a new role
dev
to your PostgreSQL this way:- Launch
psql
from your command line - Type
\du
to display the current users - Type
CREATE ROLE dev WITH LOGIN CREATEDB SUPERUSER;
- Launch
- Change the database username from
-
Go to
http://mailgun.com
, open a demo account and obtain your domain and key, then add and customize these three lines to theconfig/config.exs
file:
# Configures MailGun
config :welcome, mailgun_domain: "https://api.mailgun.net/v3/sandbox<your_mailgun_domain_code>.mailgun.org",`
mailgun_key: "key-<your_mailgun_key_code>"
- Before to try the admin login, open a new terminal session, then launch the Interactive Elixir
by typing
iex -S mix
on the command line, then typeWelcome.Repo.get_by(Welcome.User, username: "<username>").otp_secret |> Comeonin.Otp.gen_totp
but without executing the command by pressing theReturn
key. (substitute"<username>"
with the actual username for the admin user, i.e."tom"
for theTom
user in the default seed file) then:- Navigate to
http://localhost:4000/login
- Enter username and password for the preferred admin user
(i.e.
tom
,mangoes
) and press the submit button - You will see the
Verification code
request - Go to the
iex
session and press theReturn
key at the end of the previously typed command line to generate the tOTP token - You will get a six digits number
- Submit the token into the "Verification code" login form before the token expires.
- Navigate to
Happy testing!
Following there is the content of the Openmaize README.md file (you can safely ignore to read this to be able to run the example):
The following instructions show the most straightforward of getting started with Openmaize.
- Add openmaize to your
mix.exs
dependencies
defp deps do
[{:openmaize, "~> 1.0.0-beta"}]
end
- List
:openmaize
as an application dependency
def application do
[applications: [:logger, :openmaize]]
end
-
Run
mix do deps.get, compile
-
Run
mix openmaize.gen.ectodb
-
Run
mix openmaize.gen.phoenixauth
You then need to configure Openmaize. For more information, see the documentation for the Openmaize.Config module.
Follow the above instructions for generating database and authorization modules, and then add the following lines to the config file:
config :openmaize,
hash_name: :encrypted_password
Some of the functions in the Authorize module depend on a role
being
set for each user. If you are not using roles, you will need to edit
these functions before use.
This example uses a password strength checker which is an optional dependency of
Openmaize. If you don't want to use this, just remove the line {:not_qwerty123, "~> 1.0"}
from the deps in the mix.exs file.
MIT.