omniauth-google-oauth2을 사용하여 인증 된 경로 /me
을 만들려고합니다. 지금까지 라이브러리를 로그인 및 로그 아웃하도록 설정 했으므로 정상적으로 작동합니다. 그러나 사용자가 로그인했을 때만 특정 경로에 액세스 할 수 있도록하고 싶습니다. this snippet을 찾았으며 설정에 맞게 약간의 변경 작업을 수행했습니다.Omniauth Google 인증 경로
application_controller.rb
before_filter :authenticate
def authenticate
redirect_to :login unless User.from_omniauth(env["omniauth.auth"])
end
user.rb
def self.from_omniauth(auth)
where(provider: auth.provider, uid: auth.uid).first_or_initialize.tap do |user|
user.provider = auth.provider
user.uid = auth.uid
user.name = auth.info.name
user.first_name = auth.info.first_name
user.last_name = auth.info.last_name
user.email = auth.info.email
user.picture = auth.info.image
user.oauth_token = auth.credentials.token
user.oauth_expires_at = Time.at(auth.credentials.expires_at)
user.save!
end
그게 내가 내 SessionsController
에 사용되는 인증 해시이기 때문에 내가 env["omniauth"]
을 사용했다. 내가 localhost:3000
에 갈 때마다
그러나 지금, 나는 다음과 같은 오류 얻을 : env["omniauth.auth"]
이 application_controller.rb
에서 액세스 할 수 없기 때문에이
undefined method `provider' for nil:NilClass
내가 겠지를 어떻게됩니까? 그렇다면 인증 해시에 올바르게 액세스하려면 어떻게해야합니까?
'from_omniauth' 클래스 메쏘드는'user.rb' 파일 내에서 어떻게 생겼습니까? –
@JustinLicata가 방금 원래 게시물을 업데이트했습니다. – Carpetfizz
"localhost : 3000"을 방문하면'env [ "omniauth.auth"]'는'nil'을 반환합니다. 내가 믿는 이유는이 env 변수가 사회적 로그인 후에 콜백 요청에서만 설정된다는 것입니다. 다른 모든 요청의 경우이 값은 'nil'입니다. –