感谢您抽出宝贵时间回答我的问题。所以我一直在将Facebook登录与我的应用程序集成,我遇到了一个我似乎无法解决的错误。问题似乎在第22行:
user = User.from_omniauth(env['omniauth.auth'])
这是我得到的错误:
SyntaxError at /auth/facebook/callback
syntax error, unexpected tLABEL, expecting '='
SessionsController#facebook_login
app/controllers/sessions_controller.rb, line 22
sessions_controller.rb
def facebook_login
if request.env['omniauth.auth']
user = User.from_omniauth(env['omniauth.auth'])
session[:user_id] = user.id
redirect_back_or root_path
end
end
user.rb
class User < ActiveRecord::Base
include Tokenable
has_many :events
has_secure_password, unless: Proc.new { |a| !a.oauth_token.nil? }
validates_presence_of :email, :first_name, :last_name
validates_uniqueness_of :email, format: /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates_length_of :password, minimum: 6, unless: Proc.new { |a| !a.oauth_token.nil? }
def to_param
token
end
def self.from_omniauth(auth)
where(auth.slice(:provider, :uid)).first_or_initialize.tap do |user|
user.provider = auth.provider
user.uid = auth.uid
user.first_name = auth.info.first_name
user.last_name = auth.info.last_name
user.email = auth.info.email
user.oauth_token = auth.credentials.token
user.oauth_expires_at = Time.at(auth.credentials.expires_at)
user.save!
end
end
end
答案 0 :(得分:1)
更新self.from_omniauth
,如下所示:
def self.from_omniauth(auth)
## Use first_or_create
where(auth.slice(:provider, :uid)).first_or_create do |user|
user.provider = auth.provider
user.uid = auth.uid
user.first_name = auth.info.first_name
user.last_name = auth.info.last_name
user.email = auth.info.email
user.oauth_token = auth.credentials.token
user.oauth_expires_at = Time.at(auth.credentials.expires_at)
## Removed user.save!
end
end
答案 1 :(得分:0)
我想我明白了。问题是
has_secure_password, unless: Proc.new { |a| !a.oauth_token.nil? }
我把它改成了has_secure_password,世界很好:)