dailycred 0.1.36 → 0.1.41

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -28,12 +28,85 @@ While this is enough to get off the ground running with user authentication, thi
28
28
 
29
29
  ## Authenticating
30
30
 
31
+ #### login_path
32
+
33
+ A helper for linking to the authentication url.
34
+
35
+ <%= link_to 'sign up', login_path %>
36
+ # => <a href="/auth/dailycred">sign up</a>
37
+
38
+ #### logout_path
39
+
40
+ To logout a user, simply send them to `/auth/logout`.
41
+
42
+ <%= link_to 'logout', logout_path %>
43
+ # => <a href="/auth/logout">logout</a>
44
+
45
+ #### authenticate
46
+
31
47
  To protect a controller method from unauthorized users, use the 'authorize' helper method as a `before_filter`.
32
48
 
33
49
  #before_filter :authenticate, :except => [:index] #don't authenticate some
34
50
  #before_filter :authenticate, :only => [:create, :new] #only authenticate some
35
51
  before_filter :authenticate #all methods
36
52
 
53
+ ## Social Connect
54
+
55
+ To use a social sign-in service instead of email, and password, use `connect_path.`
56
+
57
+ <%= link_to 'sign in with facebook', connect_path(:identity_provider => :facebook) %>
58
+
59
+ The `identity_provider` can be one of `facebook`, `google`, or `twitter.`
60
+
61
+ After a user has social connected, their social data is serialized into individual fields in the user model. The serialized object is the exact same as what the social provider's graph response returns. For example:
62
+
63
+ p current_user.facebook
64
+ # =>
65
+ {
66
+ "video_upload_limits" => {
67
+ "length" => 1200.0,
68
+ "size" => 1073741824.0
69
+ },
70
+ "locale" => "en_US",
71
+ "link" => "http://www.facebook.com/joe.smith",
72
+ "updated_time" => "2012-09-27T19:04:38+0000",
73
+ "currency" => {
74
+ "user_currency" => "USD",
75
+ "currency_exchange" => 10.0,
76
+ "currency_exchange_inverse" => 0.1,
77
+ "currency_offset" => 100.0
78
+ },
79
+ "picture" => {
80
+ "data" => {
81
+ "url" => "http://profile.ak.fbcdn.net/hprofile-ak-ash4/370570_1039690812_2022945351_q.jpg",
82
+ "is_silhouette" => false
83
+ }
84
+ },
85
+ "id" => "1092609812",
86
+ "third_party_id" => "cBLDKnqlfYlReV7Jo4yRAFB1a4I",
87
+ "first_name" => "Joe",
88
+ "username" => "jsmitty",
89
+ "bio" => "shred the gnar.",
90
+ "email" => "jsmitty@dailycred.com",
91
+ "verified" => true,
92
+ "name" => "Joe Smith",
93
+ "last_name" => "Stoever",
94
+ "gender" => "male",
95
+ "access_token" =>"AAAFHsZAi9ddUBAKPMOKPDrmJlclwCoVHCfwflF5ZCyLZC70SOo0MPvj62lhHZAnV6jk8DEfBSjLtfcyC7Bx25a9CLphzoayv3EtvbE2tAQZDZD"
96
+ }
97
+
98
+ You can also connect additional social accounts to an existing user:
99
+
100
+ <%= link_to 'connect with facebook', connect_user(:facebook) %>
101
+
102
+ `connect_user` defaults to connecting the `current_user`, but you can explicitly connect any user:
103
+
104
+ <%= link_to 'connect with google', connect_user(:google, @user) %>
105
+
106
+ ---
107
+
108
+ ##Helpers
109
+
37
110
  There are a few other helper methods available:
38
111
 
39
112
  #### current_user
@@ -62,20 +135,6 @@ or just as a helper
62
135
  dailycred.event(current_user.uid, "New Task", @task.name)
63
136
  end
64
137
 
