[英]Devise + Omniauth multiple models
我在這里看到了一些解決方案,但沒有一個能解決我的問題。 我使用devise生成了兩個模型,即:User和Designer。 而且我需要使用Omniauth分別為此兩個模型進行注冊/登錄。 目前,這是我所擁有的:
User.rb
def self.from_omniauth(auth)
where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
user.provider = auth.provider
user.uid = auth.uid
user.user_name = auth.info.name
user.email = auth.info.email
user.password = "password"
user.skip_confirmation!
end
user_authentications_controller.rb
class UserAuthenticationsController < Devise::OmniauthCallbacksController
def create
begin
@user = User.from_omniauth(request.env['omniauth.auth'])
sign_in_and_redirect @user
#redirect_to root_url, notice: "Signed in!"
flash[:success] = "Welcome, #{@user.first_name}!"
UserMailer.welcome(@user).deliver_now
rescue
flash[:warning] = "There was an error while trying to authenticate you..."
end
end
end
designer.rb
def self.from_omniauth(auth)
where(provider: auth.provider, uid: auth.uid).first_or_create do |designer|
designer.provider = auth.provider
designer.uid = auth.uid
designer.user_name = auth.info.name
designer.email = auth.info.email
designer.password = "password"
designer.skip_confirmation!
end
end
designer_authentication_controller.rb
DesignerAuthenticationsController < Devise::OmniauthCallbacksController
def create
begin
@designer = Designer.from_omniauth(request.env['omniauth.auth'])
sign_in_and_redirect @designer
#redirect_to root_url, notice: "Signed in!"
flash[:success] = "Welcome, #{@designer.first_name}!"
UserMailer.welcome(@designer).deliver_now
rescue
flash[:warning] = "There was an error while trying to authenticate you..."
end
end
end
routes.rb
devise_scope :user do get "/auth/:provider/callback" => "user_authentications#create" end
devise_scope :designer do get "/auth/:provider/callback" => "designer_authentications#create" end
我的問題是:
1)無論我從哪個頁面注冊,設計者或用戶,它都將作為用戶注冊。 我了解這是因為它同時使用user_authentications_controller進行注冊。 有什么想法可以讓他們確定注冊頁面將調用哪個控制器嗎?
2)我使用的方式正確嗎,還是有更好的方法來注冊多個模型?
謝謝。 並請幫助!
讓我在這里分享我的經驗,我所做的和您目前所做的一樣。 問題可能是您沒有對兩個用戶都遵循所有步驟。
以下是要遵循的步驟的摘要-
1) rails generate devise MODEL (user and designer)
2) rails generate devise:controllers [scope] (users and designers)
3) rails generate devise:views USER (users and deigners)
此后,做一個耙路,看看設計如何為用戶和設計人員創建不同的路,並相應地調用它。
就我所知,最好使用角色而不是創建不同的用戶,因為稍后代碼庫會變得很混亂。 如果您剛剛開始構建角色,那么角色就是前進的方式。
為什么需要兩個模型? 您可以使用相同的身份驗證,然后通過角色來區分用戶或設計者。 我將看一些Railscasts視頻,以掌握最新的技術。
康康的角色: http: //railscasts.com/episodes/192-authorization-with-cancan
觀看此內容,以了解如何編寫自己的Auth: http : //railscasts.com/episodes/250-authentication-from-scratch-revised
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.