Sha256: 5d2e1a2748333cca28e55f12c82e0285708981d8777108856d171e168787f506

Contents?: true

Size: 767 Bytes

Versions: 3

Compression:

Stored size: 767 Bytes

Contents

module ApiUserAuth
  # Model for social provider registration
  class ProviderToken < ApplicationRecord
    belongs_to :auth_user,
               class_name: 'ApiUserAuth::AuthUser'

    enum provider: %I[facebook google instagram]

    def self.find_by_data(data)
      find_by(
        provider: data[:provider],
        user_id: data[:id]
      )
    end

    def self.create_by_data(data, auth_user)
      auth_user ||= create_auth_user(data)
      auth_user.provider_tokens.create(
        provider: data[:provider],
        user_id: data[:id],
        user_data: data
      )
    end

    def self.create_auth_user(data)
      email = data[:id] + '@' + data[:provider] + '.com'
      AuthUser.create(email: email, password: SecureRandom.uuid)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
api_user_auth-0.1.2 app/models/api_user_auth/provider_token.rb
api_user_auth-0.1.1 app/models/api_user_auth/provider_token.rb
api_user_auth-0.1.0 app/models/api_user_auth/provider_token.rb