Class: Socials::Generators::DeviseGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/socials/devise_generator.rb

Instance Method Summary collapse

Instance Method Details

Add the HTML with the partial Social Logins



67
68
69
70
# File 'lib/generators/socials/devise_generator.rb', line 67

def add_oauth_partial_links
  template "views/users/shared/_link.html.slim", "app/views/users/shared/_link.html.slim"
  puts 'Check your app/views/users/shared/_link.html.slim to custom the OAuth links'.colorize(:light_green)
end

#copy_initializerObject



8
9
10
11
12
13
14
15
# File 'lib/generators/socials/devise_generator.rb', line 8

def copy_initializer
  update_routes
  update_devise_rb
  update_devise_user
  update_application_rb
  add_oauth_partial_links
  update_application_controller
end

#update_application_controllerObject

Add Devise auth & validations to the Main top Controller



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/generators/socials/devise_generator.rb', line 112

def update_application_controller
  inject_into_file 'app/controllers/application_controller.rb', after: "protect_from_forgery with: :exception\n" do "  before_action :authenticate_or_token\n\n  protected\n    def configure_permitted_parameters\ndevise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:locale, :name, :username, :email, :password, :password_confirmation, :role, :remember_me) }\ndevise_parameter_sanitizer.for(:sign_in) { |u| u.permit(:login, :username, :email, :password, :remember_me) }\ndevise_parameter_sanitizer.for(:account_update) { |u| u.permit(:username, :email, :password, :password_confirmation, :current_password, :role) }\nI18n.locale = @current_user.locale || I18n.default_locale unless @current_user.nil?\n    end\n\n    # Validate user session if is not API call\n    def authenticate_or_token\nauthenticate_user! if params[:controller].index('api').nil? && request.fullpath != root_path\n@current_user = current_user if @current_user.nil?\n    end\n\n    RUBY\n  end\nend\n"

#update_application_rbObject

Update the application.rb to load the socials_keys on initialize



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/generators/socials/devise_generator.rb', line 28

def update_application_rb
  inject_into_file 'config/application.rb', after: "class Application < Rails::Application\n" do "\n    # It setup your social apps\n    social_keys = File.join(Rails.root, 'config', 'social_keys.yml')\n    CONFIG = HashWithIndifferentAccess.new(YAML::load(IO.read(social_keys)))[Rails.env]\n\n    unless CONFIG.nil?\nCONFIG.each do |k,v|\n  ENV[k.upcase] ||= v\nend\n    end\n\n    RUBY\n  end\n\n  puts 'Just updated your config/initializers/application.rb to config the environment'.colorize(:light_blue)\nend\n"

#update_devise_rbObject

Add the ENV KEYs based on the social_keys.YML



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/generators/socials/devise_generator.rb', line 48

def update_devise_rb
  inject_into_file 'config/initializers/devise.rb', after: "config.sign_out_via = :delete\n" do "\n  # Config Social Keys to create the SignUps\n  config.sign_out_via = :get\n  config.omniauth :facebook, ENV[\"FACEBOOK_KEY\"], ENV[\"FACEBOOK_SECRET\"], { :scope => 'email, offline_access', :client_options => {:ssl => {:ca_file => '/usr/lib/ssl/certs/ca-certificates.crt'}}}\n  config.omniauth :twitter, ENV[\"TWITTER_KEY\"], ENV[\"TWITTER_SECRET\"], { :scope => 'r_fullprofile, r_emailaddress', :client_options => {:ssl => {:ca_file => '/usr/lib/ssl/certs/ca-certificates.crt'}}}\n  config.omniauth :linkedin, ENV[\"LINKEDIN_KEY\"], ENV[\"LINKEDIN_SECRET\"], { :scope => 'r_fullprofile r_emailaddress', :client_options => {:ssl => {:ca_file => '/usr/lib/ssl/certs/ca-certificates.crt'}}}\n  config.omniauth :github, ENV['GITHUB_KEY'], ENV['GITHUB_SECRET'], scope: \"user, public_repo\"\n  config.omniauth :google_oauth2, ENV['GOOGLE_KEY'], ENV['GOOGLE_SECRET'], {}\n\n    RUBY\n  end\n\n  puts 'Check your config/initializers/devise.rb which was updated to have the Social Keys used (OmniAuth linked to devise)'.colorize(:light_green)\n  puts 'UPDATE your config/initializers/devise.rb if you need more data from the user, CHANGING the: SCOPE'.colorize(:light_yellow)\nend\n"

#update_devise_userObject

Update the DeviseUser to execute OAuth correctly



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/generators/socials/devise_generator.rb', line 73

def update_devise_user
  inject_into_file 'app/models/user.rb', after: ":validatable" do ", :omniauthable\n  validates_presence_of :email\n  has_many :authorizations\n\n  def self.new_with_session(params,session)\n    if session[\"devise.user_attributes\"]\nnew(session[\"devise.user_attributes\"],without_protection: true) do |user|\n  user.attributes = params\n  user.valid?\nend\n    else\nsuper\n    end\n  end\n\n  def self.from_omniauth(auth, current_user)\n    authorization = Authorization.where(:provider => auth.provider, :uid => auth.uid.to_s, :token => auth.credentials.token, :secret => auth.credentials.secret).first_or_initialize\n    if authorization.user.blank?\nuser = current_user.nil? ? User.where('email = ?', auth[\"info\"][\"email\"]).first : current_user\nif user.blank?\n user = User.new\n user.password = Devise.friendly_token[0,10]\n user.name = auth.info.name\n user.email = auth.info.email\n auth.provider == \"twitter\" ?  user.save(:validate => false) :  user.save\n     end\n     authorization.username = auth.info.nickname\n     authorization.user_id = user.id\n     authorization.save\n   end\n   authorization.user\n  end\n    RUBY\n  end\nend\n"

#update_routesObject

Add the Devise+OAuth Routes



18
19
20
21
22
23
24
25
# File 'lib/generators/socials/devise_generator.rb', line 18

def update_routes
  inject_into_file 'config/routes.rb', after: "devise_for :users" do ", controllers: { omniauth_callbacks: \"omniauth_callbacks\" }\n    RUBY\n  end\n\n  puts 'Check out your config/routes.rb where the devise OAuth route was created'.colorize(:light_green)\nend\n"