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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
# File 'lib/omniauth/strategies/vkontakte/view_helper.rb', line 25
def vkontakte_login_button control = nil
default_control = false
control ||= begin
default_control = true
'<div id="vk_login" onclick="vkLogin.doLogin();"></div>'
end
button= <<-BUTTON
<div id="vk_api_transport" style="float:left"></div>
<script type="text/javascript">
window.vkAsyncInit = function() {
VK.init({
apiId: '#{OmniAuth.config.vkontakte_app_id}',
nameTransportPath: "/xd_receiver.html"
});
#{ "VK.UI.button('vk_login');" if default_control }
};
vkLogin = {
doLogin: function() {
VK.Auth.login(vkLogin.loginResult, '#{OmniAuth.config.vkontakte_params[:permissions]}');
},
redirectWithPost: function(url, data) {
data = data || {};
#{ respond_to?(:request_forgery_protection_token) && respond_to?(:form_authenticity_token) ?
"data['#{request_forgery_protection_token}'] = '#{form_authenticity_token}'; var method = 'POST';" :
"var method = 'GET';" }
var form = document.createElement("form"),
input;
form.setAttribute("action", url);
form.setAttribute("method", method);
for (var property in data) {
if (data.hasOwnProperty(property)) {
var value = data[property];
if (value instanceof Array) {
for (var i = 0, l = value.length; i < l; i++) {
input = document.createElement("input");
input.setAttribute("type", "hidden");
input.setAttribute("name", property);
input.setAttribute("value", value[i]);
form.appendChild(input);
}
}
else {
input = document.createElement("input");
input.setAttribute("type", "hidden");
input.setAttribute("name", property);
input.setAttribute("value", value);
form.appendChild(input);
}
}
}
document.body.appendChild(form);
form.submit();
document.body.removeChild(form);
},
loginResult: function (r) {
if (r.session) {
if (r.session.expire != "0") {
vkLogin.getUserProfile(vkLogin.putUserProfile);
} else if (r.session.expire == "0") {
VK.Observer.subscribe("auth.sessionChange", function (r) {
VK.Observer.unsubscribe("auth.sessionChange");
if (r.session && r.session.expire != "0") {
vkLogin.getUserProfile(vkLogin.putUserProfile)
} else {
}
});
VK.Auth.login()
}
}
},
getUserProfile: function (callFunc) {
var code;
code = 'return {';
code += 'me: API.getProfiles({uids: API.getVariable({key: 1280}), fields: "nickname,sex,photo"})[0]';
code += '};';
VK.Api.call('execute', {
'code': code
},
callFunc);
},
putUserProfile: function (data) {
if (data.response) {
r = data.response;
vkLogin.redirectWithPost('#{OmniAuth.config.path_prefix}/vkontakte/callback', r.me);
}
}
};
(function() {
var el = document.createElement("script");
el.type = "text/javascript";
el.charset = "windows-1251";
el.src = "http://vkontakte.ru/js/api/openapi.js";
el.async = true;
document.getElementById("vk_api_transport").appendChild(el);
}());
</script>
#{ control }
BUTTON
button.respond_to?(:html_safe) ? button.html_safe : button
end
|