[英]Rails 4 Devise Omniauth with multiple providers tied to model associated with user
[英]how to deal with multiple devise model for omniauth in ruby on rails
我有 3 類用戶(學生、教師和管理員)。 學生將記錄他們的學習,教師將監督學生,管理員將管理服務。 所以我為所有用戶制作了一個設計寶石。 實際上,我的目的是讓每個用戶都可以使用 facebook 或 twitter 登錄,但我不能這樣做,因為設計中的 omniauthable 僅支持一種設計模型......如何處理 omniauth 的多個設計模型,例如
<%= link_to 'student login with facebook', ........ %>
<%= link_to 'teacher login with facebook', ........ %>
我使用 omniauth 解決了一個模型的問題。 對於另一個模型,我設置了相同的 omniauth_login 按鈕和一個參數,比如 student: true,在我的例子中:
<%= link_to user_google_oauth2_omniauth_authorize_path(doctor: true) do %>
<i class="fab fa-google"></i>
<div>Continua con Google</div>
<% end %>
然后在我的 omniauth 回調控制器上
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.