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 execute_login(&block)
end
def promise_login(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` nil
else
Isomorfeus.raise_error(message: "Login failed with '#{agent.response[:error]}'!") end
end
end
end
end
end
|