socials 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +1 -1
- data/README.md +15 -13
- data/lib/generators/socials/devise_generator.rb +74 -3
- data/lib/generators/socials/install_generator.rb +6 -0
- data/lib/generators/templates/tasks/recreate.rake +18 -0
- data/lib/generators/templates/views/users/shared/_links.html.slim +24 -0
- data/lib/socials/devise.rb +4 -1
- data/lib/socials/version.rb +1 -1
- metadata +3 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5acab175fca6a73cf2aa9d47c8abff30efe96cb1
|
4
|
+
data.tar.gz: 8044adfe7471056dc05643215f42fcb4cc18eccc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 248ed6c8216447e0b2cbc13985477f3cacc50c554905a1f47f9bb8d84c366716642d35d4b3b85379e4d0debef2633c9ec2c76b621b4b98c8df3ab981fb5884dd
|
7
|
+
data.tar.gz: 1d6938334f729d0a74d25ee3aaaa1de0ca1d955a3ff3ad5022e2ebd641402d77ba51e797b14da31e6f05f7f326ee07a377614a5254c22c04981bbe70aff9f35f
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,28 +1,30 @@
|
|
1
1
|
# Socials
|
2
2
|
|
3
|
-
|
3
|
+
## Installation
|
4
4
|
|
5
|
-
|
5
|
+
First install the Rails :gem:
|
6
6
|
|
7
|
-
|
7
|
+
$ gem install 'rails'
|
8
|
+
|
9
|
+
Create your new Rails app:
|
10
|
+
|
11
|
+
$ rails new app_name
|
8
12
|
|
9
|
-
|
13
|
+
Install the Socials :gem:
|
10
14
|
|
11
|
-
|
12
|
-
gem 'socials'
|
13
|
-
```
|
15
|
+
$ gem install 'socials'
|
14
16
|
|
15
|
-
|
17
|
+
Run the social generator:
|
16
18
|
|
17
|
-
$
|
19
|
+
$ rails g socials:install
|
18
20
|
|
19
|
-
|
21
|
+
It updates your Gemfile :page_facing_up: so run the bundle
|
20
22
|
|
21
|
-
$
|
23
|
+
$ bundle install
|
22
24
|
|
23
|
-
|
25
|
+
Now just let's link the Devise with the OAuth :boom:
|
24
26
|
|
25
|
-
|
27
|
+
$ rake socials:devise
|
26
28
|
|
27
29
|
## Development
|
28
30
|
|
@@ -8,13 +8,16 @@ module Socials
|
|
8
8
|
def copy_initializer
|
9
9
|
update_routes
|
10
10
|
update_devise_rb
|
11
|
+
update_devise_user
|
11
12
|
update_application_rb
|
13
|
+
add_oauth_partial_links
|
14
|
+
update_application_controller
|
12
15
|
end
|
13
16
|
|
14
17
|
# Add the Devise+OAuth Routes
|
15
18
|
def update_routes
|
16
|
-
inject_into_file 'config/routes.rb', after: "
|
17
|
-
|
19
|
+
inject_into_file 'config/routes.rb', after: "devise_for :users" do <<-'RUBY'
|
20
|
+
, controllers: { omniauth_callbacks: "omniauth_callbacks" }
|
18
21
|
RUBY
|
19
22
|
end
|
20
23
|
|
@@ -53,12 +56,80 @@ module Socials
|
|
53
56
|
config.omniauth :github, ENV['GITHUB_KEY'], ENV['GITHUB_SECRET'], scope: "user, public_repo"
|
54
57
|
config.omniauth :google_oauth2, ENV['GOOGLE_KEY'], ENV['GOOGLE_SECRET'], {}
|
55
58
|
|
56
|
-
|
59
|
+
RUBY
|
57
60
|
end
|
58
61
|
|
59
62
|
puts 'Check your config/initializers/devise.rb which was updated to have the Social Keys used (OmniAuth linked to devise)'.colorize(:light_green)
|
60
63
|
puts 'UPDATE your config/initializers/devise.rb if you need more data from the user, CHANGING the: SCOPE'.colorize(:light_yellow)
|
61
64
|
end
|
65
|
+
|
66
|
+
# Add the HTML with the partial Social Logins
|
67
|
+
def add_oauth_partial_links
|
68
|
+
template "views/users/shared/_link.html.slim", "app/views/users/shared/_link.html.slim"
|
69
|
+
puts 'Check your app/views/users/shared/_link.html.slim to custom the OAuth links'.colorize(:light_green)
|
70
|
+
end
|
71
|
+
|
72
|
+
# Update the DeviseUser to execute OAuth correctly
|
73
|
+
def update_devise_user
|
74
|
+
inject_into_file 'app/models/user.rb', after: ":validatable" do <<-'RUBY'
|
75
|
+
, :omniauthable
|
76
|
+
validates_presence_of :email
|
77
|
+
has_many :authorizations
|
78
|
+
|
79
|
+
def self.new_with_session(params,session)
|
80
|
+
if session["devise.user_attributes"]
|
81
|
+
new(session["devise.user_attributes"],without_protection: true) do |user|
|
82
|
+
user.attributes = params
|
83
|
+
user.valid?
|
84
|
+
end
|
85
|
+
else
|
86
|
+
super
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
def self.from_omniauth(auth, current_user)
|
91
|
+
authorization = Authorization.where(:provider => auth.provider, :uid => auth.uid.to_s, :token => auth.credentials.token, :secret => auth.credentials.secret).first_or_initialize
|
92
|
+
if authorization.user.blank?
|
93
|
+
user = current_user.nil? ? User.where('email = ?', auth["info"]["email"]).first : current_user
|
94
|
+
if user.blank?
|
95
|
+
user = User.new
|
96
|
+
user.password = Devise.friendly_token[0,10]
|
97
|
+
user.name = auth.info.name
|
98
|
+
user.email = auth.info.email
|
99
|
+
auth.provider == "twitter" ? user.save(:validate => false) : user.save
|
100
|
+
end
|
101
|
+
authorization.username = auth.info.nickname
|
102
|
+
authorization.user_id = user.id
|
103
|
+
authorization.save
|
104
|
+
end
|
105
|
+
authorization.user
|
106
|
+
end
|
107
|
+
RUBY
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
# Add Devise auth & validations to the Main top Controller
|
112
|
+
def update_application_controller
|
113
|
+
inject_into_file 'app/controllers/application_controller.rb', after: "protect_from_forgery with: :exception\n" do <<-'RUBY'
|
114
|
+
before_action :authenticate_or_token
|
115
|
+
|
116
|
+
protected
|
117
|
+
def configure_permitted_parameters
|
118
|
+
devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:locale, :name, :username, :email, :password, :password_confirmation, :role, :remember_me) }
|
119
|
+
devise_parameter_sanitizer.for(:sign_in) { |u| u.permit(:login, :username, :email, :password, :remember_me) }
|
120
|
+
devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:username, :email, :password, :password_confirmation, :current_password, :role) }
|
121
|
+
I18n.locale = @current_user.locale || I18n.default_locale unless @current_user.nil?
|
122
|
+
end
|
123
|
+
|
124
|
+
# Validate user session if is not API call
|
125
|
+
def authenticate_or_token
|
126
|
+
authenticate_user! if params[:controller].index('api').nil? && request.fullpath != root_path
|
127
|
+
@current_user = current_user if @current_user.nil?
|
128
|
+
end
|
129
|
+
|
130
|
+
RUBY
|
131
|
+
end
|
132
|
+
end
|
62
133
|
end
|
63
134
|
end
|
64
135
|
end
|
@@ -20,6 +20,9 @@ module Socials
|
|
20
20
|
template 'tasks/socials.rake', 'lib/tasks/socials.rake'
|
21
21
|
puts 'Just created the socials rake tasks, check it on the GitHub README'.colorize(:light_blue)
|
22
22
|
|
23
|
+
template 'tasks/recreate.rake', 'lib/tasks/recreate.rake'
|
24
|
+
puts 'Now you can easy rebase (clean up) your DB using rake db:recreate'.colorize(:light_green)
|
25
|
+
|
23
26
|
# Add the OAuth Controller
|
24
27
|
template "controllers/omniauth_callbacks_controller.rb", "app/controllers/omniauth_callbacks_controller.rb"
|
25
28
|
puts 'Check out you your app/controllers/omniauth_callbacks_controller.rb which persist the social user through devise'.colorize(:light_green)
|
@@ -33,6 +36,9 @@ module Socials
|
|
33
36
|
# Add dependency GEMs & run the bundle install
|
34
37
|
def update_gemfile
|
35
38
|
inject_into_file 'Gemfile', after: "source 'https://rubygems.org'\n" do <<-'RUBY'
|
39
|
+
# Easier & faster then ERB
|
40
|
+
gem 'slim-rails', '~> 2.1.5'
|
41
|
+
|
36
42
|
# For easy user session management
|
37
43
|
gem 'devise', '~> 3.4.1'
|
38
44
|
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'colorize'
|
2
|
+
|
3
|
+
namespace :db do
|
4
|
+
desc 'Populate the Database'
|
5
|
+
task :recreate => :environment do
|
6
|
+
puts 'DBs creation'.colorize(:yellow)
|
7
|
+
Rake::Task['db:drop'].invoke
|
8
|
+
Rake::Task['db:create'].invoke
|
9
|
+
|
10
|
+
puts 'Development Env'.colorize(:blue)
|
11
|
+
Rake::Task['db:migrate'].invoke
|
12
|
+
Rake::Task['db:seed'].invoke
|
13
|
+
|
14
|
+
puts 'Test Env'.colorize(:green)
|
15
|
+
system('rake db:migrate RAILS_ENV=test')
|
16
|
+
system('rake db:seed RAILS_ENV=test')
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
- if controller_name != 'sessions'
|
2
|
+
=link_to "Sign in", new_session_path(resource_name)
|
3
|
+
br
|
4
|
+
|
5
|
+
-if devise_mapping.registerable? && controller_name != 'registrations'
|
6
|
+
=link_to "Sign up", new_registration_path(resource_name)
|
7
|
+
br
|
8
|
+
|
9
|
+
-if devise_mapping.recoverable? && controller_name != 'passwords'
|
10
|
+
=link_to "Forgot your password?", new_password_path(resource_name)
|
11
|
+
br
|
12
|
+
|
13
|
+
-if devise_mapping.confirmable? && controller_name != 'confirmations'
|
14
|
+
=link_to "Didn't receive confirmation instructions?", new_confirmation_path(resource_name)
|
15
|
+
br
|
16
|
+
|
17
|
+
-if devise_mapping.lockable? && resource_class.unlock_strategy_enabled?(:email) && controller_name != 'unlocks'
|
18
|
+
=link_to "Didn't receive unlock instructions?", new_unlock_path(resource_name)
|
19
|
+
br
|
20
|
+
|
21
|
+
-if devise_mapping.omniauthable?
|
22
|
+
-resource_class.omniauth_providers.each do |provider|
|
23
|
+
=link_to "Sign in with #{provider.to_s.titleize}", omniauth_authorize_path(resource_name, provider)
|
24
|
+
br
|
data/lib/socials/devise.rb
CHANGED
@@ -8,8 +8,11 @@ module Socials
|
|
8
8
|
puts 'Running `rails g devise User`'.light_green
|
9
9
|
system 'rails g devise User'
|
10
10
|
|
11
|
+
puts 'Running `rails g devise:views`'.light_green
|
12
|
+
system 'rails g devise:views'
|
13
|
+
|
11
14
|
puts 'Running `rake db:migrate` for the devise User created'.light_blue
|
12
|
-
system 'rake db:
|
15
|
+
system 'rake db:recreate'
|
13
16
|
end
|
14
17
|
|
15
18
|
# Update the devise to load the OmNiAuth gem
|
data/lib/socials/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: socials
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ilton Garcia dos Santos Silveira
|
@@ -232,7 +232,9 @@ files:
|
|
232
232
|
- lib/generators/socials/install_generator.rb
|
233
233
|
- lib/generators/templates/config/social_keys.yml
|
234
234
|
- lib/generators/templates/controllers/omniauth_callbacks_controller.rb
|
235
|
+
- lib/generators/templates/tasks/recreate.rake
|
235
236
|
- lib/generators/templates/tasks/socials.rake
|
237
|
+
- lib/generators/templates/views/users/shared/_links.html.slim
|
236
238
|
- lib/socials.rb
|
237
239
|
- lib/socials/devise.rb
|
238
240
|
- lib/socials/version.rb
|