This is a sample in order to provide an example of a JSON API server built with Hanami Framework using JWT based authentication.
There is a tutorial for implementation of this project. Please Visit here(Korean)
Main features:
- Implement a token based authentication using JWT Gem
- Store encrypted password in the database with BCrypt
- Return a JSON representation of resources
Development environment
- Ruby v2.4.1
- Hanami v1.0.0
- Postgresql v9.6.2
You need at least Ruby v2.3+, and bundler
. If you have not installed Hanami, Please install it with this command first.
gem install hanami
You need postgresql installed as well, or change Gemfile
in order to use SQLite3 instead.
# Gemfile
# ...
gem 'pg' # => gem 'sqlite3'
# ...
git clone git@github.com:emaren84/moneybook_api.git
cd moneybook_api
bundle install
then you should create .env.*
files for development and test environment. and set DATABASE_URL
, JWT_ISSUER
, JWT_SECRET
.
# .env.development / .env.test
DATABASE_URL="postgresql://username:password@localhost/moneybook_api_development" # or moneybook_api_test
JWT_ISSUER="localhost" # or Your domain name
JWT_SECRET="your secret" # Strongly recommend SHA-256 encrypted hash
DATABASE_URL
have to be changed that if you want to use SQLite3 as a databse engine.
DATABASE_URL="sqlite://db/moneybook_api_development"
bundle exec hanami db prepare
bundle exec hanami db migrate
HANAMI_ENV=test bundle exec hanami db prepare
HANAMI_ENV=test bundle exec hanami db migrate
bundle exec rake test
Tests aren't elegant nor covering whole application yet. It needs to be improved.
bundle exec hanami server
By deafult Hanami development server is launched at http://localhost:2300