以 preexisting_user 身份登录会话?
Sign into session as preexisting_user?
我在尝试使用 Facebook 登录时遇到错误:"couldn't find user with id"。我想要它,如果已经创建了用户,它会触发条件中的第一组代码,如果用户是新用户,它会触发第二组代码:
sessions_controller
def facebook
preexisting_user = User.find(params[:id]) # What should go in this line?
user = User.from_omniauth(env["omniauth.auth"])
if preexisting_user
cookies.permanent.signed[:user_id] = user.id
redirect_to root_url
flash.now[:info] = 'Welcome Back to Live to Challenge!'
else
action = session.delete(:challenge_action)
user.challenges.create(action: action)
user.send_welcome_email
user.remember
cookies.permanent.signed[:user_id] = user.id
redirect_to tutorial_url
flash.now[:info] = 'Welcome to Live to Challenge!'
end
end
user.rb
def self.from_omniauth(auth)
# Sets 60 day auth token
oauth = Koala::Facebook::OAuth.new("154037ewr2976229929", "ee917abf2ere8f1c98274cdfwqreaebb1346f4")
new_access_info = oauth.exchange_access_token_info auth.credentials.token
new_access_token = new_access_info["access_token"]
new_access_expires_at = DateTime.now + new_access_info["expires"].to_i.seconds
where(provider: auth.provider, uid: auth.uid).first_or_initialize.tap do |user|
user.provider = auth.provider
user.image = auth.info.image
user.uid = auth.uid
user.name = auth.info.name
user.oauth_token = new_access_token # auth.credentials.token <- your old token. Not needed anymore.
user.oauth_expires_at = Time.at(auth.credentials.expires_at)
user.password = (0...8).map { (65 + rand(26)).chr }.join
user.email = SecureRandom.hex + "@mailinator.com" unless user.email.present?
user.activated = true
user.save!
end
end
您可以尝试下面的代码,我相信它会起作用。
def facebook
user = User.from_omniauth(env["omniauth.auth"])
# preexisting_user = User.find(params[:id]) # What should go in this line?
# user.persisted? is what you need to add if you want to add here
# if user pre existed, it will execute if block otherwise else block.
# checks if pre-existing user
if user.persisted?
cookies.permanent.signed[:user_id] = user.id
redirect_to root_url
flash.now[:info] = 'Welcome Back to Live to Challenge!'
else
action = session.delete(:challenge_action)
user.challenges.create(action: action)
user.send_welcome_email
user.remember
cookies.permanent.signed[:user_id] = user.id
redirect_to tutorial_url
flash.now[:info] = 'Welcome to Live to Challenge!'
end
end
我在尝试使用 Facebook 登录时遇到错误:"couldn't find user with id"。我想要它,如果已经创建了用户,它会触发条件中的第一组代码,如果用户是新用户,它会触发第二组代码:
sessions_controller
def facebook
preexisting_user = User.find(params[:id]) # What should go in this line?
user = User.from_omniauth(env["omniauth.auth"])
if preexisting_user
cookies.permanent.signed[:user_id] = user.id
redirect_to root_url
flash.now[:info] = 'Welcome Back to Live to Challenge!'
else
action = session.delete(:challenge_action)
user.challenges.create(action: action)
user.send_welcome_email
user.remember
cookies.permanent.signed[:user_id] = user.id
redirect_to tutorial_url
flash.now[:info] = 'Welcome to Live to Challenge!'
end
end
user.rb
def self.from_omniauth(auth)
# Sets 60 day auth token
oauth = Koala::Facebook::OAuth.new("154037ewr2976229929", "ee917abf2ere8f1c98274cdfwqreaebb1346f4")
new_access_info = oauth.exchange_access_token_info auth.credentials.token
new_access_token = new_access_info["access_token"]
new_access_expires_at = DateTime.now + new_access_info["expires"].to_i.seconds
where(provider: auth.provider, uid: auth.uid).first_or_initialize.tap do |user|
user.provider = auth.provider
user.image = auth.info.image
user.uid = auth.uid
user.name = auth.info.name
user.oauth_token = new_access_token # auth.credentials.token <- your old token. Not needed anymore.
user.oauth_expires_at = Time.at(auth.credentials.expires_at)
user.password = (0...8).map { (65 + rand(26)).chr }.join
user.email = SecureRandom.hex + "@mailinator.com" unless user.email.present?
user.activated = true
user.save!
end
end
您可以尝试下面的代码,我相信它会起作用。
def facebook
user = User.from_omniauth(env["omniauth.auth"])
# preexisting_user = User.find(params[:id]) # What should go in this line?
# user.persisted? is what you need to add if you want to add here
# if user pre existed, it will execute if block otherwise else block.
# checks if pre-existing user
if user.persisted?
cookies.permanent.signed[:user_id] = user.id
redirect_to root_url
flash.now[:info] = 'Welcome Back to Live to Challenge!'
else
action = session.delete(:challenge_action)
user.challenges.create(action: action)
user.send_welcome_email
user.remember
cookies.permanent.signed[:user_id] = user.id
redirect_to tutorial_url
flash.now[:info] = 'Welcome to Live to Challenge!'
end
end