/
user.rb
39 lines (34 loc) · 1.46 KB
/
user.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
class User < ActiveRecord::Base
has_many :user_tokens
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable, :lockable and :timeoutable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :omniauthable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me
def self.new_with_session(params, session)
super.tap do |user|
if data = session[:omniauth]
user.user_tokens.build(:provider => data['provider'], :uid => data['uid'])
end
end
end
def apply_omniauth(omniauth)
#add some info about the user
#self.name = omniauth['user_info']['name'] if name.blank?
#self.nickname = omniauth['user_info']['nickname'] if nickname.blank?
unless omniauth['credentials'].blank?
user_tokens.build(:provider => omniauth['provider'], :uid => omniauth['uid'])
#user_tokens.build(:provider => omniauth['provider'],
# :uid => omniauth['uid'],
# :token => omniauth['credentials']['token'],
# :secret => omniauth['credentials']['secret'])
else
user_tokens.build(:provider => omniauth['provider'], :uid => omniauth['uid'])
end
#self.confirm!# unless user.email.blank?
end
def password_required?
(user_tokens.empty? || !password.blank?) && super
end
end