Advertisement
Guest User

Untitled

a guest
Feb 11th, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.74 KB | None | 0 0
  1. class User < ApplicationRecord
  2. has_many :songs #set song user relations
  3. attr_accessor :login #attr_accessor can login with username and email
  4.  
  5. # Include default devise modules. Others available are:
  6. devise :database_authenticatable, :registerable,
  7. :recoverable, :rememberable, :trackable, :validatable, :confirmable,
  8. :authentication_keys => [:login]
  9. #Facebook Login Authentcation devise module
  10. devise :omniauthable, :omniauth_providers => [:facebook]
  11.  
  12. validates :avatar, presence: true
  13.  
  14. def self.find_for_database_authentication(warden_conditions)
  15. conditions = warden_conditions.dup
  16. if login = conditions.delete(:login)
  17. where(conditions.to_h).where(["lower(username) = :value OR lower(email) = :value", { :value => login.downcase }]).first
  18. elsif conditions.has_key?(:username) || conditions.has_key?(:email)
  19. where(conditions.to_h).first
  20. end
  21. end
  22.  
  23. validates :username,
  24. :presence => true,
  25. :uniqueness => {
  26. :case_sensitive => false
  27. }
  28. #validate username
  29. validates_format_of :username, with: /^[a-zA-Z0-9_\.]*$/, :multiline => true
  30.  
  31. #Add avatar attachment file for paperclip, offer 2 styles
  32. has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100#" }, :default_url => "/images/:style/missing.png"
  33. validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/
  34.  
  35.  
  36. #Facebook Auth handler
  37. def facebook
  38. @user = User.from_omniauth(request.env["omniauth.auth"])
  39.  
  40. if @user.persisted?
  41. sign_in_and_redirect @user, :event => :authentication #this will throw if @user is not activated
  42. set_flash_message(:notice, :success, :kind => "Facebook") if is_navigational_format?
  43. else
  44. session["devise.facebook_data"] = request.env["omniauth.auth"]
  45. redirect_to new_user_registration_url
  46. end
  47. end
  48.  
  49. def failure
  50. redirect_to root_path
  51. end
  52.  
  53. def self.from_omniauth(auth)
  54. where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
  55. user.email = auth.info.email
  56. user.username = auth.info.name
  57. user.password = Devise.friendly_token[0,20]
  58. #user.name = auth.info.name # assuming the user model has a name
  59. #user.image = auth.info.image # assuming the user model has an image
  60. # If you are using confirmable and the provider(s) you use validate emails,
  61. # uncomment the line below to skip the confirmation emails.
  62. # user.skip_confirmation!
  63. end
  64. end
  65.  
  66.  
  67.  
  68. def self.new_with_session(params, session)
  69. super.tap do |user|
  70. if data = session["devise.facebook_data"] && session["devise.facebook_data"]["extra"]["raw_info"]
  71. user.email = data["email"] if user.email.blank?
  72. end
  73. end
  74. end
  75.  
  76. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement