0
votes

I have a 3types of users (student, teacher and admin). Students will record their study, Teachers will supervise students and admins will administer service. So I made all users with a devise gem. Actually my purpose was that every user can login with facebook or twitter but I couldn't do that because omniauthable in devise supports only one devise model... how to deal with multiple devise model for omniauth like

<%= link_to 'student login with facebook', ........ %>
<%= link_to 'teacher login with facebook', ........ %>
2

2 Answers

0
votes

You can follow this Devise guide on how to get around setting up multiple models with Devise :omniauthable here.

If you can get around using multiple models you should consider using 1 User model with 3 roles (Student, Teacher, Admin). Check out the cancancan gem and that might fit in better what with you are looking to do.

0
votes

I solve the problem using omniauth for one model. For the other model I set the same omniauth_login button plus a param, like student: true, in my case for example:

<%= link_to user_google_oauth2_omniauth_authorize_path(doctor: true) do %>
    <i class="fab fa-google"></i>
    <div>Continua con Google</div>
<% end %>

Then on my omniauth callback controller

def google_oauth2
    set_user
    if @user.persisted?
        sign_in_and_redirect @user, event: :authentication
    else
        # Removing extra as it can overflow some session stores
        session['devise.google_data'] = request.env['omniauth.auth'].except(:extra)
        redirect_to new_user_registration_url, alert: 
        @user.errors.full_messages.join("\n")
    end
end

def set_user
    @user = if request.env['omniauth.params']['doctor']
                Doctor.from_omniauth(request.env['omniauth.auth'])
            else
                User.from_omniauth(request.env['omniauth.auth'])
            end
end