The OmniAuth Identity gem provides a way for applications to utilize a
traditional username/password based authentication system without the need
to give up the simple authentication flow provided by OmniAuth. Identity
is designed on purpose to be as featureless as possible: it provides the
basic construct for user management and then gets out of the way.
This can be a bit hard to understand the first time. Luckily, Ryan Bates made
a Railscast about it!
You use omniauth-identity just like you would any other OmniAuth provider: as a
Rack middleware. In rails, this would be created by an initializer, such as
config/initializers/omniauth.rb. The basic setup for a email/password authentication would look something like this:
use OmniAuth::Builderdo
provider :identity, #mandatory: tells OA that the Identity strategy is being usedmodel:Identity, # optional: specifies the name of the "Identity" model. Defaults to "Identity"fields: %i[email custom1 custom2] # optional: list of custom fields that are in the model's tableend
Next, you need to create a model (called Identity by default, or specified
with :model argument above) that will be able to persist the information
provided by the user. Luckily for you, there are pre-built models for popular
ORMs that make this dead simple.
Once you've got an Identity persistence model and the strategy up and
running, you can point users to /auth/identity and it will request
that they log in or give them the opportunity to sign up for an account.
Once they have authenticated with their identity, OmniAuth will call
through to /auth/identity/callback with the same kinds of information
it would had the user authenticated through an external provider.
Note: OmniAuth Identity is different from many other user authentication
systems in that it is not built to store authentication information in your primary
User model. Instead, the Identity model should be associated with your
User model giving you maximum flexibility to include other authentication
strategies such as Facebook, Twitter, etc.
ActiveRecord
Just subclass OmniAuth::Identity::Models::ActiveRecord and provide fields
in the database for all of the fields you are using.
classIdentity < OmniAuth::Identity::Models::ActiveRecord
auth_key :email# optional: specifies the field within the model that will be used during the login process# defaults to email, but may be username, uid, login, etc.# Anything else you want!end
Just include OmniAuth::Identity::Models::Sequel mixin, and specify
whatever else you will need.
classSequelTestIdentity < Sequel::Model(:identities)
include::OmniAuth::Identity::Models::Sequel
auth_key :email# whatever else you want!end
Mongoid
Include the OmniAuth::Identity::Models::Mongoid mixin and specify
fields that you will need.
classIdentityinclude::Mongoid::Documentinclude::OmniAuth::Identity::Models::Mongoid
field :email, type:String
field :name, type:String
field :password_digest, type:Stringend
CouchPotato
Include the OmniAuth::Identity::Models::CouchPotatoModule mixin and specify
fields that you will need.
classIdentity# NOTE: CouchPotato::Persistence must be included before OmniAuth::Identity::Models::CouchPotatoModuleinclude::CouchPotato::Persistenceinclude::OmniAuth::Identity::Models::CouchPotatoModule
property :email
property :password_digestdefself.where(search_hash)
CouchPotato.database.view(Identity.by_email(key: search_hash))
end
view :by_email, key::emailend
To use a class other than the default, specify the :model option to a
different class.
use OmniAuth::Builderdo
provider :identity, fields: [:email], model:MyCustomClassend
NOTE: In the above example, MyCustomClass must have a class method called auth_key that returns
the default (email) or custom auth_key to use.
Customizing Registration Failure
To use your own custom registration form, create a form that POSTs to
/auth/identity/register with password, password_confirmation, and your
other fields.
<%= form_tag '/auth/identity/register' do |f| %>
<h1>Create an Account</h1>
<%= text_field_tag :email %>
<%= password_field_tag :password %>
<%= password_field_tag :password_confirmation %>
<%= submit_tag %>
<% end %>
Beware not to nest your form parameters within a namespace. This strategy
looks for the form parameters at the top level of the post params. If you are
using simple_form, then you
can avoid the params nesting by specifying :input_html.
Next you'll need to let OmniAuth know what action to call when a registration
fails. In your OmniAuth configuration, specify any valid rack endpoint in the
:on_failed_registration option.
use OmniAuth::Builderdo
provider :identity,
fields: [:email],
on_failed_registration:UsersController.action(:new)
end
You can customize the way that matching records are found when authenticating.
For example, for a site with multiple domains, you may wish to scope the search
within a particular subdomain. To do so, add :locate_conditions to your config.
The default value is:
locate_conditions takes a Proc object, and must return a Hash object, which will be used
as the argument to the locate method for your ORM. The proc is evaluated in the
callback context, and has access to your Identity model (using model) and receives the request
object as a parameter. Note that model.auth_key defaults to email, but is also configurable.
Note: Be careful when customizing locate_conditions. The best way to modify the conditions is
to copy the default value, and then add to the hash. Removing the default condition will almost
always break things!
Customizing Other Things
From the code - here are the options we have for you, a couple of which are documented above, and the rest are documented... in the specs we hope!?
option :fields, %i[name email]
# Primary Feature Switches:
option :enable_registration, true # See #other_phase and #request_phase
option :enable_login, true # See #other_phase
# Customization Options:
option :on_login, nil # See #request_phase
option :on_validation, nil # See #registration_phase
option :on_registration, nil # See #registration_phase
option :on_failed_registration, nil # See #registration_phase
option :locate_conditions, ->(req) { {model.auth_key => req.params["auth_key"]} }
Please contribute some documentation if you have the gumption! The maintainer's time is limited, and sometimes the authors of PRs with new options don't update the this readme. 😭
🤝 Contributing
If you need some ideas of where to help, you could work on adding more code coverage,
or if it is already 💯 (see below) then check issues, or PRs,
or use the gem and think about how it could be better.
Everyone interacting in this project's codebases, issue trackers,
chat rooms and mailing lists is expected to follow the code of conduct.
📌 Versioning
This Library adheres to Semantic Versioning 2.0.0.
Violations of this scheme should be reported as bugs.
Specifically, if a minor or patch version is released that breaks backward compatibility,
a new version should be immediately released that restores compatibility.
Breaking changes to the public API will only be introduced with new major versions.
To get a better understanding of how SemVer is intended to work over a project's lifetime,
read this article from the creator of SemVer:
As a result of this policy, you can (and should) specify a dependency on these libraries using
the Pessimistic Version Constraint with two digits of precision.
Copyright (c) 2021, 2024 Peter H. Boling, and OmniAuth-Identity Maintainers
Copyright (c) 2020 Peter H. Boling, Andrew Roberts, and Jellybooks Ltd.
Copyright (c) 2010-2015 Michael Bleigh, and Intridea, Inc.
🤑 One more thing
You made it to the bottom of the page,
so perhaps you'll indulge me for another 20 seconds.
I maintain many dozens of gems, including this one,
because I want Ruby to be a great place for people to solve problems, big and small.
Please consider supporting my efforts via the giant yellow link below,
or one of the others at the head of this README.
FAQs
Unknown package
We found that omniauth-identity demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago.It has 3 open source maintainers collaborating on the project.
Package last updated on 18 Nov 2024
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Bybit's $1.46B hack by North Korea's Lazarus Group pushes 2025 crypto losses to $1.6B in just two months, already surpassing all of 2024's $1.49B total.
OpenSSF has published OSPS Baseline, an initiative designed to establish a minimum set of security-related best practices for open source software projects.
Michigan TypeScript founder Dimitri Mitropoulos implements WebAssembly runtime in TypeScript types, enabling Doom to run after processing 177 terabytes of type definitions.