Reputation: 1363
Thanks for taking the time to answer my question. So I've been integrating Facebook login with my application and I'm encountering a bug I can't seem to fix. The problem seems to be with line 22 :
user = User.from_omniauth(env['omniauth.auth'])
Here's the error I'm getting:
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
Upvotes: 0
Views: 258
Reputation: 1363
I think I figured it out. The problem was
has_secure_password, unless: Proc.new { |a| !a.oauth_token.nil? }
I changed it to just has_secure_password and the world is good :)
Upvotes: 0
Reputation: 53038
Update self.from_omniauth
as below:
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
Upvotes: 1