Class: Infinum::Omniauth::Generators::AuthGenerator

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

Instance Method Summary collapse

Instance Method Details

#add_auth_controllerObject

def add_auth_helper methods = “n”\ “ def sign_in(user)n”\ “ session = OmniAuth::AuthHash.new({n”\ “ :provider => ‘infinum’,n”\ “ :uid => (user.try(:uid) || ‘75’),n”\ “ :extra => {n”\ “ :first_name => user.try(:first_name),n”\ “ :last_name => user.try(:last_name), n”\ “ :email => user.try(:email),n”\ “ :avatar_url => user.try(:avatar_url)n”\ “ }n”\ “ }) n”\ “ end # sign_inn”\ “ n”\ “ def sign_outn”\ “ session = niln”\ “ end # sign_outn”

append_to_file “app/helpers/application_helper.rb”, methods, after: Regexp.new(“^.*” + Regexp.escape(“module ApplicationHelper”) + “.*$”, Regexp::IGNORECASE) end # add_auth_helper



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/generators/infinum/omniauth/auth_generator.rb', line 33

def add_auth_controller
  methods =  "\n"\
        "\n"\
        " before_action :authenticate_user!"\
        "\n"\
        " helper_method :current_user\n"\
        " helper_method :authenticate_user!\n"\
        "\n"\
        " def authenticate_user!\n"\
        "   if current_user.blank?\n"\
        "     respond_to do |format|\n"\
        "       format.html  {\n"\
        "         redirect_to \"/auth/infinum?origin=\#{request.url}\"\n"\
        "       }\n"\
        "       format.json {\n"\
        "         render :json => { 'error' => 'Access Denied' }.to_json\n"\
        "       }\n"\
        "     end\n"\
        "   end\n"\
        " end\n"\
        "\n"\
        " def current_user\n"\
        "   return nil unless session[:user_id]\n"\
        "   @current_user ||= USER_MODEL.new_from_omniauth(session[:user_id])\n"\
        " end\n"

  append_to_file   "app/controllers/application_controller.rb", methods,
          after: Regexp.new("^.*" + Regexp.escape("class ApplicationController") + ".*$", Regexp::IGNORECASE)
end

#warn_userObject

add_auth_controller



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/generators/infinum/omniauth/auth_generator.rb', line 63

def warn_user
  return nil if options[:no_warnings]
  say "To complete this process you have to add the following"
  say "method to your user model:"
  say ""
  say "  def self.new_from_omniauth(omniauth)"
      say "    user = self.find_by_uid(omniauth['uid']) || self.find_by_email(omniauth[:extra][:email]) || self.new(:uid => omniauth['uid'])"
      say "      user.uid = omniauth['uid']"
      say "      user.first_name = omniauth[:extra][:first_name]"
      say "      user.last_name = omniauth[:extra][:last_name]"
      say "      user.email = omniauth[:extra][:email]"
      say "      user.avatar_url = omniauth[:extra][:avatar_url]"
      say "      user.save"
      say "    user"
      say "  end"
      say ""
      say "Remember to teplace USER_MODEL with the name of your user model"
      say "in the ApplicationControllar and ApplicationHelper"
      say ""
      say "This configuration can also be done by calling:"
      say "  rails generate omniauth_infinum:config"
      say ""
end