-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
162 lines (155 loc) · 6.06 KB
/
main.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
var debug = true;
(function(){
var attributeList = [];
var isloggedIn = false;
var isConfirmed = false;
var tokens;
var settings = {
mailSignupForm : document.querySelector('form.signup.mail'),
signoutButtons : document.querySelectorAll('.signoutbutton'),
confirmForm : document.querySelector('form.signup.confirm'),
loginForm : document.querySelector('form.signup.login'),
confirmParameter : "c",
region : 'us-east-1',
IdentityPoolData : {
IdentityPoolId: 'YOUR IDENTETY POOL ID HERE',
},
CognitoUserAttributes : [],
poolData : {
Paranoia : 7,
UserPoolId : 'YOUR USER POOL ID HERE', // your user pool id here
ClientId : 'your client id here' // your client id here
},
userData : {}
}
var getUrlParams = function getUrlParams(q,s) {
s = (s) ? s : window.location.search;
var re = new RegExp('&'+q+'=([^&]*)','i');
return (s=s.replace(/^\?/,'&').match(re)) ?s=s[1] :s='';
}
var logger = function logger(){
if(!!window.debug){
if(!!window.console){
console.log(arguments);
}
}
}
if(!!window.debug){
console.log("debug mode enabled");
AWSCognito.config.logger = logger;
}
// start collecting entropy for not ending up with paranoia level 0 on old browsers - https://github.com/bitwiseshiftleft/sjcl/issues/77
sjcl.random.startCollectors();
// pass configs to aws and awscognito sdk
AWS.config.region = settings.region;
AWS.config.credentials = new AWS.CognitoIdentityCredentials(settings.IdentityPoolData);
AWSCognito.config.region = settings.region;
AWSCognito.config.credentials = new AWS.CognitoIdentityCredentials(settings.IdentityPoolData);
var addCognitoUserAttribute = function CognitoUserAttributes(key, value){
settings.CognitoUserAttributes.push(
new AWSCognito.CognitoIdentityServiceProvider.CognitoUserAttribute(
{
Name : key,
Value : value
}
)
);
}
var initEvents = function initEvents(){
settings.confirmForm.addEventListener("submit", function submitVerificationCode(e) {
e.preventDefault();
settings.userData.Username = settings.confirmForm.querySelector('[name=mail]').value;
settings.userData.confirmationCode = settings.confirmForm.querySelector('[name=verification]').value;
settings.userData.cognitoUser = new AWSCognito.CognitoIdentityServiceProvider.CognitoUser(settings.userData);
confirmUser();
})
settings.mailSignupForm.addEventListener("submit", function submitSignupByEmail(e) {
e.preventDefault();
addCognitoUserAttribute('email', settings.mailSignupForm.querySelector('[name=mail]').value)
signup(settings.mailSignupForm.querySelector('[name=mail]').value, settings.mailSignupForm.querySelector('[name=password]').value);
});
settings.loginForm.addEventListener("submit", function loginFormSubmit(e) {
e.preventDefault();
settings.userData.Username = settings.loginForm.querySelector('[name=username]').value;
login(settings.loginForm.querySelector('[name=username]').value, settings.loginForm.querySelector('[name=password]').value);
});
[].forEach.call(settings.signoutButtons, function(el) {
el.addEventListener("click", function signout(e) {
e.preventDefault();
signOut();
}, true);
});
}
var login = function login(username, password){
var authenticationDetails = new AWSCognito.CognitoIdentityServiceProvider.AuthenticationDetails({Username : username, Password : password});
settings.userData.cognitoUser = new AWSCognito.CognitoIdentityServiceProvider.CognitoUser(settings.userData);
settings.userData.cognitoUser.authenticateUser(authenticationDetails, {
onSuccess: function (result) {
logger(result);
tokens = result.getAccessToken();
console.log('access token + ' + result.getAccessToken().getJwtToken());
},
onFailure: function(err) {
alert(err);
},
});
init();
}
var init = function init(){
isloggedIn = false;
settings.userData.Pool = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(settings.poolData);
if(!!getUrlParams(settings.confirmParameter) && !isConfirmed){
settings.confirmForm.querySelector('[name=verification]').value = getUrlParams(settings.confirmParameter);
}
if(!!settings.userData.Pool.getCurrentUser()){
settings.userData.cognitoUser = settings.userData.Pool.getCurrentUser();
settings.userData.cognitoUser.getSession(function(err, session) {
if (err) {
isloggedIn = false;
}
isloggedIn = true;
console.log('session validity: ' + session.isValid());
});
}
console.log(isloggedIn);
initEvents();
}
var signup = function signup(username, password){
settings.userData.Pool.signUp(
username,
password,
settings.CognitoUserAttributes,
null,
function(err, result){
if (err) {
logger(err);
return;
}
settings.userData.cognitoUser = result.user;
logger('user name is ' + settings.userData.cognitoUser.getUsername());
}
);
init();
}
var signOut = function signOut(){
console.log(settings.userData);
if(!!settings.userData.cognitoUser){
settings.userData.cognitoUser.signOut();
}
init();
}
var confirmUser = function confirm(){
settings.userData.cognitoUser.confirmRegistration(settings.userData.confirmationCode, true, function(err, result) {
if (err) {
alert(err);
return;
}
isConfirmed = true;
// initialize again to update status of the user
init();
logger('user confirmed');
logger('call result: ' + result);
});
}
init();
}())