Ruby on Railsの新機能で、RubyとRuby on Railsのスタイルについてはあまり知られていません。私はオムニバスの許可を得て始めました。私は外部のサービスアカウントでのみサインインしたいです。次のステップは、複数のアカウントで承認されます。Ruby on Railsの方法
しかし、今私は/ auth/twitterに行くときに新しいユーザーを作成したり、古いユーザーを戻したりするちょっとしたコードを持っています。もっとルビーにする方法を教えてもらえますか?あなたが行うことができます
user.rb
class User < ActiveRecord::Base
has_many :authentications
def self.from_omniauth(auth)
authentication = Authentication.find_by_provider_and_uid(auth["provider"], auth["uid"])
authentication.nil? ? create_with_omniauth(auth) : authentication.user
end
def self.create_with_omniauth(auth)
@user = User.new
@user.name = auth["info"]["name"]
@user.save
@user.authentications.create_with_omniauth(auth, @user.id)
@user
end
end
authentication.rb
class Authentication < ActiveRecord::Base
belongs_to :user
def self.create_with_omniauth(auth, user_id)
@authentication = Authentication.new
@authentication.user_id = user_id
@authentication.provider = auth[:provider]
@authentication.uid = auth[:uid]
@authentication.save
end
end
authentication_controller.rb
class AuthenticationsController < ApplicationController
def index
@authentications = Authentication.all
end
def create
user = User.from_omniauth(request.env['omniauth.auth'])
session[:user_id] = user.id
redirect_to root_url, notice: "signed in"
end
def destroy
@authentication = Authentication.find(params[:id])
@authentication.destroy
redirect_to authentications_url, :notice => "Successfully destroyed authentication."
end
end
認証ライブラリには、標準の電子メールパスワード認証などの不要な機能が多すぎます。私はちょうど簡単な許可は、外部サービスを投げるだけで、私はそれも新人のための複雑なタスクだとは思わない – Donotello
間違いなく、あなたがそれらについて知っていたかどうかは分かりません。 – Init