Class: OdaniaOmniauthAuthentication::SessionsController

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

Instance Method Summary collapse

Instance Method Details

#createObject



10
11
12
13
14
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
48
49
50
51
52
53
54
55
56
# File 'app/controllers/odania_omniauth_authentication/sessions_controller.rb', line 10

def create
  auth = request.env['omniauth.auth']
  # Find an identity here
  @identity = OdaniaOmniauthAuthentication::UserAuthentication.find_with_omniauth(auth)

  if @identity.nil?
    # If no identity was found, create a brand new one here
    @identity = OdaniaOmniauthAuthentication::UserAuthentication.create_with_omniauth(auth)
  end

  if user_signed_in?
    if @identity.user == current_user
      # User is signed in so they are trying to link an identity with their
      # account. But we found the identity and the user associated with it
      # is the current user. So the identity is already associated with
      # this user. So let's display an error message.
      redirect_to redirect_back_or_default('Already linked that account')
    else
      # The identity is not associated with the current_user so lets
      # associate the identity
      @identity.user = current_user
      @identity.save!
      redirect_to redirect_back_or_default(t('Successfully linked that account'))
    end
  else
    if @identity.user.present?
      # The identity we found had a user associated with it so let's
      # just log them in here
      set_current_user(@identity.user)
      redirect_to redirect_back_or_default(t('Signed in'))
    else
      # No user associated with the identity so we need to create a new one
      user = Odania::User.new
      user.ip = request.remote_ip
      user.email = auth['info']['email']
      user.name = auth['info']['name']
      user.save!

      @identity.user_id = user.id
      @identity.save!

      set_current_user(user)

      redirect_to redirect_back_or_default(t('Signed in'))
    end
  end
end

#destroyObject



58
59
60
61
# File 'app/controllers/odania_omniauth_authentication/sessions_controller.rb', line 58

def destroy
  set_current_user(nil)
  redirect_to redirect_back_or_default('Signed out')
end

#newObject



7
8
# File 'app/controllers/odania_omniauth_authentication/sessions_controller.rb', line 7

def new
end