module Jm81auth::Models::User::ClassMethods

Public Instance Methods

create_from_oauth(oauth) click to toggle source

Create User based on oauth data.

@param oauth [OAuth::Base]

OAuth login object, include #provider_data (Hash with provider_name
and provider_id), #email and #display_name.

@return [User]

# File lib/jm81auth/models/user.rb, line 66
def create_from_oauth oauth
  create(
    email: oauth.email.downcase,
    display_name: oauth.display_name
  )
end
find_by_email(email) click to toggle source

Find user by email address. Returns nil if the email address is not valid (for a minimal version of valid)

@param email [~to_s] Email Address @return [User, nil]

# File lib/jm81auth/models/user.rb, line 26
def find_by_email email
  if email.to_s =~ EMAIL_REGEX
    where(email: email.to_s.downcase.strip).first
  else
    nil
  end
end
oauth_login(oauth) click to toggle source

Login from OAuth.

First try to find an AuthMethod matching the provider data. If none, find or create a User based on email, then create an AuthMethod. Finally, create and return an AuthToken.

@param oauth [OAuth::Base]

OAuth login object, include #provider_data (Hash with provider_name
and provider_id), #email and #display_name.

@return [AuthToken]

# File lib/jm81auth/models/user.rb, line 44
def oauth_login oauth
  method = ::AuthMethod.by_provider_data oauth.provider_data

  if !method
    user = find_by_email(oauth.email) || create_from_oauth(oauth)

    if user.respond_to? :add_auth_method
      method = user.add_auth_method oauth.provider_data
    else
      method = user.auth_methods.create! oauth.provider_data
    end
  end

  method.create_token access_token: oauth.access_token
end