Module: Jm81auth::Models::User::ClassMethods

Defined in:
lib/jm81auth/models/user.rb

Instance Method Summary collapse

Instance Method Details

#find_by_email(email) ⇒ User?

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

Parameters:

  • email (~to_s)

    Email Address

Returns:



26
27
28
29
30
31
32
# 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) ⇒ AuthToken

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.

Parameters:

  • oauth (OAuth::Base)

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

Returns:



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/jm81auth/models/user.rb', line 44

def  oauth
  method = ::AuthMethod.by_provider_data oauth.provider_data

  if !method
    user = find_by_email(oauth.email) || create(
      email: oauth.email.downcase,
      display_name: oauth.display_name
    )

    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
end