-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
99 lines (82 loc) · 3.36 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
// app.js
// Initialize AWS SDK
AWS.config.region = 'us-east-1';
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
IdentityPoolId: 'us-east-1:1b026943-879e-4545-870c-88c166b51811',
});
// Get credentials asynchronously
AWS.config.credentials.get(() => {
// Credentials will be available here
});
// Handle sign-up form submission
document.getElementById('signup-form').addEventListener('submit', function(event) {
event.preventDefault(); // Prevent default form submission
// Retrieve user input
var username = document.getElementById('username').value.trim();
var password = document.getElementById('password').value.trim();
var email = document.getElementById('email').value.trim();
var name = document.getElementById('name').value.trim();
// Configure user pool data
var poolData = {
UserPoolId: 'us-east-1_DpapndWLE',
ClientId: '7otv9qv5hiuurdqvvs7rj2nebi'
};
var userPool = new AmazonCognitoIdentity.CognitoUserPool(poolData);
var attributeList = [];
var dataEmail = {
Name: 'email',
Value: email
};
var dataName = {
Name: 'name',
Value: name
};
var attributeEmail = new AmazonCognitoIdentity.CognitoUserAttribute(dataEmail);
var attributeName = new AmazonCognitoIdentity.CognitoUserAttribute(dataName);
attributeList.push(attributeEmail);
attributeList.push(attributeName);
// Sign up the user
userPool.signUp(username, password, attributeList, null, function(err, result) {
if (err) {
console.log('Error registering user:', err);
document.getElementById('signup-result').innerHTML = 'Error: ' + err.message;
return;
}
var cognitoUser = result.user;
console.log('User successfully registered:', cognitoUser);
document.getElementById('signup-result').innerHTML = 'User successfully registered!';
});
});
// Handle sign-in form submission
document.getElementById('signin-form').addEventListener('submit', function(event) {
event.preventDefault(); // Prevent default form submission
// Retrieve user input
var signinUsername = document.getElementById('signin-username').value.trim();
var signinPassword = document.getElementById('signin-password').value.trim();
// Configure user pool data
var poolData = {
UserPoolId: 'us-east-1_DpapndWLE',
ClientId: '7otv9qv5hiuurdqvvs7rj2nebi'
};
var userPool = new AmazonCognitoIdentity.CognitoUserPool(poolData);
var authenticationDetails = new AmazonCognitoIdentity.AuthenticationDetails({
Username: signinUsername,
Password: signinPassword
});
var cognitoUser = new AmazonCognitoIdentity.CognitoUser({
Username: signinUsername,
Pool: userPool
});
// Sign in the user
cognitoUser.authenticateUser(authenticationDetails, {
onSuccess: function (result) {
console.log('User successfully authenticated:', result);
document.getElementById('signin-result').innerHTML = 'User successfully authenticated!';
// Redirect or perform additional actions after successful authentication
},
onFailure: function (err) {
console.log('Error authenticating user:', err);
document.getElementById('signin-result').innerHTML = 'Error: ' + err.message;
}
});
});