Simple Auth
SimpleAuth is an authentication library to be used when everything else is just too complicated.
This library only handles session. You have to implement the authentication strategy as you want (e.g. in-site authentication, OAuth, etc).
Installation
Just the following line to your Gemfile:
gem "simple_auth"
Then run rails generate simple_auth:install
to copy the initializer file.
Usage
The initializer will install the required helper methods on your controller. So,
let's say you want to support user
and admin
authentication. You'll need to
specify the following scope.
# config/initializers/simple_auth.rb
SimpleAuth.setup do |config|
config.scopes = %i[user admin]
config.login_url = proc { login_path }
config.logged_url = proc { dashboard_path }
config.flash_message_key = :alert
config.install_helpers!
end
Session is valid only when Controller#authorized_#{scope}?
method returns
true
, which is the default behavior. You can override these methods with your
own rules; the following example shows how you can authorize all e-mails from
@example.com
to access the admin dashboard.
class Admin::DashboardController < ApplicationController
private
def authorized_admin?
current_user.email.match(/@example.com\z/)
end
end
So, how do you set up a new user session? That's really simple, actually.
class SessionsController < ApplicationController
def new
end
def create
@user = User.find_by_email(params[:email])
if @user.try(:authenticate, params[:password])
SimpleAuth::Session.create(scope: "user", session: session, record: @user)
redirect_to return_to(dashboard_path)
else
flash[:alert] = "Invalid username or password"
render :new
end
end
def destroy
reset_session
redirect_to root_path
end
end
First thing to notice is that SimpleAuth doesn't care about how you
authenticate. You could easily set up a different authentication strategy, e.g.
API tokens. The important part is assigning the record:
and scope:
options.
The return_to
helper will give you the requested url (before the user logged
in) or the default url.
SimpleAuth uses GlobalID as the session
identifier. This allows using any objects that respond to #to_gid
, including
namespaced models and POROs.
session[:user_id]
#=> gid://myapp/User/1
If you need to locate a record using such value, you can do it by calling
GlobalID::Locator.locate(session[:user_id])
Finally, only ActiveRecord::RecordNotFound
errors are trapped by SimpleAuth
(when ActiveRecord is available). If you locator raises a different exception,
add the error class to the list of known exceptions.
SimpleAuth::Session.record_not_found_exceptions << CustomNotFoundRecordError
Logging out users
Logging out a user is just as simple; all you have to do is calling the regular
reset_session
.
Restricting access
You can restrict access by using 2 macros. Use redirect_logged_#{scope}
to
avoid rendering a page for logged user.
class SignupController < ApplicationController
before_action :redirect_logged_user
end
Use require_logged_#{scope}
to enforce authenticated access.
class DashboardController < ApplicationController
before_action :require_logged_user
end
"So which helpers are defined?", you ask. Just three simple helpers.
#{scope}_logged_in? # e.g. user_logged_in? (available in controller & views)
current_#{scope} # e.g. current_user (available in controller & views)
#{scope}_session # e.g. user_session (available in controller & views)
From your routes file
You can also restrict routes directly from your routes:
Rails.application.routes.draw do
authenticate :admin, ->(user) { user.admin? } do
mount Sidekiq::Web, at: "sidekiq"
end
end
In this case, :admin
is the scope and the lambda will only be called whenever
there's a valid record associated with that record.
Translations
These are the translations you'll need:
---
en:
simple_auth:
user:
unlogged_in: "You need to be logged in"
unauthorized: "You don't have permission to access this page"
If you don't set these translations, a default message will be used.
To display the error message, use something like <%= flash[:alert] %>
. If you
want to use a custom key, say :error
, use the configuration file
config/initializers/simple_auth.rb
to define the new key:
# config/initializers/simple_auth.rb
SimpleAuth.setup do |config|
# ...
config.flash_message_key = :error
# ...
end
Maintainer
Contributors
Contributing
For more details about how to contribute, please read https://github.com/fnando/simple_auth/blob/main/CONTRIBUTING.md.
License
The gem is available as open source under the terms of the MIT License. A copy of the license can be found at https://github.com/fnando/simple_auth/blob/main/LICENSE.md.
Code of Conduct
Everyone interacting in the simple_auth project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.