# OmniAuth Identity [![Build Status](https://img.shields.io/endpoint.svg?url=https%3A%2F%2Factions-badge.atrox.dev%2Fomniauth%2Fomniauth-identity%2Fbadge&style=flat)](https://actions-badge.atrox.dev/omniauth/omniauth-identity/goto) [![Maintainability](https://api.codeclimate.com/v1/badges/621d6211cb2e0959ce00/maintainability)](https://codeclimate.com/github/omniauth/omniauth-identity/maintainability) [![Test Coverage](https://api.codeclimate.com/v1/badges/621d6211cb2e0959ce00/test_coverage)](https://codeclimate.com/github/omniauth/omniauth-identity/test_coverage) [![License: MIT](https://img.shields.io/badge/License-MIT-green.svg)](https://opensource.org/licenses/MIT) [![Open Source Helpers](https://www.codetriage.com/omniauth/omniauth-identity/badges/users.svg)](https://www.codetriage.com/omniauth/omniauth-identity) [![Downloads Rank](https://img.shields.io/gem/rd/omniauth-identity.svg)](https://rubygems.org/gems/omniauth-identity) The OmniAuth Identity gem provides a way for applications to utilize a traditional login/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. ## Compatibility This gem is compatible with, as of Feb 2021, version 3: * Latest released version of omniauth, v2.0.2 * Ruby 2.4, 2.5, 2.6, 2.7, 3.0, ruby-head ## Installation To acquire the latest release from RubyGems add the following to your `Gemfile`: ```ruby gem '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/intridea/omniauth-identity.git' ``` ## 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. **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 ``` ### 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 ``` ### MongoMapper Unfortunately MongoMapper is **not supported** in `omniauth-identity` from >= v2.0 as a result of it not being maintained for several years. It wasn't possible to include Mongoid *and* MongoMapper due to incompatible gem version requirements. Therefore precedence was given to Mongoid as it is significantly more popular and actively maintained. ### DataMapper Include the `OmniAuth::Identity::Models::DataMapper` mixin and specify fields that you will need. ```ruby class Identity include DataMapper::Resource include OmniAuth::Identity::Models::DataMapper property :id, Serial property :email, String property :password_digest, Text attr_accessor :password_confirmation end ``` ### CouchPotato Include the `OmniAuth::Identity::Models::CouchPotatoModule` mixin and specify fields that you will need. ```ruby class Identity 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 ``` 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. Simple! ## 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| %>