Class: AuthenticationsController

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

Overview

AuthenticationController will handle OmniAuth-providers for an existing user. Since authentications are handled by the omni-gem all we need is index, create, and delete. There is no need to edit/update or show a single authentication.

Instance Method Summary (collapse)

Methods inherited from ApplicationController

#is_current_user?

Instance Method Details

- (Object) auth_failure

try again when authentication failed.



48
49
50
# File 'app/controllers/authentications_controller.rb', line 48

def auth_failure
  redirect_to '/users/sign_in', :alert => params[:message]
end

- (Object) create

Create an authentication when this is called from the authentication provider callback.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/controllers/authentications_controller.rb', line 16

def create
  omniauth = request.env["omniauth.auth"]
  authentication = Authentication.where(:provider => omniauth['provider'], :uid => omniauth['uid']).first
  if authentication
    # Just sign in an existing user with omniauth
    flash[:notice] = t(:signed_in_successfully)
    (:user, authentication.user)
  elsif current_user
    # Add authentication to signed in user
    current_user.authentications.create!(:provider => omniauth['provider'], :uid => omniauth['uid'])
    flash[:notice] = t(:authentication_successful)
    redirect_to authentications_url
  elsif user = create_new_omniauth_user(omniauth)
    # Create a new User through omniauth
    flash[:notice] = t(:signed_in_successfully)
    (:user, user)
  else
    # New user data not valid, try again
    session[:omniauth] = omniauth.except('extra')
    redirect_to new_user_registration_url
  end
end

- (Object) destroy

destroy user’s authentication and return to the authentication page.



40
41
42
43
44
45
# File 'app/controllers/authentications_controller.rb', line 40

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

- (Object) index

Load user’s authentications (Twitter, Facebook, .…)



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

def index
  @authentications = current_user.authentications if current_user
end