loginprompt 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/loginprompt.rb +99 -0
- metadata +45 -0
data/lib/loginprompt.rb
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
|
3
|
+
|
4
|
+
# Add current_user to ActionController automatically
|
5
|
+
|
6
|
+
ActiveSupport.on_load(:action_controller) do
|
7
|
+
include LoginPrompt::ActionController
|
8
|
+
end
|
9
|
+
|
10
|
+
|
11
|
+
module LoginPrompt
|
12
|
+
|
13
|
+
# Simple user model
|
14
|
+
class User
|
15
|
+
attr_accessor :name, :email, :id
|
16
|
+
|
17
|
+
def initialize(hash)
|
18
|
+
hash.each do |k, v|
|
19
|
+
self.send("#{k}=", v)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
# When included into ActionController provides
|
25
|
+
# current_user and signed_in?
|
26
|
+
|
27
|
+
module ActionController
|
28
|
+
extend ActiveSupport::Concern
|
29
|
+
|
30
|
+
included do
|
31
|
+
helper_method :current_user, :signed_in?
|
32
|
+
end
|
33
|
+
|
34
|
+
def current_user
|
35
|
+
|
36
|
+
if @current_user.nil? && cookies['LP-auth']
|
37
|
+
begin
|
38
|
+
token = JSON.parse(cookies['LP-auth'])
|
39
|
+
rescue
|
40
|
+
token = nil
|
41
|
+
end
|
42
|
+
|
43
|
+
user = Api.get_user(token)
|
44
|
+
@current_user = user if user
|
45
|
+
end
|
46
|
+
|
47
|
+
@current_user
|
48
|
+
end
|
49
|
+
|
50
|
+
def signed_in?
|
51
|
+
current_user.present?
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
|
56
|
+
# Helper to make API calls
|
57
|
+
class Api
|
58
|
+
|
59
|
+
# Returns a user object if successful
|
60
|
+
def self.get_user(token)
|
61
|
+
#call('https://demo.loginprompt.com/login/get_user?remember_token=' + token['remember_token']) if token
|
62
|
+
if token
|
63
|
+
result = call('http://localhost:3000/login/get_user?remember_token=' + token['remember_token'])
|
64
|
+
user = result['user'] if result
|
65
|
+
|
66
|
+
return User.new(id: user['id'], email: user['email'], name: user['name'])
|
67
|
+
end
|
68
|
+
|
69
|
+
return nil
|
70
|
+
end
|
71
|
+
|
72
|
+
private
|
73
|
+
|
74
|
+
# Calls a given URL and checks for success
|
75
|
+
def self.call(api_url)
|
76
|
+
result = nil
|
77
|
+
|
78
|
+
uri = URI(api_url)
|
79
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
80
|
+
|
81
|
+
if uri.scheme =='https'
|
82
|
+
http.use_ssl = true
|
83
|
+
end
|
84
|
+
|
85
|
+
http.start do
|
86
|
+
request = Net::HTTP::Get.new(uri.request_uri)
|
87
|
+
answer = http.request(request)
|
88
|
+
|
89
|
+
if (answer.is_a? Net::HTTPOK)
|
90
|
+
response = JSON.parse(answer.body)
|
91
|
+
result = response if response['success'] == true
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
result
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
metadata
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: loginprompt
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Cedric Dussud
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-11-08 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: A simple gem to integrate Rails apps with the loginPrompt service.
|
15
|
+
email: cedric@loginprompt.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- lib/loginprompt.rb
|
21
|
+
homepage: https://www.loginprompt.com/docs/current_user
|
22
|
+
licenses: []
|
23
|
+
post_install_message:
|
24
|
+
rdoc_options: []
|
25
|
+
require_paths:
|
26
|
+
- lib
|
27
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
34
|
+
none: false
|
35
|
+
requirements:
|
36
|
+
- - ! '>='
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
39
|
+
requirements: []
|
40
|
+
rubyforge_project:
|
41
|
+
rubygems_version: 1.8.24
|
42
|
+
signing_key:
|
43
|
+
specification_version: 3
|
44
|
+
summary: loginPrompt rails library
|
45
|
+
test_files: []
|