module Infinum module Omniauth module Generators class AuthGenerator < Rails::Generators::Base source_root File.expand_path("../templates", __FILE__) class_option :no_warnings, type: :boolean, default: false, banner: 'Turn off warnings' #def add_auth_helper # methods = "\n"\ # " def sign_in(user)\n"\ # " session[:user_id] = 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_in\n"\ # " \n"\ # " def sign_out\n"\ # " session[:user_id] = nil\n"\ # " end # sign_out\n" # # append_to_file "app/helpers/application_helper.rb", methods, # after: Regexp.new("^.*" + Regexp.escape("module ApplicationHelper") + ".*$", Regexp::IGNORECASE) #end # add_auth_helper 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 # add_auth_controller 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 # warn_user ############### ### Helpers ### ############### private end # InstallGenerator end # Generators end # Omniauth end # Infinum