65
- #### login_path
66
-
67
- A helper for linking to the authentication url. Usage:
68
-
69
- <%= link_to 'sign up', login_path %>
70
- # => <a href="/auth/dailycred">sign up</a>
71
-
72
- #### Logging out
73
-
74
- To logout a user, simply send them to `/auth/logout`.
75
-
76
- <%= link_to 'logout', logout_path %>
77
- # => <a href="/auth/logout">logout</a>
78
-
79
138
  #### Tagging a User
80
139
 
81
140
  Dailycred provides the ability to 'tag' users, whether for reference in analytics or any other reason. Note that this is a very simple 'tagging' system, and not something you should use for dynamic tagging situations in your application.
@@ -90,6 +149,12 @@ You can also fire events tied to a specific user - this is helpful for goal trac
90
149
  # user#fire_event(key, value)
91
150
  @user.fire_event 'task added', @task.name
92
151
 
152
+ #### Building Referral URLs
153
+
154
+ To easily build referral URLs to track sharing amongst your users, use `referral_link(my_site)`, where *my_site* is the url that you wish referred users to be sent to.
155
+
156
+ current_user.referral_link("http://www.mysite.com")
157
+
93
158
  #### Testing Controllers
94
159
 
95
160
  Testing controllers that have the `authenticate` before filter is easy:
@@ -1,6 +1,7 @@
1
1
  class SessionsController < ApplicationController
2
2
  before_filter :authenticate, :only => [:destroy]
3
3
  before_filter :current_user
4
+ include Dailycred::Helpers
4
5
 
5
6
  # Callback Route for OAuth flow
6
7
  def create
@@ -1,7 +1,13 @@
1
1
  <% if current_user %>
2
2
  <p>Hello <%= current_user.email %> <br>
3
3
  <%= link_to 'Logout', dailycred_engine.logout_path %>
4
+ <br><%= link_to 'connect with facebook', connect_user(:facebook) %>
5
+ <br><%= link_to 'connect with google', connect_user(:google) %>
6
+ <br><%= link_to 'connect with twitter', connect_user(:twitter) %>
4
7
  </p>
5
8
  <% else %>
6
9
  <%= link_to 'Log In', login_path() %>
10
+ <br><%= link_to 'connect with facebook', connect_path(:identity_provider => :facebook) %>
11
+ <br><%= link_to 'connect with google', connect_path(:identity_provider => :google) %>
12
+ <br><%= link_to 'connect with twitter', current_user.connect_path(:twitter) %>
7
13
  <% end %>
data/dailycred.gemspec CHANGED
@@ -15,5 +15,5 @@ Gem::Specification.new do |gem|
15
15
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
16
16
  gem.name = "dailycred"
17
17
  gem.require_paths = ["lib"]
18
- gem.version = "0.1.36"
18
+ gem.version = "0.1.41"
19
19
  end
data/dummy/Gemfile CHANGED
@@ -7,6 +7,7 @@ gem 'rails', '3.2.8'
7
7
 
8
8
  gem 'sqlite3'
9
9
  gem 'dailycred', :path => '../'
10
+ gem 'awesome_print'
10
11
 
11
12
 
12
13
  # Gems used only for assets and not required
@@ -24,6 +25,8 @@ end
24
25
  gem 'jquery-rails'
25
26
  gem 'factory_girl'
26
27
 
28
+ gem 'quiet_assets', :group => :development
29
+
27
30
  group :test, :development do
28
31
  gem 'guard-test'
29
32
  gem 'rb-fsevent', '~> 0.9.1'
@@ -1,12 +1,13 @@
1
1
  class ApplicationController < ActionController::Base
2
2
  helper_method :current_user
3
-
4
3
  private
5
4
 
6
5
  # helper method for getting the current signed in user
7
6
  def current_user
8
7
  begin
9
- @current_user || User.find(session[:user_id]) if session[:user_id]
8
+ @current_user ||= User.find(session[:user_id]) if session[:user_id]
9
+ ap @current_user
10
+ @current_user
10
11
  rescue
11
12
  nil
12
13
  end
