Class: Contour::AuthenticationsController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/contour/authentications_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



15
16
17
18
19
20
21
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
# File 'app/controllers/contour/authentications_controller.rb', line 15

def create
  omniauth = request.env["omniauth.auth"]
  if omniauth
    omniauth['uid'] = omniauth['info']['email'] if omniauth['provider'] == 'google_apps' and omniauth['info']
    authentication = Authentication.find_by_provider_and_uid(omniauth['provider'], omniauth['uid'])
    omniauth['info']['email'] = omniauth['extra']['raw_info']['email'] if omniauth['info'] and omniauth['info']['email'].blank? and omniauth['extra'] and omniauth['extra']['raw_info']
    if authentication
      logger.info "Existing authentication found."
      session["user_return_to"] = request.env["action_dispatch.request.unsigned_session_cookie"]["user_return_to"] if request.env and request.env["action_dispatch.request.unsigned_session_cookie"] and request.env["action_dispatch.request.unsigned_session_cookie"]["user_return_to"] and session["user_return_to"].blank?
      flash[:notice] = "Signed in successfully." if authentication.user.active_for_authentication?
      (:user, authentication.user)
    elsif current_user
      logger.info "Logged in user found, creating associated authentication."
      current_user.authentications.create!( provider: omniauth['provider'], uid: omniauth['uid'] )
      redirect_to authentications_path, notice: "Authentication successful."
    else
      logger.info "Creating new user with new authentication."
      user = User.new(params[:user])
      user.apply_omniauth(omniauth)
      user.password = Devise.friendly_token[0,20] if user.password.blank?
      if user.save
        session["user_return_to"] = request.env["action_dispatch.request.unsigned_session_cookie"]["user_return_to"] if request.env and request.env["action_dispatch.request.unsigned_session_cookie"] and request.env["action_dispatch.request.unsigned_session_cookie"]["user_return_to"] and session["user_return_to"].blank?
        flash[:notice] = "Signed in successfully." if user.active_for_authentication?
        (:user, user)
      else
        session[:omniauth] = omniauth.except('extra')
        redirect_to new_user_registration_path
      end
    end
  else
    redirect_to authentications_path, alert: 'Authentication not successful.'
  end
end

#destroyObject



49
50
51
52
53
54
55
56
# File 'app/controllers/contour/authentications_controller.rb', line 49

def destroy
  @authentication = current_user.authentications.find(params[:id])
  @authentication.destroy
  respond_to do |format|
    format.html { redirect_to authentications_path, notice: 'Successfully removed authentication.' }
    format.js
  end
end

#failureObject



11
12
13
# File 'app/controllers/contour/authentications_controller.rb', line 11

def failure
  redirect_to new_user_session_path, alert: params[:message].blank? ? nil : params[:message].humanize
end

#indexObject



4
5
# File 'app/controllers/contour/authentications_controller.rb', line 4

def index
end

#passthruObject



7
8
9
# File 'app/controllers/contour/authentications_controller.rb', line 7

def passthru
  render file: "#{Rails.root}/public/404", formats: [:html], status: 404, layout: false
end