No commit activity in last 3 years
No release in over 3 years
A simple authentication module for hanami.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.15
~> 10.0
~> 3.0

Runtime

~> 3.1
 Project Readme

Hanami::Authentication

Gem Version

Hanami::Authentication is simple authentication helpers for hanami-controller

Installation

Add this line to your application's Gemfile:

gem 'hanami-authentication'

Usage

Setup

# application.rb
controller.prepare do
  include Hanami::Authentication

  before authenticate  # This will use callbacks if authentication is failed.  
  before authenticate! # This will force to halt by 401 if authentication is failed.
  
  after_session_expired do # This will be called if authenticate method is called when session is expired.
    flash[:error] = 'The session is expired.'
    redirect_to routes.root_path
  end

  after_authentication_failed do # This will be called if authenticate method is called when user has not logged in.
    flash[:error] = 'Please login'
    redirect_to routes.root_path
  end
end

Methods

Login example

# in_your_login_action.rb
def call(params)
  email = params[:email]
  password = params[:password]
  remember_me = params[:remember_me]

  @current_user = @repository.find_by_email(email)

  if match_password?(@current_user, password)
    login(@current_user, remember_me: remember_me)
    flash[:success] = 'Successful logged in'
    redirect_to routes.root_path
  else
    flash[:error] = 'Email or password is incorrect'
    redirect_to routes.root_path
  end
end

Logout example

# in_your_login_action.rb
def call(params)
  logout
end

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/legalforce/hanami-auth. 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.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

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