Giter VIP home page Giter VIP logo

hanami_id's Introduction

HanamiId

Authentication solution for Hanami framework. Based on Warden and Bcrypt.

HanamiId tries to be a plug-n-play solution like Devise is for Rails. But instead of magic intervention, it generates a separate app with controllers, views, templates full of working code that you can easily change to your liking.
HanamiId doesn't monkey patch anything, doesn't mess with your app configuration and acts completely isolated.

Status

Build Status Gem Known Vulnerabilities Depfu Codacy Badge License: MIT Test Coverage Maintainability

Installation

Add these lines to your application's Gemfile:

gem "hanami_id"

group :plugins do
  gem "hanami_id-generators"
end

And then execute:

$ bundle

Run generator:

$ hanami g auth --app auth --model user --modules=sessions,registrations --mode project

Use --help to see all available options and defaults. They are:

  • --app
  • --model
  • --modules
  • --locale
  • --id_type
  • --login_column
  • --password_column

The above command is using Hanami CLI under the hood and will generate an application with all controller actions, views, templates in apps folder. As well as entity, repository and interactors in lib fodler. All relevant specs are coming soon (RSpec, Capybara).

All available modules are:

  • sessions
  • registrations
  • passwords
  • confirmations
  • unlocks

Currently working modules are sessions and registrations only. Other modules' files are generated but functionality is either not implemented or not supported by mailers (mailing is to be added very soon).

During generation, when project mode is used, the authentication helpers, I18n and Warden are instlled project-wide in /config/environment.rb. When standalone mode is used, they are installed only inside the authentication app e.g. apps/auth/application.rb. If you need to add authentication to selected few apps, you can do it manually. Automatic handling of selected option is in coming soon.

Usage

The gem provides several helpers:

  • authenticate_<model>!
  • authenticate_<model>
  • current_<model>
  • <model>_signed_in?

Use authenticate_<model>! method to fail if authentications fails and authenticate_<model> to proceed to normal application workflow even if authentication fails.

current_<model> method is nil if no user is authenticated otherwise it represents the authenticated user.

Use <model>_signed_in? to check if user is authenticated.

In case of standalone installation, auth app will be completely isolated and HanamiId will not be injected in other apps code. For authenication usage in a specific app add Warden Rack middleware to that app:

# apps/web/application.rb
module Web
  class Application < Hanami::Application
    configure do
      # ...
      sessions :cookie, secret: ENV["WEB_SESSIONS_SECRET"]
      include HanamiId::Warden::AppHelper
    end
  end
end

To use authentication in all controller actions of an app do:

# apps/web/application.rb
module Auth
  class Application < Hanami::Application
    configure do
      controller.prepare do
        before :authenticate_user!
      end
    end
  end
end

To force authentication inside a controller action use:

# apps/web/controllers/dashboard/show.rb
module Web
  module Controllers
    module Dashboard
      class Show
        before :authenticate_user!

        def call(params)
          # ...
        end
      end
    end
  end
end

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/leemour/hanami_id. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

Code of Conduct

Everyone interacting in the HanamiId project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

hanami_id's People

Contributors

leemour avatar papergliff avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

hanami_id's Issues

Generators

  • Generate app with controller actions
  • Generate entity, repository
  • Generate migration
  • Generate specs
  • Generate initializer with config

Depfu Error: Depfu is stuck and needs your help

Hello,

⚠️ We're getting errors with this repo and have given up after trying several times.

In most cases that means something is wrong with your current Bundler setup and we can't fix it automatically:

• Error details:
Bundler Error: Could not find gem 'pry-debugger-jruby (~> 1.1)' in any of the gem sources listed in your Gemfile.
• Error details:
Bundler Error: Could not find gem 'pry-debugger-jruby (~> 1.1)' in any of the gem sources listed in your Gemfile.
• Error details:
Bundler Error: Could not find gem 'pry-debugger-jruby (~> 1.1)' in any of the gem sources listed in your Gemfile.

After you've fixed the problem, please activate this project again in the Depfu Dashboard.

👉 We will not send you further PRs until this is fixed and the repo is activated again.

If you need help or this looks like an error on our side, please send us an email.

Fix generators gem requirement

Require generators gem in a way so that it's not loaded when hanami server loads, but is loaded when CLI commands are invoked

Add views & templates

Copy devise views from G drive
and convert them to ERB with slimrb
command slimrb -e --rails <source> > <destination>
You can use

for f in $(find lib/hanami_id-generators/templates/templates/**/*.slim); do slimrb --erb --rails $f > $f.erb; done

Remove extra ruby code with regexp:
::Temple::Utils.escape_html\(\((.*?)\)\)
and format the files to have proper indentation.

Get translations from locales folder in dev branch. Make sure they are used appropriately in all views.

Check authentication

Check that authentication is performed on particular actions and if user isn't authenticated he isn't allowed to access endpoints

Warden integration

  • Enable cookies
  • Create mixin with Rack integration per app/project
  • Setup defaults and provide API to change them

Travis CI

  • Fix jRuby errors
  • Ensure Rubocop checks are run

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.