App by Nick West. Email: mail@nickwe.st
Companion repo for JWT Auth with an Elixir on Phoenix 1.4 API and React Native, Part I: Phoenix JWT API
This is a demo Phoenix Elixir JWT REST API app, using Ueberauth's Guardian package for JWT authentication and riverrun's Comeonin/bcrypt_elixir packages for password hashing.
Note: replace the Guardian config secret with your own, as the secrets used in this demo app are public.
To start your Phoenix server:
- Install dependencies with
mix deps.get
- Create and migrate your database with
mix ecto.create && mix ecto.migrate
- Start Phoenix server with
mix phx.server
Our API routes are configured in lib/myApi_web/router.ex
.
By default, they are:
POST
a JSON "user"
to /api/v1/sign_up
:
{
"user": {
"email": "foobar@email.com",
"password": "some_password",
"password": "some_password"
}
}
If the client sign up POST
request is successful, the server will return a JSON Web Token, as { "jwt": "some JSON web token here"}
POST
a valid email and password JSON to /api/v1/sign_in
:
{
"email": "foobar@email.com",
"password": "some_password"
}
If the client POST
sign in request is successful, the server will return a JSON Web Token, as { "jwt": "some JSON web token here"}
.
The subject of our JWT is its correlating user's user_id
.
With the request header "Authorization": "Bearer " + jwt
, a client can retrieve user data via a GET
request to /api/v1/my_user
.
- Guides: http://phoenixframework.org/docs/overview
- Docs: https://hexdocs.pm/phoenix
- Source: https://github.com/phoenixframework/phoenix
If you enjoyed this tutorial, consider buying me a coffee :)
My Bitcoin address: 1EoaCpygbLgmeNaKWuba1PMM1Nu9WzRpXm