Module: Gigya::Session

Defined in:
lib/gigya/session.rb

Overview

This module is a mix-in that makes for much easier Gigya UI integration in Gigya apps. Essentially if you include this, it is very straightforward to do a Gigya login mechanism.

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



4
5
6
7
# File 'lib/gigya/session.rb', line 4

def self.included(base)
	base.include Gigya::ControllerUtils
	base.extend ClassMethods
end

Instance Method Details

#createObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/gigya/session.rb', line 13

def create
	gigya_save_jwt(self.class.gigya_token_storage || :cookie)

	if params[:redirect].blank?
		redir = self.class.
		case redir
			when String
				redirect_to redir
			when Symbol
				redirect_to self.send(redir)
			else
				redirect_to redir.call
		end
	else
		redirect_to params[:redirect]
	end
end

#destroyObject



9
10
11
# File 'lib/gigya/session.rb', line 9

def destroy
	cookies.delete Gigya::ControllerUtils::GIGYA_COOKIE_PARAM
end

#newObject



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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/gigya/session.rb', line 31

def new
	head_code = <<EOF
<script type='text/javascript' src='https://cdns.gigya.com/js/gigya.js?apikey=#{self.class.gigya_api_key}'></script>
<script type="text/javascript">
function did_login(evt, resp) {
	form = document.getElementById("hidden-login-form");
	gtok = document.getElementById("gigya-token");

	gtok.value = resp.id_token;
	form.submit();
}

var expire_time = #{self.class.gigya_token_expire_time};
gigya.accounts.showScreenSet({
	screenSet: '#{self.class.gigya_screen_set}',
	startScreen: '#{self.class.gigya_start_screen}',
	containerID: 'gigya-screenset-container',
	deviceType: 'mobile',
	sessionExpiration: expire_time,
	onAfterSubmit: function(evt) {
		if(evt.form == 'gigya-login-form' && evt.response.errorCode == 0) {
			gigya.accounts.getJWT({
				fields: "#{self.class.gigya_jwt_fields}",
				expiration: expire_time,
				callback: function(resp) {
					did_login(evt, resp);
				}
			});
		}
	}
});
</script>
EOF
	body_code = <<EOF
<%= form_tag request.path.gsub('/new', ''), :id => "hidden-login-form" do %>
<%= hidden_field_tag :redirect, params[:redirect] %>
<%= hidden_field_tag :gigya_token, "", :id => "gigya-token" %>
<% end %>
<div class="gigya-screenset-class" id="gigya-screenset-container"></div>
EOF

	if self.class.gigya_script_content_for.present?
		head_code = "<% content_for :#{gigya_script_content_for} do %>#{head_code}<% end %>"
	end

	full_erb = head_code + body_code
	render :inline => full_erb, :layout => true
end