-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.html
258 lines (209 loc) · 7.08 KB
/
index.html
1
2
3
4
5
6
7
8
9
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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>First screen</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<script type="text/javascript" src="phonegap.js"></script>
<script src="js/jquery-2.1.0.min.js"></script>
<script>
$(document).on("mobileinit",function() {
$.mobile.autoInitializePage = false;
});
</script>
<script src="js/jquery.mobile-1.4.2.min.js"></script>
<link href="css/jquery.mobile-1.4.2.min.css" rel="stylesheet">
<link href="css/my-theme.min.css" rel="stylesheet">
<script>
// Wait for PhoneGap to load
// document.addEventListener("deviceready", onDeviceReady, false);
var authWindow; // Used to store a reference to InAppBrowser window
var accessToken; // The token obtained for an authorized user
var loadingAuthPage; // Used to track the initial loading of the OAuth page
var userId;
var userName;
var wya_token; // Token used for access to WhereYouAt server
//var serverBaseUrl = "http://192.168.1.4:1337/";
var serverBaseUrl = "http://amaltsev-test.apigee.net/whereyouat_api/";
// jQuery document.ready function instructs the browser to execute its code after the DOM finishes loading
$(function () {
$('#btn_login').on('click', doLogin);
$('#btn_verify').on('click', verifyIdentity);
var initial = "page-login";
if(localStorage["wya_token"]) {
wya_token = localStorage["wya_token"];
userId = localStorage["userId"];
showUserInfo();
// initial = "page-profile";
// loadProfile();
}
//set the page hash to our start page
window.location.hash = initial;
$.mobile.initializePage();
});
function showUserInfo() {
// If the user info doesn't exist in the storage then bail
if(!localStorage["userId"]) return;
// Set the values for the info
$('#user_name').html(localStorage["userName"]);
$('#user_id').html("<b>ID: </b>" + localStorage["userId"]);
}
function doLogin() {
var OAuthURL = "https://secure.meetup.com/oauth2/authorize?client_id=ig2nen9lq9iut7ftppm74uoefp" +
"&response_type=token&redirect_uri=http://localhost";
loadingAuthPage = true;
// Open OAuth page in the InAppBrowser window
authWindow = window.open(OAuthURL, '_blank', 'location=no,hidden=yes');
// Start activity indicator
$.mobile.loading("show");
authWindow.addEventListener('loadstop', function(event) {
// If we just finished loading the Meetup OAuth page, then make it visible
if(loadingAuthPage) {
// Stop activity indicator
$.mobile.loading("hide");
// Make the OAuth window visible
authWindow.show();
loadingAuthPage = false;
}
});
authWindow.addEventListener('loadstart', function(event) {
var url = event.url;
// Execute reg-exp on the URL. If 'access_token' is absent the result will be null.
// Otherwise the result is a two-element array, with second element containing the actual token.
var token = /access_token=(\w+)/.exec(url);
var error = /error=/.exec(url);
if (token) {
authWindow.close();
accessToken = token[1];
// Store access_token in the local storage
localStorage["accessToken"] = accessToken;
performLogin();
return;
}
// Authorization was denied or failed
if(error) {
authWindow.close();
token = null;
alert("Error");
}
});
}
function performLogin() {
var requestUrl = serverBaseUrl + "login";
// Start activity indicator
$.mobile.loading("show");
$.ajax({
type: 'POST',
url: requestUrl,
data: JSON.stringify({ token: accessToken }),
contentType: "application/json",
dataType: 'json'
})
.done( function( data ) {
// Hide activity indicator
$.mobile.loading("hide");
if(data.success) {
wya_token = data.token;
localStorage["wya_token"] = wya_token;
userId = data.id;
localStorage["userId"] = userId;
alert("WYA login successful: " + JSON.stringify(data));
loadProfile();
}
else
alert("WYA login unsuccessful: " + JSON.stringify(data));
})
.fail( function( data ) {
// Hide activity indicator
$.mobile.loading("hide");
alert("WYA login failed: " + JSON.stringify(data));
});
}
function loadProfile() {
// Request only the specified fields of the profile of the OAuth'd user
var requestUrl = "https://api.meetup.com/2/member/self?only=name,id,photo&access_token=" + accessToken;
// Start activity indicator with text
$.mobile.loading( "show", { text: "Loading profile", textVisible: true, theme: "b" });
$.getJSON( requestUrl)
.done( function( data ) {
// Hide activity indicator
$.mobile.loading("hide");
userName = data.name;
localStorage["userName"] = userName;
showUserInfo();
var picUrl = data.photo.photo_link;
$('#profile_pic').attr('src', picUrl);
$.mobile.changePage( "#page-profile", { transition: "slideup", changeHash: false });
})
.fail( function( data ) {
// Hide activity indicator
$.mobile.loading("hide");
alert("Failed with data: " + JSON.stringify(data));
});
}
function verifyIdentity() {
var requestUrl = serverBaseUrl + "verify";
// Start activity indicator with text
$.mobile.loading( "show", { text: "Verifying", textVisible: true, theme: "b" });
$.ajax({
type: 'POST',
url: requestUrl,
data: JSON.stringify({ token: accessToken }),
contentType: "application/json",
dataType: 'json'
})
.done( function( data ) {
// Hide activity indicator
$.mobile.loading("hide");
if(data.success)
alert("Verification successful");
else
alert("Verification failed");
})
.fail( function( data ) {
// Hide activity indicator
$.mobile.loading("hide");
alert("Failed with data: " + JSON.stringify(data));
});
}
</script>
</head>
<body>
<div data-role="page" data-control-title="Login" id="page-login">
<div data-theme="a" data-role="header">
<h3>
Login
</h3>
</div>
<div data-role="content">
<div style="margin:0 auto; text-align:center;">
<img id="img_logo" src="img/logo.png" alt="Logo" style="width:290px; height:100px;">
</div>
<br><br>
<a id="btn_login" data-role="button" href="#">
Login via Meetup
</a>
</div>
</div>
<div data-role="page" data-control-title="Login result" id="page-profile">
<div data-theme="a" data-role="header">
<h3>
Profile
</h3>
</div>
<div data-role="content">
<div style="margin:0 auto; margin-top: 20px; text-align:center;">
<img id="profile_pic" src="" alt="profile picture" style="height:30%; border: 1px solid #b8b8b8;">
</div>
<p id="user_name" style="font-size: 25px;">Name</p>
<p id="user_id" style="font-size: 15px;">ID</p>
<br><br>
<a id="btn_verify" data-role="button" href="#">
Verify identity
</a>
</div>
</div>
</body>
</html>