83 lines
2.0 KiB
Ruby
83 lines
2.0 KiB
Ruby
# == Schema Information
|
|
#
|
|
# Table name: users
|
|
#
|
|
# id :uuid not null, primary key
|
|
# first_name :string
|
|
# last_name :string
|
|
# email :string default(""), not null
|
|
# encrypted_password :string default(""), not null
|
|
# reset_password_token :string
|
|
# reset_password_sent_at :datetime
|
|
# remember_created_at :datetime
|
|
# created_at :datetime not null
|
|
# updated_at :datetime not null
|
|
# uid :string
|
|
# provider :string
|
|
#
|
|
class User < ApplicationRecord
|
|
# Include default devise modules. Others available are:
|
|
# :confirmable, :lockable, :timeoutable, :trackable and
|
|
devise :database_authenticatable, :registerable, :omniauthable
|
|
|
|
|
|
def name
|
|
"#{first_name} #{last_name}"
|
|
end
|
|
|
|
def self.find_or_create_from_auth_hash(auth)
|
|
user = where(email: auth.info.email).first
|
|
|
|
unless user.present?
|
|
user = where(provider: auth.provider, uid: auth.uid).first_or_initialize
|
|
user.email = auth.info.email
|
|
user.password = Devise.friendly_token.first(8)
|
|
end
|
|
|
|
unless user.provider.present?
|
|
user.provider = auth.provider
|
|
user.uid = auth.uid
|
|
end
|
|
|
|
user.first_name = auth.info.first_name
|
|
user.last_name = auth.info.last_name
|
|
|
|
user.save!
|
|
|
|
user
|
|
end
|
|
|
|
def fullname
|
|
"#{first_name} #{last_name}"
|
|
end
|
|
|
|
def confirmation_token
|
|
if read_attribute(:confirmation_token).nil?
|
|
self.confirmation_token = generate_confirmation_token
|
|
save!
|
|
end
|
|
read_attribute(:confirmation_token)
|
|
end
|
|
|
|
def json_payload
|
|
{
|
|
id: id,
|
|
email: email,
|
|
first_name: first_name,
|
|
last_name: last_name,
|
|
}
|
|
end
|
|
|
|
def admin?
|
|
%w[mike@wizewerx.com].include?(email)
|
|
end
|
|
|
|
private
|
|
|
|
def generate_confirmation_token
|
|
token = Devise.friendly_token(10)
|
|
token = Devise.friendly_token(10) while User.where(confirmation_token: token).count.positive?
|
|
self.confirmation_token = token
|
|
end
|
|
end
|