@@ -2,8 +2,12 @@ Rails.configuration.DAILYCRED_CLIENT_ID = "4133a23f-b9c3-47e4-8989-cfb30510079d"
2
2
  Rails.configuration.DAILYCRED_SECRET_KEY = "a1c21e72-98d8-47c2-9e9a-1e2dcd363b2f-f353b2af-1f51-416c-ad4c-59e70721dfab"
3
3
 
4
4
  # configure where users should be redirected after authentication
5
- #
6
- # Rails.configuration.DAILYCRED_OPTIONS = {
7
- # :after_auth => '/hello', #after login
8
- # :after_unauth => '/goodbye' #after logout
9
- # }
5
+
6
+ Rails.configuration.DAILYCRED_OPTIONS = {
7
+ :client_options => {
8
+ :site => 'http://ec2-72-44-40-55.compute-1.amazonaws.com:9000',
9
+ :verbose => true
10
+ }
11
+ # :after_auth => '/hello', #after login
12
+ # :after_unauth => '/goodbye' #after logout
13
+ }
@@ -0,0 +1,70 @@
1
+ class UpdateUsers2 < ActiveRecord::Migration
2
+ def self.up
3
+ if !table_exists? :users
4
+ create_table :users do |t|
5
+ t.string :provider
6
+ t.string :uid
7
+ t.string :email
8
+ t.integer :created
9
+ t.string :username
10
+ t.boolean :verified
11
+ t.boolean :admin
12
+ t.string :referred_by
13
+ t.string :token
14
+ t.text :facebook
15
+ t.text :tags
16
+ t.text :referred
17
+ t.text :google
18
+ t.text :twitter
19
+ t.text :github
20
+ t.text :access_tokens
21
+ t.boolean :subscribed
22
+ t.string :display
23
+
24
+ t.timestamps
25
+ end
26
+ else
27
+ safe_column :users, :provider, :string
28
+ safe_column :users, :uid, :string
29
+ safe_column :users, :email, :string
30
+ safe_column :users, :created, :integer
31
+ safe_column :users, :username, :string
32
+ safe_column :users, :verified, :booleanied
33
+ safe_column :users, :admin, :boolean
34
+ safe_column :users, :referred, :string
35
+ safe_column :users, :token, :string
36
+ safe_column :users, :facebook, :text
37
+ safe_column :users, :tags, :text
38
+ safe_column :users, :referred, :text
39
+ safe_column :users, :google, :text
40
+ safe_column :users, :twitter, :text
41
+ safe_column :users, :github, :text
42
+ safe_column :users, :access_tokens, :text
43
+ safe_column :users, :display, :string
44
+ safe_column :users, :subscribed, :boolean
45
+ end
46
+ end
47
+
48
+ def self.down
49
+ drop_table :users
50
+ end
51
+
52
+ private
53
+
54
+ def table_exists? table
55
+ ActiveRecord::Base.connection.table_exists?(table.to_s)
56
+ end
57
+
58
+ def column_exists?(table, column)
59
+ begin
60
+ ActiveRecord::Base.connection.columns(table.to_s).map(&:name).include?(column.to_s)
61
+ rescue NoMethodError #actual_columns is empty and it doesn't respond to the map method
62
+ false
63
+ end
64
+ end
65
+
66
+ def safe_column table, name, type
67
+ add_column table.to_sym, name.to_sym, type.to_sym unless column_exists? table, name
68
+ end
69
+
70
+ end
data/dummy/db/schema.rb CHANGED
@@ -11,7 +11,7 @@
11
11
  #
12
12
  # It's strongly recommended to check this file into your version control system.
13
13
 
14
- ActiveRecord::Schema.define(:version => 20120926012555) do
14
+ ActiveRecord::Schema.define(:version => 20121002192037) do
15
15
 
16
16
  create_table "posts", :force => true do |t|
17
17
  t.string "title"
@@ -23,7 +23,7 @@ ActiveRecord::Schema.define(:version => 20120926012555) do
23
23
  t.string "provider"
24
24
  t.string "uid"
25
25
  t.string "email"
26
- t.integer "created", :limit => 8
26
+ t.integer "created", :limit => 8
27
27
  t.string "username"
28
28
  t.boolean "verified"
29
29
  t.boolean "admin"
@@ -35,10 +35,11 @@ ActiveRecord::Schema.define(:version => 20120926012555) do
35
35
  t.text "google"
36
36
  t.text "twitter"
37
37
  t.text "github"
38
- t.datetime "created_at", :null => false
39
- t.datetime "updated_at", :null => false
38
+ t.datetime "created_at", :null => false
39
+ t.datetime "updated_at", :null => false
40
40
  t.string "display"
41
41
  t.boolean "subscribed"
42
+ t.text "access_tokens"
42
43
  end
43
44
 
44
45
  end
@@ -58,6 +58,11 @@ class UserTest < ActiveSupport::TestCase
58
58
  assert_true json_response(@user.fire_event 'got tested')['worked']
59
59
  end
60
60
 
61
+ test 'making a referral link' do
62
+ id = @user.uid
63
+ assert_equal @user.referral_link('http://me.com'), "https://www.dailycred.com/r/#{id}?redirect_uri=http://me.com"
64
+ end
65
+
61
66
  def json_response response
62
67
  JSON.parse response.body
63
68
  end
@@ -6,9 +6,11 @@ module ActsAsDailycred
6
6
  serialize :github, Hash
7
7
  serialize :tags, Array
8
8
  serialize :referred, Array
9
+ serialize :access_tokens, Hash
9
10
 
10
11
  extend ActsAsDailycred::SingletonMethods
11
12
  include ActsAsDailycred::InstanceMethods
13
+ include Dailycred::Helpers
12
14
  end
13
15
  module SingletonMethods
14
16
  def find_or_create_with_omniauth(model)
@@ -38,13 +40,33 @@ module ActsAsDailycred
38
40
  end
39
41
  end
40
42
 
43
+ def display_name
44
+ display = self.email || ""
45
+ p '1'
46
+ if self.facebook != {}
47
+ p '2'
48
+ return self.facebook["name"]
49
+ elsif self.google != {}
50
+ p '3'
51
+ return self.google["name"]
52
+ elsif self.twitter != {}
53
+ p '4'
54
+ return "@"+self.twitter["screen_name"]
55
+ end
56
+ display
57
+ end
58
+
59
+ def referral_link url
60
+ "https://www.dailycred.com/r/#{self.uid}?redirect_uri=#{url}"
61
+ end
41
62
  def reset_password
42
63
  get_client.reset_password self.email
43
64
  end
44
65
 
45
66
  def update_from_dailycred dc
67
+ bad = ['updated_at', 'created_at']
46
68
  dc.each do |k,v|
47
- self[k] = v if self[k] != v
69
+ self[k] = v if self.respond_to?(k) and !bad.include?(k.to_s)
48
70
  end
49
71
  save!
50
72
  end
@@ -56,6 +78,10 @@ module ActsAsDailycred
56
78
  def get_client
57
79
  @dailycred ||= Dailycred::Client.new Rails.configuration.DAILYCRED_CLIENT_ID, Rails.configuration.DAILYCRED_SECRET_KEY
58
80
  end
81
+
82
+ def connect_path provider
83
+ "/auth/dailycred?identity_provider=#{provider.to_s}"
84
+ end
59
85
  end
60
86
  end
61
87
  require 'active_record'
@@ -28,6 +28,23 @@ module Dailycred
28
28
  "/auth/dailycred"
29
29
  end
30
30
 
31
+ def connect_path(params)
32
+ url = "#{request.protocol}#{request.host_with_port}/auth/dailycred"
33
+ p = []
34
+ params.each do |k,v|
35
+ p << "#{k}=#{v.to_s}"
36
+ end
37
+ url += "?" if p.size > 0
38
+ url += p.join("&")
39
+ end
40
+
41
+ def connect_user provider, user=nil
42
+ if user.nil?
43
+ user = current_user
44
+ end
45
+ connect_path(access_token: user.token, identity_provider: provider)
46
+ end
47
+
31
48
  def redirect_to_auth opts={}
32
49
  conf = Rails.configuration.DAILYCRED_OPTIONS
33
50
  path = !conf[:after_auth].nil? ? conf[:after_auth] : dailycred_engine.auth_info_path
@@ -39,10 +56,5 @@ module Dailycred
39
56
  path = !conf[:after_unauth].nil? ? conf[:after_unauth] : dailycred_engine.auth_info_path
40
57
  redirect_to path, opts
41
58
  end
42
-
43
-
44
- ActiveSupport.on_load(:action_controller) do
45
- helper_method :current_user, :login_path, :set_state, :dailycred, :authenticate
46
- end
47
59
  end
48
60
  end
@@ -50,7 +50,7 @@ module Dailycred
50
50
  </script>
51
51
  EOT
52
52
  str2 =<<-EOT
53
- <script src="#{@opts[:url]}/public/js/dailycred.coffee"></script>
53
+ <script src="https://s3.amazonaws.com/file.dailycred.com/js/dailycred.js"></script>
54
54
  <script>
55
55
  DC.init({
56
56
  "showModal" : #{@opts[:modal]}
@@ -17,6 +17,7 @@ class CreateUsers < ActiveRecord::Migration
17
17
  t.text :google
18
18
  t.text :twitter
19
19
  t.text :github
20
+ t.text :access_tokens
20
21
  t.boolean :subscribed
21
22
  t.string :display
22
23
 
@@ -38,6 +39,7 @@ class CreateUsers < ActiveRecord::Migration
38
39
  safe_column :users, :google, :text
39
40
  safe_column :users, :twitter, :text
40
41
  safe_column :users, :github, :text
42
+ safe_column :users, :access_tokens, :text
41
43
  safe_column :users, :display, :string
42
44
  safe_column :users, :subscribed, :boolean
43
45
  end
@@ -12,12 +12,12 @@ module OmniAuth
12
12
  # default options
13
13
  option :client_options, {
14
14
  :site => "https://www.dailycred.com",
15
- :authorize_url => '/oauth/authorize',
15
+ :authorize_url => '/connect',
16
16
  :token_url => '/oauth/access_token'
17
17
  }
18
18
 
19
19
  # allows parameters to be passed through
20
- AUTH_PARAMS = ["action","identity_provider","referrer"]
20
+ AUTH_PARAMS = ["action","identity_provider","referrer", "access_token"]
21
21
 
22
22
  option :authorize_options, OmniAuth::Strategies::Dailycred::AUTH_PARAMS
23
23
 
@@ -57,7 +57,7 @@ module OmniAuth
57
57
  connection = Faraday::Connection.new options.client_options[:site], :ssl => options.client_options[:ssl]
58
58
  response = connection.get("/graph/me.json?access_token=#{access_token.token}")
59
59
  json = JSON.parse(response.body)
60
- # pp json
60
+ pp json if options[:verbose]
61
61
  @duser = {'token' => access_token.token}
62
62
  @duser['provider'] = 'dailycred'
63
63
  @duser['uid'] = json['id'] || json['user_id']
@@ -68,7 +68,8 @@ module OmniAuth
68
68
  @duser[k] = v
69
69
  @duser[k][:access_token] = json["access_tokens"][k]
70
70
  end if !json["identities"].nil?
71
- # pp @duser
71
+ pp @duser if options[:verbose]
72
+ @duser.delete("id")
72
73
 
73
74
  @duser
74
75
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dailycred
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.36
4
+ version: 0.1.41
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-26 00:00:00.000000000 Z
12
+ date: 2012-10-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: omniauth
@@ -121,6 +121,7 @@ files:
121
121
  - dummy/config/routes.rb
122
122
  - dummy/db/migrate/20120925172903_create_users.rb
123
123
  - dummy/db/migrate/20120926012555_create_posts.rb
124
+ - dummy/db/migrate/20121002192037_update_users_2.rb
124
125
  - dummy/db/schema.rb
125
126
  - dummy/db/seeds.rb
126
127
  - dummy/lib/assets/.gitkeep