Class: AuthenticationsController

Inherits:
ApplicationController
  • Object
show all
Defined in:
lib/generators/gunnertechnology/project/templates/app/controllers/authentications_controller.rb

Instance Method Summary collapse

Instance Method Details

#build_omniauth(faux_ominauth) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/generators/gunnertechnology/project/templates/app/controllers/authentications_controller.rb', line 7

def build_omniauth(faux_ominauth)
  pieces = faux_ominauth.split("~~~")
  {
    'provider' => pieces[0],
    'uid' => pieces[1],
    'credentials' => {
      'token' => pieces[2],
      'secret' => pieces[3],
    },
    'user_info' => {
      'nickname' => pieces[4]
    }
  }
end

#createObject



22
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/generators/gunnertechnology/project/templates/app/controllers/authentications_controller.rb', line 22

def create
  omniauth = params[:faux_ominauth].nil? ? request.env["omniauth.auth"] : build_omniauth(params[:faux_ominauth])
  if omniauth
    # THIS IS A MESS. WE CAN'T CARRY PARAMETERS THROUGH TO THE NETWORK, SO WE HAVE TO SET A SESSION VARIABLE. BUT, WE HAVE TO IGNORE THAT SESSION VARIABLE IF THE USER
    # IS TRYING TO CONNECT FACEBOOOK FROM THEIR RETAILER PAGE. BUT IF ARE TRYING TO CONNECT TO A FACEBOOK PAGE, WE NEED TO HONOR THE SESSION VARIABLE
    type = omniauth['provider'] == 'facebook' && params[:faux_ominauth].nil? ? 'User' : (params[:auth_type] || session[:auth_type] || 'User')
    uid = (omniauth['uid']||omniauth['extra']['access_token'].params['member_id'])
    authentication = Authentication.find_by_provider_and_authenticationable_type_and_uid(omniauth['provider'], type, uid)
    
    if type == 'Retailer'
      retailer_id = params[:auth_type_id] || session[:retailer_id]
      session[:auth_type] = nil
      session[:retailer_id] = nil
      if !retailer_id
        redirect_to root_url
      end
      
      retailer = Retailer.find(retailer_id)
      if authentication
        flash[:notice] = "You already added that account"
      else          
        retailer.authentications.create!(:nickname => omniauth['user_info']['nickname'], :provider => omniauth['provider'], :uid => uid, :token => omniauth['credentials']['token'], :secret => omniauth['credentials']['secret'])
      end
      redirect_to edit_retailer_url(retailer)
    else
      if authentication
        flash[:notice] = "Signed in successfully."
        (:user, authentication.authenticationable)
      elsif current_user
        current_user.authentications.create!(:nickname => omniauth['user_info']['nickname'], :provider => omniauth['provider'], :uid => uid, :token => omniauth['credentials']['token'], :secret => omniauth['credentials']['secret'])
        flash[:notice] = "Authentication successful." 
        
        if retailer_id = session[:retailer_id]
          session[:auth_type] = nil
          session[:retailer_id] = nil
          
          redirect_to edit_retailer_url(retailer_id)
        else
          redirect_to authentications_url  
        end
      else
        user = User.new  
        user.apply_omniauth(omniauth,params[:user])  
        if user.save
          flash[:notice] = "Signed in successfully."  
          (:user, user)  
        else  
          session[:omniauth] = omniauth.except('extra')  
          redirect_to new_user_registration_url  
        end
      end
    end
  else
    redirect_to root_url
  end
end

#destroyObject



79
80
81
82
83
84
# File 'lib/generators/gunnertechnology/project/templates/app/controllers/authentications_controller.rb', line 79

def destroy  
  @authentication = current_user.authentications.find(params[:id])  
  @authentication.destroy  
  flash[:notice] = "Successfully destroyed authentication."  
  redirect_to authentications_url  
end

#indexObject



3
4
5
# File 'lib/generators/gunnertechnology/project/templates/app/controllers/authentications_controller.rb', line 3

def index  
  @authentications = current_user.authentications if current_user
end