Module: ClickfunnelsAuth::ControllerHelper

Extended by:
ActiveSupport::Concern
Included in:
ApplicationController
Defined in:
lib/clickfunnels_auth/controller_helper.rb

Instance Method Summary collapse

Instance Method Details

#auth_redirectObject



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/clickfunnels_auth/controller_helper.rb', line 46

def auth_redirect
  origin = "#{request.protocol}#{request.host_with_port}#{request.fullpath}"
  # Currently Doorkeeper has a bug when the redirct contains query params, so for now
  # we'll put the origin in the session instead of the redirect url.
  #observable_redirect_to "/auth/clickfunnels?origin=#{CGI.escape(origin)}"
  session['origin'] = origin
  if ENV['ENABLE_FAKE_AUTH'] == 'true'
    observable_redirect_to "/fake_auth"
  else
    observable_redirect_to "/auth/clickfunnels"
  end
end


14
15
16
17
18
# File 'lib/clickfunnels_auth/controller_helper.rb', line 14

def check_cookie
  if !cookie_valid?
    session[:user_id] = nil
  end
end

Returns:

  • (Boolean)


20
21
22
# File 'lib/clickfunnels_auth/controller_helper.rb', line 20

def cookie_valid?
  cookies[:clickfunnels_login_user].present? && session[:user_id].present? && cookies[:clickfunnels_login_user].to_s == session[:user_id].to_s
end

#current_userObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/clickfunnels_auth/controller_helper.rb', line 59

def current_user
  return nil unless session[:user_id]
  @current_user ||= User.find_by_id(session[:user_id])
  token = @current_user.access_tokens.first
  puts "token = #{token}"
  puts "token.expired? = #{token.try :expired?}"
  if token.blank?
    puts "*******************************************************"
    puts "we had a user, but they did not have a token!"
    puts "*******************************************************"
    session[:user_id] = nil
    return nil
  elsif token.expired? || is_token_older_than_current_login?(token)
    begin
      puts "*******************************************************"
      puts "aobut to refresh the token!"
      puts "token.expired? : #{token.expired?}"
      puts "is_token_older_than_current_login?(token) : #{is_token_older_than_current_login?(token)}"
      puts "*******************************************************"
      token.refresh!
    rescue OAuth2::Error => e
      puts "caught error #{e}"
      token.destroy!
      session[:user_id] = nil
      return nil
    end
  end
  return @current_user
end

#is_token_older_than_current_login?(token) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
40
41
42
43
44
# File 'lib/clickfunnels_auth/controller_helper.rb', line 37

def is_token_older_than_current_login?(token)
  return false
  # TODO : We need to get the mothership setting this and the clickfunnels_login_user cookie
  if !cookies[:clickfunnels_login_timestamp].present?
    return true
  end
  return token.updated_at < Time.at(cookies[:clickfunnels_login_timestamp].to_i)
end

#login_requiredObject



24
25
26
27
28
# File 'lib/clickfunnels_auth/controller_helper.rb', line 24

def 
  if !current_user
    not_authorized
  end
end

#not_authorizedObject



30
31
32
33
34
35
# File 'lib/clickfunnels_auth/controller_helper.rb', line 30

def not_authorized
  respond_to do |format|
    format.html{ auth_redirect }
    format.json{ head :unauthorized }
  end
end

#signed_in?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/clickfunnels_auth/controller_helper.rb', line 89

def signed_in?
  current_user.present?
end