Class: Authentication::Generators::OmniauthGenerator
- Inherits:
-
Rails::Generators::Base
- Object
- Rails::Generators::Base
- Authentication::Generators::OmniauthGenerator
- Defined in:
- lib/generators/authentication/omniauth/omniauth_generator.rb
Instance Method Summary collapse
- #add_gems ⇒ Object
- #add_helper_methods ⇒ Object
- #add_routes ⇒ Object
- #add_translations ⇒ Object
- #copy_configuration ⇒ Object
- #copy_controller_files ⇒ Object
- #copy_omniauth_configuration ⇒ Object
- #copy_warden_file ⇒ Object
- #copy_warden_strategies ⇒ Object
- #generate_user ⇒ Object
- #instructions ⇒ Object
Instance Method Details
#add_gems ⇒ Object
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_methods ⇒ Object
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_routes ⇒ Object
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_translations ⇒ Object
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_configuration ⇒ Object
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_files ⇒ Object
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_configuration ⇒ Object
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_file ⇒ Object
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_strategies ⇒ Object
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_user ⇒ Object
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 |
#instructions ⇒ Object
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 = "There are a few manual steps that you need to take care of\n\n" << "1. Add an omniauth provider gem like twitter, facebook, etc..\n" << "2. Modify config/initializers/omniauth.rb and setup your provider\n" << " and your provider credentials.\n" << "3. Run bundle command to install new gems.\n" << "4. If you want to restrict access to a specific email domain.\n" << " modify config/initializers/authentication_domain.rb and add \n" << " your allowed domain.\n" << "5. Inspect warden initializer at config/initializers/warden.rb\n" << " and update the failure_app.\n" << "6. Be sure that to have definition for root in your routes.\n" << "7. Run rake db:migrate to add your #{resource_pluralize} table.\n" << "8. Inspect generated files and learn how authentication was implemented.\n\n" puts end |