Class: Authentication::Generators::OmniauthGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/authentication/omniauth/omniauth_generator.rb

Instance Method Summary collapse

Instance Method Details

#add_gemsObject



55
56
57
58
# File 'lib/generators/authentication/omniauth/omniauth_generator.rb', line 55

def add_gems
  gem 'warden', '~> 1.2.0'
  gem 'omniauth'
end

#add_helper_methodsObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/generators/authentication/omniauth/omniauth_generator.rb', line 23

def add_helper_methods
  insert_into_file 'app/controllers/application_controller.rb', after: /:exception/ do
    <<-EOS


  helper_method :current_#{resource_name}, :#{resource_name}_signed_in?, :warden_message

protected
  def current_#{resource_name}
    warden.user(scope: :#{resource_name})
  end

  def #{resource_name}_signed_in?
    warden.authenticate?(scope: :#{resource_name})
  end

  def authenticate!
    redirect_to root_path, notice: t('.not_logged') unless #{resource_name}_signed_in?
  end

  def warden_message
    warden.message
  end

  def warden
    request.env['warden']
  end
    EOS
  end

end

#add_routesObject



11
12
13
14
# File 'lib/generators/authentication/omniauth/omniauth_generator.rb', line 11

def add_routes
  route "get 'auth/:provider/callback' => 'sessions#create', as: :log_in"
  route "delete '/sessions/destroy' => 'sessions#destroy', as: :log_out"
end

#add_translationsObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/generators/authentication/omniauth/omniauth_generator.rb', line 60

def add_translations
  insert_into_file "config/locales/en.yml", after: 'en:' do
  <<-EOS

  sessions:
new:
  log_in: 'Log in'
create:
  unauthorized_domain: 'Sorry but your domain is not authorized'
  logged_in: 'Welcome back!'
destroy:
  logged_out: 'See you later!'
  EOS
  end
end

#copy_configurationObject



80
81
82
# File 'lib/generators/authentication/omniauth/omniauth_generator.rb', line 80

def copy_configuration
  template 'authentication_domain.rb', File.join('config', 'initializers', 'authentication_domain.rb')
end

#copy_controller_filesObject



7
8
9
# File 'lib/generators/authentication/omniauth/omniauth_generator.rb', line 7

def copy_controller_files
  template 'sessions_controller.rb', 'app/controllers/sessions_controller.rb'
end

#copy_omniauth_configurationObject



84
85
86
# File 'lib/generators/authentication/omniauth/omniauth_generator.rb', line 84

def copy_omniauth_configuration
  template 'omniauth.rb', File.join('config', 'initializers', 'omniauth.rb')
end

#copy_warden_fileObject



76
77
78
# File 'lib/generators/authentication/omniauth/omniauth_generator.rb', line 76

def copy_warden_file
  template 'warden.rb', File.join('config', 'initializers', 'warden.rb')
end

#copy_warden_strategiesObject



88
89
90
# File 'lib/generators/authentication/omniauth/omniauth_generator.rb', line 88

def copy_warden_strategies
  template 'oauth_authentication.rb', File.join('lib', 'strategies', 'oauth_authentication.rb')
end

#generate_userObject



16
17
18
19
20
21
# File 'lib/generators/authentication/omniauth/omniauth_generator.rb', line 16

def generate_user
  if Dir["db/migrate/*create_#{resource_pluralize}.rb"].empty?
    template 'create_identities.rb', "db/migrate/#{migration_name}"
  end
  template 'identity.rb', "app/models/#{resource_name}.rb"
end

#instructionsObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/generators/authentication/omniauth/omniauth_generator.rb', line 92

def instructions
  message = "There are a few manual steps that you need to take care of\n\n"
  message << "1. Add an omniauth provider gem like twitter, facebook, etc..\n"
  message << "2. Modify config/initializers/omniauth.rb and setup your provider\n"
  message << "   and your provider credentials.\n"
  message << "3. Run bundle command to install new gems.\n"
  message << "4. If you want to restrict access to a specific email domain.\n"
  message << "   modify config/initializers/authentication_domain.rb and add \n"
  message << "   your allowed domain.\n"
  message << "5. Inspect warden initializer at config/initializers/warden.rb\n"
  message << "   and update the failure_app.\n"
  message << "6. Be sure that to have definition for root in your routes.\n"
  message << "7. Run rake db:migrate to add your #{resource_pluralize} table.\n"
  message << "8. Inspect generated files and learn how authentication was implemented.\n\n"

  puts message
end