I'm using devise to help with my authentication and for various reasons I have a couple subclasses of the User model that helps separate the concerns for registration and some other business logic.
The subclasses of User
are Affiliate
and Admin
(and I might have more in the future), all of which store all of the data in the User table (not separate tables).
All users can log in via the default users
resource (/users/sign_in
). However, this is where I'm having an issue.
If the user signs in via the /users/sign_in
resource I can access the user_signed_in?
and current_user
helper and then access any other Active Record associations. This is what I want! Woo hoo! This works great.
However, the same is not true of a user who signs up as an Affiliate
. Since devise automatically logs the user in (which I DO want) I expect that user_signed_in?
to equal true
and current_user
to be the user that just signed in. This is not the case when a user signs up via the /affiliate/sign_up
resource.
On the Affiliate
model (remember, it subclasses user like such class Affiliate < User
) I have generated (through devise) a separate set of controller/views for sign_up so I can customize this process a bit (as the sign_up process is a bit more customized for this particular type of user). When the user signs up via this resource /affiliate/sign_up
the affiliate is then signed up, but NOW, the current_user
is nil
and user_signed_in?
is false
. But, the helpers current_affiliate
is a hydrated object and affiliate_signed_in?
is true.
What I want to do is be able to access ONE type of helper - the user_signed_in?
helper and the current_user
helper not the child affiliate_signed_in?
and current_affiliate
. I'd like to access: current_user
and user_signed_in
only. Seeing that Affiliate
subclasses User
, why doesn't user_signed_in?
and current_user
returned the current user (aka: the Affiliate
)? Why do the current_affiliate
and affiliate_signed_in?
helpers work, but not the user
-esque ones not?
Is there a way to make the framework always use current_user
and user_signed_in?
helpers since everything is subclassing the User
model?
The way you set it up, it seems that you are able to log in as both a user and an affiliate at the same time. That means, that at a given moment there could be a current_user and a current_affiliate. If you want it to be current_user all the time, you can override the Devise helpers. But you have to make sure that a there can only be a user OR an affiliate logged in, through one session. Else Devise has no way of knowing which object to return for current_user.