-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
384 lines (322 loc) · 14.3 KB
/
app.js
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
//const poolData = {
// UserPoolId: 'us-east-1_ViBGmruWb',
// ClientId: '5v1l1dgami6h8qufckv96p5vrj',
// };
//const userPool = new AmazonCognitoIdentity.CognitoUserPool(poolData);
let place;
let addressInput = document.getElementById('business-signup-address');
let autocomplete = new google.maps.places.Autocomplete(addressInput);
autocomplete.addListener('place_changed', function () {
place = autocomplete.getPlace();
if (place.geometry) {
let lat = place.geometry.location.lat();
let lng = place.geometry.location.lng();
let address = place.formatted_address; // Get the formatted address
console.log('Address: ' + address);
console.log('Latitude: ' + lat);
console.log('Longitude: ' + lng);
console.log('Location tags: ' + place.types.join(', '));
}
});
var apigClient = apigClientFactory.newClient();
const poolDataCustomer = {
UserPoolId: 'us-east-1_ViBGmruWb',
ClientId: '5v1l1dgami6h8qufckv96p5vrj',
};
const poolDataBusiness = {
UserPoolId: 'us-east-1_ztoLLvLv4',
ClientId: '3umbif497oieqmdl493cv9tr6g',
};
const userPoolCustomer = new AmazonCognitoIdentity.CognitoUserPool(poolDataCustomer);
const userPoolBusiness = new AmazonCognitoIdentity.CognitoUserPool(poolDataBusiness);
console.log("user business", userPoolBusiness);
function signUp() {
const username = document.getElementById('signup-username').value;
const password = document.getElementById('signup-password').value;
const name = document.getElementById('signup-name').value;
const birthdate = document.getElementById('signup-birthdate').value;
const dataName = {
Name: 'name',
Value: name,
};
const dataBirthdate = {
Name: 'birthdate',
Value: birthdate,
};
const attributeList = [
new AmazonCognitoIdentity.CognitoUserAttribute(dataName),
new AmazonCognitoIdentity.CognitoUserAttribute(dataBirthdate),
];
userPoolCustomer.signUp(username, password, attributeList, null, (err, result) => {
if (err) {
alert(err.message || JSON.stringify(err));
return;
}
localStorage.setItem('signupUsername', username); // Store the username
alert('User registered successfully. Please check your email for the verification code.');
document.getElementById('login-form').style.display = 'none';
document.getElementById('signup-form').style.display = 'none';
document.getElementById('verification-form').style.display = 'block';
});
}
function signIn() {
const username = document.getElementById('login-username').value;
const password = document.getElementById('login-password').value;
const authenticationData = {
Username: username,
Password: password,
};
const authenticationDetails = new AmazonCognitoIdentity.AuthenticationDetails(authenticationData);
const userData = {
Username: username,
Pool: userPoolCustomer,
};
const cognitoUser = new AmazonCognitoIdentity.CognitoUser(userData);
cognitoUser.authenticateUser(authenticationDetails, {
onSuccess: (result) => {
// Access the ID token
const idToken = result.getIdToken().getJwtToken();
// Store the ID token in local storage
localStorage.setItem('l', idToken);
cognitoUser.getUserAttributes((err, attributes) => {
if (err) {
alert(err.message || JSON.stringify(err));
return;
}
const userID = attributes.find(attr => attr.Name === 'sub').Value;
const name = attributes.find(attr => attr.Name === 'name').Value;
const birthdate = attributes.find(attr => attr.Name === 'birthdate').Value;
localStorage.setItem('userID', userID);
console.log("userID", userID)
localStorage.setItem('userName', name);
localStorage.setItem('userBirthdate', birthdate);
localStorage.setItem('userEmail', username);
window.location.href = '/static/customer-dashboard.html';
});
},
onFailure: (err) => {
alert(err.message || JSON.stringify(err));
},
});
}
function verify() {
const storedUsername = localStorage.getItem('signupUsername');
if (!storedUsername) {
alert('No username found. Please sign up first.');
return;
}
const verificationCode = document.getElementById('verification-code').value;
const userData = {
Username: storedUsername,
Pool: userPoolCustomer,
};
const cognitoUser = new AmazonCognitoIdentity.CognitoUser(userData);
cognitoUser.confirmRegistration(verificationCode, true, (err, result) => {
if (err) {
alert(err.message || JSON.stringify(err));
return;
}
alert('Email verified successfully. You can now log in.');
document.getElementById('verification-form').style.display = 'none';
document.getElementById('login-form').style.display = 'block';
});
}
function showCustomer() {
document.getElementById('selection-form').style.display = 'none';
document.getElementById('customer-form').style.display = 'block';
}
function showBusiness() {
document.getElementById('selection-form').style.display = 'none';
document.getElementById('business-form').style.display = 'block';
}
function signUpBusiness() {
console.log(place)
const bucket_name = 'business-profile-pictures';
const folder_name = 'profile-images/'
const username = document.getElementById('business-signup-username').value;
const password = document.getElementById('business-signup-password').value;
const name = document.getElementById('business-signup-name').value;
// const address = place.formatted_address;
//Temporary replacement
const address = document.getElementById('business-signup-address').value;
const email = document.getElementById('business-signup-email').value;
const phone_number = document.getElementById('business-signup-phone').value;
const website = document.getElementById('business-signup-website').value;
const adminName = document.getElementById('business-signup-adminName').value;
const cuisines = document.getElementById('business-signup-cuisines').value;
const descriptionText = document.getElementById('business-signup-descriptionText').value;
const descriptors = document.getElementById('business-signup-descriptors').value;
const locationTags = document.getElementById('business-signup-locationTags').value;
const pictureInput = document.getElementById('business-signup-picture');
const latitude = place.geometry.location.lat();
const longitude = place.geometry.location.lng();
//Temporary replacement
// const latitude = 45.508271;
// const longitude = -122.610036;
const file = pictureInput.files[0];
const reader = new FileReader();
reader.onloadend = () => {
const base64data = reader.result.split(',')[1];
const payload = {
image: base64data,
username: username
};
// URL of your Lambda function (API Gateway endpoint)
const apiGatewayUrl = 'https://sbfye9qq29.execute-api.us-east-1.amazonaws.com/beta/image';
axios.post(apiGatewayUrl, payload)
.then(response => {
//const image_url = response.data.image_url; // Get the image URL from the response
const image_url = `https://${bucket_name}.s3.amazonaws.com/${folder_name}${username}.jpg`;
// Update the attributeList to include the image URL and other attributes
const attributeList = [
new AmazonCognitoIdentity.CognitoUserAttribute({ Name: 'name', Value: name }),
new AmazonCognitoIdentity.CognitoUserAttribute({ Name: 'address', Value: address }),
new AmazonCognitoIdentity.CognitoUserAttribute({ Name: 'email', Value: email }),
new AmazonCognitoIdentity.CognitoUserAttribute({ Name: 'phone_number', Value: phone_number }),
new AmazonCognitoIdentity.CognitoUserAttribute({ Name: 'website', Value: website }),
new AmazonCognitoIdentity.CognitoUserAttribute({ Name: 'custom:adminName', Value: adminName }),
new AmazonCognitoIdentity.CognitoUserAttribute({ Name: 'custom:cuisines', Value: cuisines }),
new AmazonCognitoIdentity.CognitoUserAttribute({ Name: 'custom:descriptionText', Value: descriptionText }),
new AmazonCognitoIdentity.CognitoUserAttribute({ Name: 'custom:descriptors', Value: descriptors }),
new AmazonCognitoIdentity.CognitoUserAttribute({ Name: 'custom:locationTags', Value: locationTags }),
new AmazonCognitoIdentity.CognitoUserAttribute({ Name: 'picture', Value: image_url }),
new AmazonCognitoIdentity.CognitoUserAttribute({ Name: 'custom:latitude', Value: latitude.toString() }),
new AmazonCognitoIdentity.CognitoUserAttribute({ Name: 'custom:longitude', Value: longitude.toString() }),
];
// Continue with the sign-up process
userPoolBusiness.signUp(username, password, attributeList, null, (err, result) => {
if (err) {
alert(err.message || JSON.stringify(err));
return;
}
localStorage.setItem('businessSignupUsername', username); // Store the username
alert('Business registered successfully. Please check your email for the verification code.');
document.getElementById('business-login-form').style.display = 'none';
document.getElementById('business-signup-form').style.display = 'none';
document.getElementById('business-verification-form').style.display = 'block';
});
})
.catch(error => {
alert(error.message || JSON.stringify(error));
});
};
reader.readAsDataURL(file);
}
function signInBusiness() {
const username = document.getElementById('business-login-username').value;
const password = document.getElementById('business-login-password').value;
const authenticationData = {
Username: username,
Password: password,
};
const authenticationDetails = new AmazonCognitoIdentity.AuthenticationDetails(authenticationData);
const userData = {
Username: username,
Pool: userPoolBusiness,
};
const cognitoUser = new AmazonCognitoIdentity.CognitoUser(userData);
cognitoUser.authenticateUser(authenticationDetails, {
onSuccess: (result) => {
// Access the ID token
const idToken = result.getIdToken().getJwtToken();
// Store the ID token in local storage
localStorage.setItem('idToken', idToken);
cognitoUser.getUserAttributes((err, attributes) => {
if (err) {
alert(err.message || JSON.stringify(err));
return;
}
localStorage.setItem('Username', username);
localStorage.setItem('restaurantID', attributes.find(attr => attr.Name === 'sub').Value);
localStorage.setItem('adminName', attributes.find(attr => attr.Name === 'custom:adminName').Value);
localStorage.setItem('userEmail', attributes.find(attr => attr.Name === 'email').Value);
localStorage.setItem('address', attributes.find(attr => attr.Name === 'address').Value);
localStorage.setItem('name', attributes.find(attr => attr.Name === 'name').Value);
localStorage.setItem('phoneNumber', attributes.find(attr => attr.Name === 'phone_number').Value);
localStorage.setItem('website', attributes.find(attr => attr.Name === 'website').Value);
localStorage.setItem('cuisines', attributes.find(attr => attr.Name === 'custom:cuisines').Value);
localStorage.setItem('descriptionText', attributes.find(attr => attr.Name === 'custom:descriptionText').Value);
localStorage.setItem('descriptors', attributes.find(attr => attr.Name === 'custom:descriptors').Value);
localStorage.setItem('locationTags', attributes.find(attr => attr.Name === 'custom:locationTags').Value);
localStorage.setItem('picture', attributes.find(attr => attr.Name === 'picture').Value); // Added 'picture' attribute
localStorage.setItem('profileImageUrl', attributes.find(attr => attr.Name === 'picture').Value);
window.location.href = '/static/business-dashboard.html';
});
},
onFailure: (err) => {
alert(err.message || JSON.stringify(err));
},
});
}
function initUserPools() {
const poolDataCustomer = {
UserPoolId: 'us-east-1_ViBGmruWb',
ClientId: '5v1l1dgami6h8qufckv96p5vrj',
};
const poolDataBusiness = {
UserPoolId: 'us-east-1_ztoLLvLv4',
ClientId: '3umbif497oieqmdl493cv9tr6g',
};
let userPoolCustomer = new AmazonCognitoIdentity.CognitoUserPool(poolDataCustomer);
let userPoolBusiness = new AmazonCognitoIdentity.CognitoUserPool(poolDataBusiness);
return userPoolBusiness;
}
function initUserPoolsCustomer() {
const poolDataCustomer = {
UserPoolId: 'us-east-1_ViBGmruWb',
ClientId: '5v1l1dgami6h8qufckv96p5vrj',
};
const poolDataBusiness = {
UserPoolId: 'us-east-1_ztoLLvLv4',
ClientId: '3umbif497oieqmdl493cv9tr6g',
};
let userPoolCustomer = new AmazonCognitoIdentity.CognitoUserPool(poolDataCustomer);
let userPoolBusiness = new AmazonCognitoIdentity.CognitoUserPool(poolDataBusiness);
return userPoolCustomer;
}
function signOut() {
let userPoolBusiness = initUserPools();
const userData = {
Username: localStorage.getItem('userEmail'),
Pool: userPoolBusiness
};
console.log("trying to signout")
const cognitoUser = new AmazonCognitoIdentity.CognitoUser(userData);
cognitoUser.signOut();
localStorage.clear();
window.location.href = '/static/index.html';
}
function signOutCustomer() {
let userPoolCustomer = initUserPoolsCustomer();
const userData = {
Username: localStorage.getItem('userEmail'),
Pool: userPoolCustomer
};
console.log("trying to signout")
const cognitoUser = new AmazonCognitoIdentity.CognitoUser(userData);
cognitoUser.signOut();
localStorage.clear();
window.location.href = '/static/index.html';
}
function verifyBusiness() {
const storedUsername = localStorage.getItem('businessSignupUsername');
if (!storedUsername) {
alert('No username found. Please sign up first.');
return;
}
const verificationCode = document.getElementById('business-verification-code').value;
const userData = {
Username: storedUsername,
Pool: userPoolBusiness,
};
const cognitoUser = new AmazonCognitoIdentity.CognitoUser(userData);
cognitoUser.confirmRegistration(verificationCode, true, (err, result) => {
if (err) {
alert(err.message || JSON.stringify(err));
return;
}
alert('Email verified successfully. You can now log in.');
document.getElementById('business-verification-form').style.display = 'none';
document.getElementById('business-login-form').style.display = 'block';
});
}