Skip to content

njwest/Phoenix-JWT-Auth-API

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Elixir on Phoenix 1.4 JSON Web Token Authentication API

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

API Endpoints:

Our API routes are configured in lib/myApi_web/router.ex.

By default, they are:

POST: Sign up a User

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: Sign in a User

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"}.

GET: User Data

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.

Learn more about Phoenix

Many thanks!

If you enjoyed this tutorial, consider buying me a coffee :)

My Bitcoin address: 1EoaCpygbLgmeNaKWuba1PMM1Nu9WzRpXm

About

A Phoenix 1.3 JSON Web Token Authentication API, built with Ueberauth Guardian

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages