minimum-omniauth-scaffold 0.1.6 → 0.1.7

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b58db84863bddeda53dfe1deb1be497cf6f7e5c6
4
- data.tar.gz: f4cc21e109fa18c68b743c351bc01ef07991a133
3
+ metadata.gz: 52af5b7d1ddd1b11159df09bc27d7aa664383ec9
4
+ data.tar.gz: ab7c2a8fce67e6533b77ddb21ed0db7fa3c4aa6d
5
5
  SHA512:
6
- metadata.gz: a461ee413262ac5570b5aba564e000de4be6a54fd7e035cd39139c6dfb7ef4740730ba62832007d5e75fbea23d00961cdda2c513c6d0e3ea4c83a4451e6626b9
7
- data.tar.gz: 65fdc8f07dac7d6c9c704cb7fc5a5786ac53f91b44a3812e902d56d39a5496fdf49568032d89d8cfe03da08a170d071a710cae4de6bdabc6c2c0bd2550bdf741
6
+ metadata.gz: 67cea8ccb5f7a2d7715eea2b21fa3bd91c62da2f1d57fd3d0f7c3a6b9d9e4a3df54bb34b063b29817674511315af0ce8fec3bff90ea6bfb3b9a38689256ec0bc
7
+ data.tar.gz: ad76623071e21473e9ee084b0edc0acc7d126967402e6c4fc601dc94269edf6b8ae24a33c29c5a2f4d6c3649d48f62e1251a7d13b6848beb7324e7b98b32df7b
data/README.md CHANGED
@@ -65,6 +65,56 @@ rails g minimum:omniauth:scaffold
65
65
  rake db:migrate
66
66
  ```
67
67
 
68
+ ### キー作成
69
+
70
+ * Twitter
71
+ * [Create an application | Twitter Developers](https://dev.twitter.com/apps/new)
72
+ * ※必須マークが無いが 「Callback URL」も入力必要
73
+
74
+ * Facebook
75
+ * [Facebook Developers](https://developers.facebook.com/apps)
76
+ * ※「新しいアプリを作成」作成後「Facebookでログインするウェブサイト」に「http://0.0.0.0:3000/」を入力
77
+
78
+ * GitHub
79
+ * [New OAuth Application](https://github.com/settings/applications/new)
80
+ * ※Main URL/Callback URL:「http://0.0.0.0:3000/」
81
+
82
+ config/settings.local.yml
83
+
84
+ ```yaml
85
+ # Twitter OAuth Local Setting
86
+ twitter_key: "Consumer key"
87
+ twitter_secret: "Consumer secret"
88
+
89
+ # Facebook OAuth Setting
90
+ facebook_app_id: "アプリID / APIキー"
91
+ facebook_app_secret: "アプリのシークレットキー"
92
+
93
+ # GitHub OAuth Setting
94
+ github_client_id: "Client ID"
95
+ github_secret: "Client Secret"
96
+ ```
97
+
98
+ ## Heroku環境変数設定
99
+
100
+ * Twitter
101
+
102
+ ```
103
+ heroku config:set TWITTER_KEY=xxxxxxxxxx TWITTER_SECRET=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
104
+ ```
105
+
106
+ * Facebook
107
+
108
+ ```
109
+ heroku config:set FACEBOOK_APP_ID=xxxxxxxxxx FACEBOOK_APP_SECRET=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
110
+ ```
111
+
112
+ * GitHub
113
+
114
+ ```
115
+ heroku config:set GITHUB_CLIENT_ID=xxxxxxxxxx GITHUB_SECRET=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
116
+ ```
117
+
68
118
  ## Contributing
69
119
 
70
120
  1. Fork it
@@ -21,12 +21,12 @@
21
21
 
22
22
  # ログインユーザ
23
23
  def current_user
24
- @current_user ||= User.where( id: session[:user_id] ).first
24
+ @current_user ||= User.find_by(id: session[:user_id])
25
25
  end
26
26
  helper_method :current_user
27
27
 
28
- # ユーザ登録チェック
28
+ # ログイン/ユーザ登録済みチェック
29
29
  def signed_in?
30
- User.where( id: session[:user_id] ).exists?
30
+ User.where(id: session[:user_id]).exists?
31
31
  end
32
32
  helper_method :signed_in?
@@ -4,11 +4,14 @@ class SessionsController < ApplicationController
4
4
  # ログイン
5
5
  def callback
6
6
  auth = request.env["omniauth.auth"]
7
+ authentication = Authentication.find_by(provider: auth["provider"], uid: auth["uid"]) || Authentication.create_with_omniauth(auth)
8
+ authentication.auth_update(auth)
7
9
 
8
- user = User.where( provider: auth["provider"], uid: auth["uid"] ).first || User.create_with_omniauth( auth )
9
- user.auth_update( auth )
10
+ # ユーザ作成
11
+ user = User.create_with_auth(authentication)
10
12
 
11
13
  session[:user_id] = user.id
14
+ flash[:notice] = "ログインしました。"
12
15
 
13
16
  # 保管URLへリダイレクト
14
17
  unless session[:request_url].blank?
@@ -17,18 +20,19 @@ class SessionsController < ApplicationController
17
20
  return
18
21
  end
19
22
 
20
- redirect_to :root, notice: "ログインしました。"
23
+ redirect_to :root and return
21
24
  end
22
25
 
23
26
  # ログアウト
24
27
  def destroy
25
28
  session[:user_id] = nil
26
29
 
27
- redirect_to :root, notice: "ログアウトしました。"
30
+ redirect_to :root, notice: "ログアウトしました。" and return
28
31
  end
29
32
 
30
33
  # ログインエラー
31
34
  def failure
32
- render text: "<span style='color: red;'>Auth Failure</span>"
35
+ flash[:alert] = 'Auth Failure'
36
+ redirect_to :root and return
33
37
  end
34
38
  end
@@ -0,0 +1,19 @@
1
+ class CreateAuthentications < ActiveRecord::Migration
2
+ def change
3
+ create_table :authentications do |t|
4
+ t.references :user, index: true
5
+ t.string :provider
6
+ t.string :uid
7
+ t.string :name
8
+ t.string :nickname
9
+ t.string :image
10
+ t.string :email
11
+ t.string :location
12
+ t.string :gender
13
+ t.string :token
14
+ t.string :secret
15
+
16
+ t.timestamps
17
+ end
18
+ end
19
+ end
@@ -1,16 +1,12 @@
1
1
  class CreateUsers < ActiveRecord::Migration
2
2
  def change
3
3
  create_table :users do |t|
4
- t.string :provider
5
- t.string :uid
6
- t.string :name
7
- t.string :nickname
8
- t.string :image
9
- t.string :email
10
- t.string :location
11
- t.string :gender
12
- t.string :token
13
- t.string :secret
4
+ t.string :name
5
+ t.string :image
6
+ t.string :email
7
+ t.boolean :admin_flag, default: false
8
+ t.string :last_login_provider
9
+ t.timestamp :last_login_at
14
10
 
15
11
  t.timestamps
16
12
  end
@@ -0,0 +1,65 @@
1
+ class Authentication < ActiveRecord::Base
2
+ belongs_to :user
3
+
4
+ # バリデーション
5
+ validates :provider, presence: true
6
+ validates :uid, presence: true
7
+
8
+ # auth情報更新
9
+ def auth_update(auth)
10
+ case auth["provider"]
11
+ when "facebook"
12
+ image_path = "https://graph.facebook.com/#{auth['info']['nickname'].presence || auth["uid"]}/picture?width=200&height=200"
13
+ when "twitter"
14
+ image_path = auth["info"]["image"].to_s.gsub('_normal', '') rescue nil
15
+ when "github"
16
+ image_path = "#{auth['info']['image']}&size=200" rescue nil
17
+ end
18
+
19
+ gender = auth["extra"]["raw_info"]["gender"] rescue nil
20
+ location = (auth["info"]["location"].presence || auth["extra"]["raw_info"]["location"]) rescue nil
21
+
22
+ self.name = auth["info"]["name"] if auth["info"]["name"].present?
23
+ self.nickname = auth["info"]["nickname"] if auth["info"]["nickname"].present?
24
+ self.image = image_path if image_path.present?
25
+ self.email = auth["info"]["email"] if auth["info"]["email"].present?
26
+ self.gender = gender if gender.present?
27
+ self.location = location if location.present?
28
+ self.save!
29
+ rescue => e
30
+ puts "[ ---------- e ---------- ]" ; e.tapp ;
31
+ return nil
32
+ end
33
+
34
+ private
35
+
36
+ # auth情報登録/ユーザ作成
37
+ def self.create_with_omniauth(auth)
38
+ # auth情報登録
39
+ authentication = Authentication.new
40
+ authentication.provider = auth["provider"]
41
+ authentication.uid = auth["uid"]
42
+
43
+ if auth["info"].present?
44
+ authentication.name = auth["info"]["name"]
45
+ authentication.nickname = auth["info"]["nickname"]
46
+ authentication.image = auth["info"]["image"]
47
+ authentication.email = auth["info"]["email"]
48
+ authentication.location = auth["info"]["location"]
49
+ end
50
+
51
+ if auth["credentials"].present?
52
+ authentication.token = auth['credentials']['token']
53
+ authentication.secret = auth['credentials']['secret']
54
+ end
55
+
56
+ if auth["extra"].present? and auth["extra"]["raw_info"].present?
57
+ authentication.gender = auth["extra"]["raw_info"]["gender"]
58
+ authentication.location = auth["extra"]["raw_info"]["location"] if authentication.location.blank?
59
+ end
60
+
61
+ authentication.save!
62
+
63
+ return authentication
64
+ end
65
+ end
@@ -1,51 +1,25 @@
1
1
  class User < ActiveRecord::Base
2
- # auth情報更新
3
- def auth_update( auth )
4
- if auth["provider"] == "facebook"
5
- image_path = "https://graph.facebook.com/#{auth['info']['nickname'].presence || auth["uid"]}/picture?width=200&height=200"
6
- else
7
- image_path = auth["info"]["image"]
8
- end
9
-
10
- if self.name != auth["info"]["name"] or self.nickname != auth["info"]["nickname"] or self.image != image_path or self.email != auth["info"]["email"]
11
- self.name = auth["info"]["name"]
12
- self.nickname = auth["info"]["nickname"]
13
- self.image = image_path
14
- self.email = auth["info"]["email"]
15
- self.gender = auth["extra"]["raw_info"]["gender"]
16
- self.location = auth["info"]["location"] || auth["extra"]["raw_info"]["location"]
17
- self.save!
18
- end
19
- end
2
+ has_many :authentications
20
3
 
21
4
  private
22
5
 
23
- # auth情報登録
24
- def self.create_with_omniauth( auth )
6
+ # ユーザ作成
7
+ def self.create_with_auth(authentication)
8
+ # ユーザ作成
25
9
  user = User.new
26
- user.provider = auth["provider"]
27
- user.uid = auth["uid"]
28
-
29
- if auth["info"].present?
30
- user.name = auth["info"]["name"]
31
- user.nickname = auth["info"]["nickname"]
32
- user.image = auth["info"]["image"]
33
- user.email = auth["info"]["email"]
34
- user.location = auth["info"]["location"]
35
- end
36
-
37
- if auth["credentials"].present?
38
- user.token = auth['credentials']['token']
39
- user.secret = auth['credentials']['secret']
40
- end
41
-
42
- if auth["extra"].present? and auth["extra"]["raw_info"].present?
43
- user.gender = auth["extra"]["raw_info"]["gender"]
44
- user.location = auth["extra"]["raw_info"]["location"] if user.location.blank?
45
- end
10
+ user.name = (authentication.nickname.presence || authentication.name)
11
+ user.image = authentication.image if authentication.image.present?
12
+ user.email = authentication.email if authentication.email.present?
13
+ user.last_login_provider = authentication.provider if authentication.provider.present?
14
+ user.last_login_at = Time.now
46
15
 
16
+ # データ保存
47
17
  user.save!
48
18
 
19
+ # auth紐付け
20
+ authentication.user_id = user.id
21
+ authentication.save!
22
+
49
23
  return user
50
24
  end
51
25
  end
@@ -1,7 +1,7 @@
1
1
  # Provider Page
2
- twitter_page: "https://twitter.com/"
2
+ twitter_page: "https://twitter.com/"
3
3
  facebook_page: "https://www.facebook.com/"
4
- github_page: "https://github.com/"
4
+ github_page: "https://github.com/"
5
5
 
6
6
  # アプリケーション名
7
7
  app_name: "YOUR_APP_NAME"
@@ -1,3 +1,3 @@
1
1
  <% if user.present? %>
2
- <%= link_to_unless( user.nickname.blank?, image_tag( user.image, size: size, class: klass ), "#{Settings[user.provider + '_page']}#{user.nickname}", target: "_blank", title: user.nickname, rel: "tooltip", "data-original-title" => user.nickname ) %>
2
+ <%= link_to_unless(user.name.blank?, image_tag( user.image, size: size, class: klass ), "#{Settings[user.last_login_provider + '_page']}#{user.name}", target: "_blank", title: user.name, rel: "tooltip", "data-original-title" => user.name) %>
3
3
  <% end %>
@@ -3,8 +3,8 @@
3
3
  <% if signed_in? %>
4
4
  <%# ログイン済み %>
5
5
  <%= render partial: '/layouts/user_icon', locals: { user: current_user, size: "18x18", klass: "margin_minus2" } %>
6
- <%= link_to( "Top", root_path ) %>
7
- <%= link_to( "Logout", logout_path ) %>
6
+ <%= link_to "Top", root_path %>
7
+ <%= link_to "Logout", logout_path %>
8
8
  <% else %>
9
9
  <%# 未ログイン %>
10
10
  Login:
@@ -14,8 +14,8 @@
14
14
  <% end %>
15
15
 
16
16
  <%# 通知/エラーメッセージ %>
17
- <%= simple_format( flash[:notice], style: "color: green;" ) if flash[:notice].present? %>
18
- <%= simple_format( flash[:alert], style: "color: red;" ) if flash[:alert].present? %>
17
+ <%= simple_format(flash[:notice], style: "color: green;") if flash[:notice].present? %>
18
+ <%= simple_format(flash[:alert], style: "color: red;") if flash[:alert].present? %>
19
19
 
20
20
  <%= yield %>
21
21
 
@@ -1,7 +1,7 @@
1
1
  module Minimum
2
2
  module Omniauth
3
3
  module Scaffold
4
- VERSION = "0.1.6"
4
+ VERSION = "0.1.7"
5
5
  end
6
6
  end
7
7
  end
@@ -1,7 +1,5 @@
1
1
  require "minimum/omniauth/scaffold/version"
2
-
3
2
  require 'rails/generators'
4
-
5
3
  require 'rails_config'
6
4
  require 'omniauth-twitter'
7
5
  require 'omniauth-facebook'
@@ -63,11 +61,13 @@ module Minimum
63
61
  copy_file( "#{@@template_path}/locales/ja.yml", "config/locales/ja.yml" )
64
62
  copy_file( "#{@@template_path}/locales/translation_ja.yml", "config/locales/translation_ja.yml" )
65
63
 
66
- # ----- create_users.rb ----- #
64
+ # ----- migration ----- #
67
65
  copy_file( "#{@@template_path}/migrate/create_users.rb", "db/migrate/20000101000000_create_users.rb" )
66
+ copy_file( "#{@@template_path}/migrate/create_authentications.rb", "db/migrate/20000102000000_create_authentications.rb" )
68
67
 
69
68
  # ----- models ----- #
70
69
  copy_file( "#{@@template_path}/models/user.rb", "app/models/user.rb" )
70
+ copy_file( "#{@@template_path}/models/authentication.rb", "app/models/authentication.rb" )
71
71
 
72
72
  # ----- controllers ----- #
73
73
  content = File.read( "#{@@template_path}/controllers/application_controller.rb", encoding: Encoding::UTF_8 )
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: minimum-omniauth-scaffold
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - shu0115
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-03-29 00:00:00.000000000 Z
11
+ date: 2013-11-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -142,7 +142,9 @@ files:
142
142
  - lib/minimum/omniauth/scaffold/templates/initializers/omniauth.rb
143
143
  - lib/minimum/omniauth/scaffold/templates/locales/ja.yml
144
144
  - lib/minimum/omniauth/scaffold/templates/locales/translation_ja.yml
145
+ - lib/minimum/omniauth/scaffold/templates/migrate/create_authentications.rb
145
146
  - lib/minimum/omniauth/scaffold/templates/migrate/create_users.rb
147
+ - lib/minimum/omniauth/scaffold/templates/models/authentication.rb
146
148
  - lib/minimum/omniauth/scaffold/templates/models/user.rb
147
149
  - lib/minimum/omniauth/scaffold/templates/rails_config/development.yml
148
150
  - lib/minimum/omniauth/scaffold/templates/rails_config/production.yml
@@ -176,7 +178,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
176
178
  version: '0'
177
179
  requirements: []
178
180
  rubyforge_project:
179
- rubygems_version: 2.0.0
181
+ rubygems_version: 2.0.3
180
182
  signing_key:
181
183
  specification_version: 4
182
184
  summary: Minimum OmniAuth Scaffold