Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9260577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:55:44+00:00 2026-06-18T12:55:44+00:00

I have a simple solution I’ve made myself with the following objects: Account (has

  • 0

I have a simple solution I’ve made myself with the following objects:

  • Account (has token field, that is returned when authenticating and used in API calls)
  • Authentication (has auth_type/auth_id and reference to Account)

I have a separate Authentication model to be able to connect several ways of login (device UUID, email/password, twitter, facebook etc). But it seems that in all examples of Devise you use it on the User (Account) model.

Isn’t that less flexible? For example OmniAuth module stores provider and id on the User model, what happens if you want to be able to login from both Twitter and Facebook, there is only room for one provider?

Should I use Devise on my Account model or the Authentication model?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T12:55:45+00:00Added an answer on June 18, 2026 at 12:55 pm

    Have been recently working on a project where I was using Devise to keep user’s tokens for different services. A bit different case, but still your question got me thinking for a while.

    I’d bind Devise to Account model anyway. Why? Let’s see.

    Since my email is the only thing that can identify me as a user (and you refer to Account as the User) I would place it in accounts table in pair with the password, so that I’m initially able do use basic email/password authentication. Also I’d keep API tokens in authentications.

    As you’ve mentioned, OmniAuth module needs to store provider and id. If you want your user to be able to be connected with different services at the same time (and for some reason you do) then obviously you need to keep both provider-id pairs somewhere, otherwise one will simply be overwritten each time a single user authenticates. That leads us to the Authentication model which is already suitable for that and has a reference to Account.

    So when looking for a provider-id pair you want to check authentications table and not accounts. If one is found, you simply return an account associated with it. If not then you check if account containing such email exists. Create new authentication if the answer is yes, otherwise create one and then create authentication for it.

    To be more specific:

    #callbacks_controller.rb
    controller Callbacks < Devise::OmniauthCallbacksContoller
      def omniauth_callback
        auth = request.env['omniauth.auth']
        authentication =  Authentication.where(provider: auth.prodiver, uid: auth.uid).first
        if authentication
          @account = authentication.account
        else
          @account = Account.where(email: auth.info.email).first
          if @account
            @account.authentication.create(provider: auth.provider, uid: auth.uid,
             token: auth.credentials[:token], secret: auth.credentials[:secret])
          else
            @account = Account.create(email: auth.info.email, password: Devise.friendly_token[0,20])
            @account.authentication.create(provider: auth.provider, uid: auth.uid,
             token: auth.credentials[:token], secret: auth.credentials[:secret])
          end
        end
        sign_in_and_redirect @account, :event => :authentication
      end
    end
    
    #authentication.rb
    class Authentication < ActiveRecord::Base
      attr_accessible :provider, :uid, :token, :secret, :account_id
      belongs_to :account
    end
    
    #account.rb
    class Account < ActiveRecord::Base
      devise :database_authenticatable
      attr_accessible :email, :password
      has_many :authentications
    end
    
    #routes.rb
    devise_for :accounts, controllers: { omniauth_callbacks: 'callbacks' }
    devise_scope :accounts do
      get 'auth/:provider/callback' => 'callbacks#omniauth_callback'
    end
    

    That should give you what you need while keeping the flexibility you want.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple task that I can't find a solution for, and I'm
I have a simple question, that I feel should have a simple solution. How
I have a simple question in RoR that I couldn't find a simple solution
I have a simple task that should have a simple solution, but I have
I have a simple question that I assume does not have a simple solution.
I have a question that will probably have a very simple solution. I'm running
I have a simple solution with the following projects as follows (base namespaces match
I have a simple problem which probably has a simple solution, but the solution
I feel this issue may have a simple solution that's just not obvious to
It looks like a problem which could have simple solution, but I haven't found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.