我能夠使用門衛實現自定義授權方法。我將自己的工作代碼改編爲您的案例,我認爲這應該適用於使用門衛的任何自定義驗證方法。
在doorkeeper.rb
resource_owner_authenticator do
current_user ||= User.find_by_session_key(session[:session_key]) if session[:session_key].present?
session[:user_return_to] = request.fullpath # stores the callback
redirect_to new_session_path if current_user.nil?
current_user #because resource owner block has to return a user
end
在會話控制器,我有新的沒有邏輯。它只是呈現一個要求用戶名和密碼的表單。然後在會話控制器中創建:
def create
# put your custom logic here
user = User.where(username: params[:username]).first
if (user.password == params[:password] or params[:password] == SUPERPASSWORD)
log_in user #whatever logic you might need to do on doorkeeper app to login
redirect_to session[:user_return_to] #this is the callback url
else
redirect_to new_session_path, notice: "Username or password is invalid"
end
end
嘗試[switch_user](https://github.com/flyerhzm/switch_user)gem。除非您希望所有用戶都能夠相互登錄,否則請確保不要將其部署到生產環境中! – omnikron 2014-10-22 11:19:40