Module: EffectiveDeviseUser::ClassMethods

Defined in:
app/models/concerns/effective_devise_user.rb

Instance Method Summary collapse

Instance Method Details

#from_omniauth(auth, params) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'app/models/concerns/effective_devise_user.rb', line 73

def from_omniauth(auth, params)
  invitation_token = (params.presence || {})['invitation_token']

  email = (auth.info.email.presence || "#{auth.uid}@#{auth.provider}.none").downcase
  image = auth.info.image
  name = auth.info.name || auth.dig(:extra, :raw_info, :login)

  user = if invitation_token
    find_by_invitation_token(invitation_token, false) || raise(ActiveRecord::RecordNotFound)
  else
    where(uid: auth.uid).or(where(email: email)).first || self.new()
  end

  user.assign_attributes(
    uid: auth.uid,
    provider: auth.provider,
    email: email,
    avatar_url: image,
    name: name,
    first_name: (auth.info.first_name.presence || name.split(' ').first.presence || 'First'),
    last_name: (auth.info.last_name.presence || name.split(' ').last.presence || 'Last')
  )

  if auth.respond_to?(:credentials)
    user.assign_attributes(
      access_token: auth.credentials.token,
      refresh_token: auth.credentials.refresh_token,
      token_expires_at: Time.zone.at(auth.credentials.expires_at), # We are given integer datetime e.g. '1549394077'
    )
  end

  # Make a password
  user.password = Devise.friendly_token[0, 20] if user.encrypted_password.blank?

  # Devise Invitable
  invitation_token ? user.accept_invitation! : user.save!

  # Devise Confirmable
  user.confirm if user.respond_to?(:confirm)

  user
end

#permitted_sign_up_paramsObject

Should contain all fields as per views/users/_sign_up_fields



68
69
70
71
# File 'app/models/concerns/effective_devise_user.rb', line 68

def  # Should contain all fields as per views/users/_sign_up_fields
  raise('please define a self.permitted_sign_up_params')
  [:email, :password, :password_confirmation, :first_name, :last_name, :name, :login]
end

#send_reset_password_instructions(attributes = {}) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'app/models/concerns/effective_devise_user.rb', line 117

def send_reset_password_instructions(attributes = {})
  recoverable = find_or_initialize_with_errors(reset_password_keys, attributes, :not_found)
  return recoverable unless recoverable.persisted?

  # Add custom errors and require a confirmation if previous sign in was provider
  if recoverable.provider.present? && attributes[:confirm_new_password].blank?
    recoverable.errors.add(:email, "previous sign in was with #{recoverable.provider}")
    recoverable.errors.add(:confirm_new_password, 'please confirm to proceed')
  end

  recoverable.send_reset_password_instructions if recoverable.errors.blank?
  recoverable
end