Class: Authentifyd::AuthenticationsController

Inherits:
ApplicationController show all
Includes:
Localyzed::LocalyzedController
Defined in:
app/controllers/authentifyd/authentications_controller.rb

Instance Method Summary collapse

Methods inherited from ActionController::Base

#after_sign_in_path_for, #after_sign_out_path_for

Instance Method Details

#addObject



21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/controllers/authentifyd/authentications_controller.rb', line 21

def add
  @user ||= Authentifyd::User.find(params[:user_id])
  if @user.valid_password?(params[:user][:password])
    omniauth = session[:omniauth]
    @user.authentications.create!(:provider => omniauth['provider'], :uid => omniauth['uid'])
    session[:omniauth] = nil
    (:user, @user)
  else
    flash[:notice] = "Incorrect Password"
    return redirect_to link_accounts_url(@user.id)
  end
end

#createObject



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
# File 'app/controllers/authentifyd/authentications_controller.rb', line 38

def create
  omniauth = request.env['omniauth.auth']
  authentication = Authentifyd::Authentication.find_by_provider_and_uid(omniauth['provider'], omniauth['uid'])
  if authentication
    flash[:notice] = "Signed in successfully"
    (:user, authentication.user)
  else
    user = user_signed_in? ? current_user : Authentifyd::User.new
    user.apply_omniauth(omniauth)      
    if user_signed_in?
      user.save
      redirect_to accounts_url
    else
      omniauth_email = (omniauth["info"] && omniauth["info"]["email"]).try(:to_s)
      user.email = omniauth_email unless user_signed_in?        
      if user.save
        flash[:notice] = "Successfully registered"
        (:user, user)
      else
        session[:omniauth] = omniauth.except('extra')
        session[:omniauth_email] = omniauth_email

        # Check if email already taken. If so, ask user to link_accounts
        if user.errors[:email][0] =~ /has already been taken/ # omniauth? TBD
          # fetch the user with this email id!
          user = Authentifyd::User.find_by_email(user.email)
          return redirect_to link_accounts_url(user.id)
        end
        redirect_to new_user_registration_url
      end
    end
  end
end

#destroyObject



77
78
79
80
81
82
83
84
85
# File 'app/controllers/authentifyd/authentications_controller.rb', line 77

def destroy
  @authentication = Authentifyd::Authentication.find(params[:id])
  @authentication.destroy

  respond_to do |format|
    format.html { redirect_to(accounts_url) }
    format.xml  { head :ok }
  end
end

#failureObject



72
73
74
75
# File 'app/controllers/authentifyd/authentications_controller.rb', line 72

def failure
  flash[:notice] = params[:message]
  redirect_to root_path
end

#indexObject



8
9
10
11
12
13
14
15
# File 'app/controllers/authentifyd/authentications_controller.rb', line 8

def index
  @authentications = current_user.authentications.all

  respond_to do |format|
    format.html # index.html.erb
    format.xml  { render :xml => @authentications }
  end
end


34
35
36
# File 'app/controllers/authentifyd/authentications_controller.rb', line 34

def link
  @user ||= Authentifyd::User.find(params[:user_id])
end

#newObject



17
18
19
# File 'app/controllers/authentifyd/authentications_controller.rb', line 17

def new
  @authentication = Authentifyd::Authentication.new
end