At Monterail we like to try new stuff. We try new languages and new (maybe not so new) frameworks.

Hanami (formerly Lotus) is a Ruby web framework created by Luca Guidi and community.

Probably the most problematic thing for people trying hanami after experience with rails is changing your mind away from the rails way. I can write many good things about hanami, but now I want to resolve one problem - authentication. The popular question is, What about users? In rails, we have devise which is a great gem but it is Rails only. In hanami, you can use other (non rails) user solutions. But sometimes you don’t need all this devise stuff and omniauth is enough (like in our internal apps).

Step 1 - create new project

I guess you have some application already, but I’ll create a new one.

hanami new hanami_oauth --test=rspec

For this tutorial we don’t need any db (File System is the default adapter, so everything will be saved in file).

Step 2 - add user

The generator will create all necessary files.

bundle exec hanami generate model user
create  lib/hanami_oauth/entities/user.rb
create  lib/hanami_oauth/repositories/user_repository.rb
create  spec/hanami_oauth/entities/user_spec.rb
create  spec/hanami_oauth/repositories/user_repository_spec.rb

Hanami uses hanami-model, but you can replace it with any other ORM. As we do not have any database we will not need migrations, but we must make some changes to our model.

Edit file: /lib/hanami_oauth.rb

Hanami::Model.configure do

...

  mapping do
    collection :users do
      entity     User
      repository UserRepository

      attribute :id,   Integer
      attribute :name, String
      attribute :github_id, String
      attribute :email, String
    end
  end
end

Next, edit file: lib/hanami_oath/entities/user.rb

class User
  include Hanami::Entity
  attributes :id, :name, :github_id, :email
end

#auth #hanami #ruby/rails #development

Hanami with OAuth
16.70 GEEK