# `OmniAuth::Identity` [![Version][👽versioni]][👽version] [![License: MIT][📄license-img]][📄license-ref] [![Downloads Rank][👽dl-ranki]][👽dl-rank] [![Open Source Helpers][👽oss-helpi]][👽oss-help] [![Depfu][🔑depfui♻️]][🔑depfu] [![CodeCov][🔑codecovi♻️]][🔑codecov] [![Test Coverage][🔑cc-covi♻️]][🔑cc-cov] [![Maintainability][🔑cc-mnti♻️]][🔑cc-mnt] [![CI Supported Build][🚎6-s-wfi]][🚎6-s-wf] [![CI Legacy Build][🚎4-lg-wfi]][🚎4-lg-wf] [![CI Unsupported Build][🚎7-us-wfi]][🚎7-us-wf] [![CI Heads Build][🚎3-hd-wfi]][🚎3-hd-wf] [![CI Ancient Build][🚎1-an-wfi]][🚎1-an-wf] [![CI Coverage Build][🚎2-cov-wfi]][🚎2-cov-wf] [![CI Style Build][🚎5-st-wfi]][🚎5-st-wf] [👽dl-rank]: https://rubygems.org/gems/omniauth-identity [👽dl-ranki]: https://img.shields.io/gem/rd/omniauth-identity.svg [👽oss-help]: https://www.codetriage.com/omniauth/omniauth-identity [👽oss-helpi]: https://www.codetriage.com/omniauth/omniauth-identity/badges/users.svg [👽version]: https://rubygems.org/gems/omniauth-identity [👽versioni]: https://img.shields.io/gem/v/omniauth-identity.svg [🔑cc-mnt]: https://codeclimate.com/github/omniauth/omniauth-identity/maintainability [🔑cc-mnti♻️]: https://api.codeclimate.com/v1/badges/621d6211cb2e0959ce00/maintainability [🔑cc-cov]: https://codeclimate.com/github/omniauth/omniauth-identity/test_coverage [🔑cc-covi♻️]: https://api.codeclimate.com/v1/badges/621d6211cb2e0959ce00/test_coverage [🔑codecov]: https://codecov.io/gh/omniauth/omniauth-identity [🔑codecovi♻️]: https://codecov.io/gh/omniauth/omniauth-identity/graph/badge.svg?token=cc6UdZCpAL [🔑depfu]: https://depfu.com/github/omniauth/omniauth-identity [🔑depfui♻️]: https://badges.depfu.com/badges/6c9b45362951b872127f9e46d39bed76/count.svg [🚎1-an-wf]: https://github.com/omniauth/omniauth-identity/actions/workflows/ancient.yml [🚎1-an-wfi]: https://github.com/omniauth/omniauth-identity/actions/workflows/ancient.yml/badge.svg [🚎2-cov-wf]: https://github.com/omniauth/omniauth-identity/actions/workflows/coverage.yml [🚎2-cov-wfi]: https://github.com/omniauth/omniauth-identity/actions/workflows/coverage.yml/badge.svg [🚎3-hd-wf]: https://github.com/omniauth/omniauth-identity/actions/workflows/heads.yml [🚎3-hd-wfi]: https://github.com/omniauth/omniauth-identity/actions/workflows/heads.yml/badge.svg [🚎4-lg-wf]: https://github.com/omniauth/omniauth-identity/actions/workflows/legacy.yml [🚎4-lg-wfi]: https://github.com/omniauth/omniauth-identity/actions/workflows/legacy.yml/badge.svg [🚎5-st-wf]: https://github.com/omniauth/omniauth-identity/actions/workflows/style.yml [🚎5-st-wfi]: https://github.com/omniauth/omniauth-identity/actions/workflows/style.yml/badge.svg [🚎6-s-wf]: https://github.com/omniauth/omniauth-identity/actions/workflows/supported.yml [🚎6-s-wfi]: https://github.com/omniauth/omniauth-identity/actions/workflows/supported.yml/badge.svg [🚎7-us-wf]: https://github.com/omniauth/omniauth-identity/actions/workflows/unsupported.yml [🚎7-us-wfi]: https://github.com/omniauth/omniauth-identity/actions/workflows/unsupported.yml/badge.svg ----- [![Liberapay Patrons][⛳liberapay-img]][⛳liberapay] [![Sponsor Me on Github][🖇sponsor-img]][🖇sponsor] [![Polar Shield][🖇polar-img]][🖇polar] [![Donate to my FLOSS or refugee efforts at ko-fi.com][🖇kofi-img]][🖇kofi] [![Donate to my FLOSS or refugee efforts using Patreon][🖇patreon-img]][🖇patreon] [⛳liberapay-img]: https://img.shields.io/liberapay/patrons/pboling.svg?logo=liberapay [⛳liberapay]: https://liberapay.com/pboling/donate [🖇sponsor-img]: https://img.shields.io/badge/Sponsor_Me!-pboling.svg?style=social&logo=github [🖇sponsor]: https://github.com/sponsors/pboling [🖇polar-img]: https://polar.sh/embed/seeks-funding-shield.svg?org=pboling [🖇polar]: https://polar.sh/pboling [🖇kofi-img]: https://img.shields.io/badge/buy%20me%20coffee-donate-yellow.svg [🖇kofi]: https://ko-fi.com/O5O86SNP4 [🖇patreon-img]: https://img.shields.io/badge/patreon-donate-yellow.svg [🖇patreon]: https://patreon.com/galtzo 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. | Primary Namespace | `OmniAuth::Identity` | |-------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | documentation | [on Github.com][homepage], [Latest release on RubyDoc.info][documentation], [HEAD on RubyDoc.info][documentation-head] | | expert support | [![Get help on Codementor](https://cdn.codementor.io/badges/get_help_github.svg)](https://www.codementor.io/peterboling?utm_source=github&utm_medium=button&utm_term=peterboling&utm_campaign=github) | | `...` 💖 | [![Follow Me on LinkedIn][🖇linkedin-img]][🖇linkedin] [![Find Me on WellFound:][✌️wellfound-img]][✌️wellfound] [![Find Me on CrunchBase][💲crunchbase-img]][💲crunchbase] [![My LinkTree][🌳linktree-img]][🌳linktree] [![Follow Me on Ruby.Social][🐘ruby-mast-img]][🐘ruby-mast] [![Tweet @ Peter][🐦tweet-img]][🐦tweet] [💻][coderme] [🌏][aboutme] | [🐦tweet-img]: https://img.shields.io/twitter/follow/galtzo.svg?style=social&label=Follow%20%40galtzo [🐦tweet]: http://twitter.com/galtzo [🚎blog]: http://www.railsbling.com/tags/omniauth-identity/ [🚎blog-img]: https://img.shields.io/badge/blog-railsbling-brightgreen.svg?style=flat [🖇linkedin]: http://www.linkedin.com/in/peterboling [🖇linkedin-img]: https://img.shields.io/badge/PeterBoling-blue?style=plastic&logo=linkedin [✌️wellfound]: https://angel.co/u/peter-boling [✌️wellfound-img]: https://img.shields.io/badge/peter--boling-orange?style=plastic&logo=wellfound [💲crunchbase]: https://www.crunchbase.com/person/peter-boling [💲crunchbase-img]: https://img.shields.io/badge/peter--boling-purple?style=plastic&logo=crunchbase [🐘ruby-mast]: https://ruby.social/@galtzo [🐘ruby-mast-img]: https://img.shields.io/mastodon/follow/109447111526622197?domain=https%3A%2F%2Fruby.social&style=plastic&logo=mastodon&label=Ruby%20%40galtzo [🌳linktree]: https://linktr.ee/galtzo [🌳linktree-img]: https://img.shields.io/badge/galtzo-purple?style=plastic&logo=linktree [aboutme]: https://about.me/peter.boling [coderme]: https://coderwall.com/Peter%20Boling ## Installation To acquire the latest release from RubyGems, and add this gem to your `Gemfile`, run: ```shell bundle add omniauth-identity ``` If the git repository has new commits not yet in an official release, simply specify the repo instead: ```ruby gem "omniauth-identity", git: "https://github.com/omniauth/omniauth-identity.git" ``` ## Compatibility This gem is compatible with a wide range of Ruby versions and Ruby ORMs, as of Nov 2024, version 3.1. * Latest released version of omniauth, v2+ * Tested in CI against Ruby 2.5, 2.6, 2.7, 3.0, 3.1, 3.2, 3.3, ruby-head, truffleruby-head * Probably also compatible with Ruby 2.4 and jruby-head * At least 5 different database ORM adapters, which connect to 15 different database clients! | Databases | Adapter Libraries | |-----------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------| | MySQL, PostgreSQL, SQLite3 | [ActiveRecord](https://guides.rubyonrails.org/active_record_basics.html) | | CouchDB | [CouchPotato](https://github.com/langalex/couch_potato) | | MongoDB | [Mongoid](https://github.com/mongodb/mongoid) | | RethinkDB | [NoBrainer](http://nobrainer.io/) | | ADO, Amalgalite, IBM_DB, JDBC, MySQL, Mysql2, ODBC, Oracle, PostgreSQL, SQLAnywhere, SQLite3, and TinyTDS | [Sequel](http://sequel.jeremyevans.net) | ## Usage This can be a bit hard to understand the first time. Luckily, Ryan Bates made a [Railscast](http://railscasts.com/episodes/304-omniauth-identity) 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: ```ruby use OmniAuth::Builder do provider :identity, #mandatory: tells OA that the Identity strategy is being used model: 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 table end ``` 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. ```ruby class Identity < 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 ``` ### Sequel [Sequel](http://sequel.jeremyevans.net/) is an alternative to ActiveRecord. Just include `OmniAuth::Identity::Models::Sequel` mixin, and specify whatever else you will need. ```ruby class SequelTestIdentity < 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. ```ruby class Identity include ::Mongoid::Document include ::OmniAuth::Identity::Models::Mongoid field :email, type: String field :name, type: String field :password_digest, type: String end ``` ### CouchPotato Include the `OmniAuth::Identity::Models::CouchPotatoModule` mixin and specify fields that you will need. ```ruby class Identity # NOTE: CouchPotato::Persistence must be included before OmniAuth::Identity::Models::CouchPotatoModule include ::CouchPotato::Persistence include ::OmniAuth::Identity::Models::CouchPotatoModule property :email property :password_digest def self.where(search_hash) CouchPotato.database.view(Identity.by_email(key: search_hash)) end view :by_email, key: :email end ``` ### NoBrainer [NoBrainer](http://nobrainer.io/) is an ORM for [RethinkDB](https://rethinkdb.com/). Include the `OmniAuth::Identity::Models::NoBrainer` mixin and specify fields that you will need. ```ruby class Identity include ::NoBrainer::Document include ::OmniAuth::Identity::Models::NoBrainer auth_key :email end ``` ### Ruby Object Mapper Would love to add a mixin for the [Ruby Object Mapper (ROM)](https://rom-rb.org/) if anyone wants to work on it! ## Custom Auth Model To use a class other than the default, specify the :model option to a different class. ```ruby use OmniAuth::Builder do provider :identity, fields: [:email], model: MyCustomClass end ``` 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. ```erb <%= form_tag '/auth/identity/register' do |f| %>