Module: LucidUser::Authentication

Defined in:
lib/lucid_user/authentication.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/lucid_user/authentication.rb', line 8

def self.included(base)
  base.instance_exec do
    def (&block)
    end

    def (user: nil, pass: nil, scheme: :isomorfeus, &block)
      send("promise_authentication_with_#{scheme}", user: user, pass: pass, &block)
    end

    def promise_authentication_with_isomorfeus(user: nil, pass: nil, &block)
      if Isomorfeus.production?
        Isomorfeus.raise_error(message: "Connection not secure, can't login!") unless Isomorfeus::Transport.socket.url.start_with?('wss:')
      else
        `console.warn("Connection not secure, ensure a secure connection in production, otherwise login will fail!")` unless Isomorfeus::Transport.socket.url.start_with?('wss:')
      end
      Isomorfeus::Transport.promise_send_path('Isomorfeus::Transport::Handler::AuthenticationHandler', 'login', self.name, user, pass).then do |agent|
        if agent.processed
          agent.result
        else
          agent.processed = true
          if agent.response.key?(:success)
            Isomorfeus.store.dispatch(type: 'DATA_LOAD', data: agent.response[:data])
            class_name = agent.response[:data].keys.first
            key = agent.response[:data][class_name].keys.first
            logged_in_user = Isomorfeus.cached_data_class(class_name).new(key: key)
            cookie_accessor = agent.response[:session_cookie_accessor]
            begin
              target = if block_given?
                         block.call(logged_in_user)
                       else
                         `window.location.pathname`
                       end
              unless target.class == String && target.start_with?('/')
                Isomorfeus.raise_error(message: "A path must be returned as string starting with '/', returned was #{target}!")
              end
            rescue
              target = `window.location.pathname`
            end
            cookie_query = "#{Isomorfeus.cookie_eater_path}?#{cookie_accessor}=#{target}"
            `window.location = cookie_query` # doing page load and redirect
            nil
          else
            Isomorfeus.raise_error(message: "Login failed with '#{agent.response[:error]}'!") # triggers .fail
          end
        end
      end
    end
  end
end

Instance Method Details

#anonymous?Boolean

Returns:

  • (Boolean)


3
4
5
# File 'lib/lucid_user/authentication.rb', line 3

def anonymous?
  self.class == Anonymous
end

#encrypt_password(password, password_confirmation) ⇒ Object



104
105
106
107
# File 'lib/lucid_user/authentication.rb', line 104

def encrypt_password(password, password_confirmation)
  raise "Password and confirmation don't match!" unless password == password_confirmation
  BCrypt::Password.create(password).to_s
end

#passwords_match?(encrypted_password, given_password) ⇒ Boolean

Returns:

  • (Boolean)


109
110
111
112
# File 'lib/lucid_user/authentication.rb', line 109

def passwords_match?(encrypted_password, given_password)
  bcrypt_pass = BCrypt::Password.new(encrypted_password)
  bcrypt_pass == given_password
end

#promise_deauthentication_with_isomorfeusObject



62
63
64
65
66
67
68
69
70
71
# File 'lib/lucid_user/authentication.rb', line 62

def promise_deauthentication_with_isomorfeus
  cookie = `document.cookie`
  p = Promise.new
  begin
    logout_query = Isomorfeus.api_logout_path
    `window.location = logout_query`
  rescue
    p.reject
  end
end

#promise_logout(scheme: :isomorfeus) ⇒ Object



58
59
60
# File 'lib/lucid_user/authentication.rb', line 58

def promise_logout(scheme: :isomorfeus)
  send("promise_deauthentication_with_#{scheme}")
end