-
Notifications
You must be signed in to change notification settings - Fork 0
/
browser.js
103 lines (85 loc) · 3.06 KB
/
browser.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
var AmazonCognitoIdentity = require('amazon-cognito-identity-js');
var poolData = {
UserPoolId : 'eu-west-1_o6rTi0jr2', // Your user pool id here
ClientId : '54j5i8o3j3j51lgdr6icppmqo5' // Your client id here
};
var userPool = new AmazonCognitoIdentity.CognitoUserPool(poolData);
var cognitoUser = userPool.getCurrentUser();
if (cognitoUser != null) {
cognitoUser.getSession((err, result) => {
console.log(err);
console.log(result);
cognitoUser.getUserAttributes(function(err, attributes) {
if (err) {
// Handle error
} else {
// Do something with attributes
console.log(attributes);
}
});
});
}
document.getElementById("registerForm").onsubmit = (evt) => {
var email = evt.target.childNodes[1].value;
var password = evt.target.childNodes[3].value;
var responseSpan = document.getElementById('registerResponse');
responseSpan.textContent = '';
userPool.signUp(email, password, [], null, (err, result) => {
if (err) {
responseSpan.textContent = err.message || JSON.stringify(err);
return;
}
cognitoUser = result.user;
responseSpan.textContent = 'User has been created';
});
return false;
};
document.getElementById("registerConfirmForm").onsubmit = (evt) => {
var email = evt.target.childNodes[1].value;
var validateCode = evt.target.childNodes[3].value;
var responseSpan = document.getElementById('registerConfirmResponse');
responseSpan.textContent = '';
var cognitoUser = new AmazonCognitoIdentity.CognitoUser({
Username: email,
Pool: userPool
});
cognitoUser.confirmRegistration(validateCode, true, function(err, result) {
if (err) {
responseSpan.textContent = err.message || JSON.stringify(err);
return;
}
responseSpan.textContent = result;
});
return false;
};
document.getElementById("loginForm").onsubmit = (evt) => {
var email = evt.target.childNodes[1].value;
var password = evt.target.childNodes[3].value;
var responseSpan = document.getElementById('loginResponse');
responseSpan.textContent = '';
var cognitoUser = new AmazonCognitoIdentity.CognitoUser({
Username: email,
Pool: userPool
});
cognitoUser.setAuthenticationFlowType('USER_PASSWORD_AUTH');
cognitoUser.authenticateUser(new AmazonCognitoIdentity.AuthenticationDetails({
Username: email,
Password: password
}), {
onSuccess: (result) => {
var accessToken = result.getAccessToken().getJwtToken();
cognitoUser.getUserAttributes(function(err, attributes) {
if (err) {
// Handle error
} else {
// Do something with attributes
console.log(attributes);
}
});
},
onFailure: (err) => {
responseSpan.textContent = err.message || JSON.stringify(err);
}
});
return false;
};