title: How can I set the authentications table when using Omniauth Identity?
body: <p>I am trying to add Omniauth Identity to my Rails application.  This is so that I can eventually support local and OAuth logins.</p>
  
  <p>I have been using the following Gist for information on how to get this working - <a href="https://gist.github.com/stefanobernardi/3769177" rel="nofollow noreferrer">https://gist.github.com/stefanobernardi/3769177</a>.</p>
  
  <p>The problem is that when a new user is registered in the Users table in the database, the <code>authentications</code> table is not being populated with the user_id or the provider.  This means that logins fail as the system is not able to use the <code>provider</code> to get the correct user from the database.</p>
  
  <p><code>models/authentication.rb</code></p>
  
  <pre><code>class Authentication &lt; ApplicationRecord
  
    attr_accessor :provider, :uid, :user_id
    belongs_to :user
  
  
    def self.find_with_omniauth(auth)
      find_by_provider_and_uid(auth['provider'], auth['uid'])
    end
  
    def self.create_with_omniauth(auth)
      create(uid: auth['uid'], provider: auth['provider']) # and other data you might want from the auth hash
    end
  
  end
  </code></pre>
  
  <p><code>controller/sessions_controller.rb</code></p>
  
  <pre><code>class SessionsController &lt; ApplicationController
  
    skip_before_action :verify_authenticity_token, only: :create
  
    def new
    end
  
  
    def create_old
      user = User.find_by(email: params[:session][:email].downcase)
      if user &amp;&amp; user.authenticate(params[:session][:password])
        log_in user
        params[:session][:remember_me] == '1' ? remember(user) : forget(user)
        redirect_back_or user
      else
        flash.now[:danger] = 'Invalid email/password combination'
        render 'new'
      end
    end
  
  
    def create
  
      auth = request.env['omniauth.auth']
  
      # Find an authentication or create one
      if @authentication.nil?
  
        # If no authentication was found, create new one here
        @authentication = Authentication.create_with_omniauth(auth)
  
      end
  
      if logged_in?
  
        if @authentication.user == current_user
  
          # User is signed in so they are trying to link an authentication with their
          # account.  But an authentication has been found and the user associated with it
          # is the current user.  So the authentication is already associated with the 
          # this user, therefore display an error
          redirect_to root_path, notice: "You have already linked this account"
        else
  
          # The authentication is not associated with the current_user to associate
          # the authentication
          @authentication.user = current_user
          @authentication.save
          redirect_to root_path, notice: "Account successfully authenticated"
        end
      else # no user is signed in
  
        if @authentication.user.present?
  
          # The authentication that has been found had a user associated with it so 
          # just lof them in 
          self.current_user = @authentication.user
          redirect_to root_path, notice: "Signed in"
        else
  
          # The authentication has no user assigned and there is not user signed in
          # The decision here is to create a new account for the user
          # Check for the Omniauth Idenitity provider which is local auth
          if @authentication.provider == 'identity'
  
            u = User.find(@authentication.uid)
  
            # if using the identity provior then the user is local so just find
            # in the database
          else
  
            # Create a new user in the databse using the hash of information from the 
            # omniauth
            u = User.create_with_omniauth(auth)
          end
  
          # Now can link the authentication with the user
          u.authentications &lt;&lt; @authentication
          self.current_user = u
          redirect_to root_path, notice: "Welcome"
        end
  
      end
    end
  
    def destroy
      log_out if logged_in?
      redirect_to root_url
    end
  
    def failure
      redirect_to root_path, alert: "Authentication failed, please try again"
    end
  end
  </code></pre>
  
  <p>The schema for the database is</p>
  
  <pre><code>ActiveRecord::Schema.define(version: 20170426182907) do
  
    create_table "authentications", force: :cascade do |t|
      t.integer  "user_id"
      t.string   "provider"
      t.string   "uid"
      t.datetime "created_at", null: false
      t.datetime "updated_at", null: false
      t.index ["user_id"], name: "index_authentications_on_user_id"
    end
  
    create_table "users", force: :cascade do |t|
      t.string   "name"
      t.string   "email"
      t.datetime "created_at",      null: false
      t.datetime "updated_at",      null: false
      t.string   "password_digest"
      t.string   "remember_digest"
      t.index ["email"], name: "index_users_on_email", unique: true
    end
  
  end
  </code></pre>
  
  <p>The user is created in the Users table without issue and the <code>authentication</code> object does container the provider information, it is just not making its way through to the authentication table.  A record is created, just not with the provider.</p>
  
  <p>Happy to provide more information, but do not want to make the post too long with (possibly) irrelevant information.  I am sure it is something very simple that I am missing and just not connecting things together properly.</p>