41 lines
1.2 KiB
Ruby
41 lines
1.2 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
# Model: User
|
|
#
|
|
# Description: Define an application user. the only authentication mecanism
|
|
# is omniauthable with the BattleNet API
|
|
class User < ApplicationRecord
|
|
devise :rememberable, :omniauthable, omniauth_providers: [:bnet]
|
|
|
|
has_many :wow_characters, dependent: :destroy
|
|
has_many :user_obtain_wow_mounts, dependent: :destroy
|
|
has_many :wow_mounts, through: :user_obtain_wow_mounts
|
|
has_many :user_obtain_wow_pets, dependent: :destroy
|
|
has_many :wow_pets, through: :user_obtain_wow_pets
|
|
|
|
validates :battletag, presence: true, uniqueness: true
|
|
|
|
def self.from_omniauth(auth)
|
|
user = User.where(provider: auth.provider, uid: auth.uid).first_or_initialize
|
|
|
|
user.provider = auth.provider
|
|
user.uid = auth.uid
|
|
user.battletag = auth.info.battletag
|
|
user.token_expires = auth.credentials.expires
|
|
user.token_expire_at = Time.at(auth.credentials.expires_at).utc
|
|
user.token = auth.credentials.token
|
|
|
|
user.save
|
|
|
|
user
|
|
end
|
|
|
|
def self.new_with_session(params, session)
|
|
super.tap do |user|
|
|
if (data = session['devise.bnet_data']) && session['devise.bnet_data']['extra']['raw_info'] && user.email.blank?
|
|
user.email = data['email']
|
|
end
|
|
end
|
|
end
|
|
end
|