Method: EffectiveDeviseUser::ClassMethods#from_omniauth

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

#from_omniauth(auth, params) ⇒ Object



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
115
116
# File 'app/models/concerns/effective_devise_user.rb', line 75

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