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



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
57
# File 'app/controllers/odania_omniauth_authentication/sessions_controller.rb', line 11

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.
			return 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!
			return 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)
			return 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)

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

#destroyObject



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

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

#newObject



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

def new
end