Module: UserOmniauthConcern::ClassMethods

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

Instance Method Summary collapse

Instance Method Details

#create_by_omniauth(auth) ⇒ User

Create a user from omniauth data.

Parameters:

  • auth (OmniAuth::AuthHash)

    The data with omniauth info.

Returns:

  • (User)

    The user created by omniauth data.


33
34
35
36
37
38
39
40
# File 'app/models/concerns/user_omniauth_concern.rb', line 33

def create_by_omniauth(auth)
  User.create do |user|
    user.assign_attributes(name: auth.info.name, email: auth.info.email,
                           password: Devise.friendly_token[0, 20])
    user.skip_confirmation! if user.email
    user.link_with_omniauth(auth)
  end
end

#find_by_omniauth(auth) ⇒ User?

Find the user from omniauth data.

Parameters:

  • auth (OmniAuth::AuthHash)

    The data with omniauth info.

Returns:

  • (User)

    The user found.

  • (nil)

    If none is found.


24
25
26
27
# File 'app/models/concerns/user_omniauth_concern.rb', line 24

def find_by_omniauth(auth)
  identity = User::Identity.find_by(provider: auth.provider, uid: auth.uid)
  identity&.user
end

#find_or_create_by_omniauth(auth) ⇒ User

Find or create the user from omniauth data.

Parameters:

  • auth (OmniAuth::AuthHash)

    The data with omniauth info.

Returns:

  • (User)

    The user found or created.


15
16
17
# File 'app/models/concerns/user_omniauth_concern.rb', line 15

def find_or_create_by_omniauth(auth)
  find_by_omniauth(auth) || create_by_omniauth(auth)
end