forked from aws-samples/aws-serverless-workshops
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcognito-auth.js
169 lines (144 loc) · 4.96 KB
/
cognito-auth.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
/*global WildRydes _config AmazonCognitoIdentity AWSCognito*/
var WildRydes = window.WildRydes || {};
(function scopeWrapper($) {
var signinUrl = '/signin.html';
var poolData = {
UserPoolId: _config.cognito.userPoolId,
ClientId: _config.cognito.userPoolClientId
};
var userPool;
if (!(_config.cognito.userPoolId &&
_config.cognito.userPoolClientId &&
_config.cognito.region)) {
$('#noCognitoMessage').show();
return;
}
userPool = new AmazonCognitoIdentity.CognitoUserPool(poolData);
if (typeof AWSCognito !== 'undefined') {
AWSCognito.config.region = _config.cognito.region;
}
WildRydes.signOut = function signOut() {
userPool.getCurrentUser().signOut();
};
WildRydes.authToken = new Promise(function fetchCurrentAuthToken(resolve, reject) {
var cognitoUser = userPool.getCurrentUser();
if (cognitoUser) {
cognitoUser.getSession(function sessionCallback(err, session) {
if (err) {
reject(err);
} else if (!session.isValid()) {
resolve(null);
} else {
resolve(session.getIdToken().getJwtToken());
}
});
} else {
resolve(null);
}
});
/*
* Cognito User Pool functions
*/
function register(email, password, onSuccess, onFailure) {
var dataEmail = {
Name: 'email',
Value: email
};
var attributeEmail = new AmazonCognitoIdentity.CognitoUserAttribute(dataEmail);
userPool.signUp(email, password, [attributeEmail], null,
function signUpCallback(err, result) {
if (!err) {
onSuccess(result);
} else {
onFailure(err);
}
}
);
}
function signin(email, password, onSuccess, onFailure) {
var authenticationDetails = new AmazonCognitoIdentity.AuthenticationDetails({
Username: email,
Password: password
});
var cognitoUser = createCognitoUser(email);
cognitoUser.authenticateUser(authenticationDetails, {
onSuccess: onSuccess,
onFailure: onFailure
});
}
function verify(email, code, onSuccess, onFailure) {
createCognitoUser(email).confirmRegistration(code, true, function confirmCallback(err, result) {
if (!err) {
onSuccess(result);
} else {
onFailure(err);
}
});
}
function createCognitoUser(email) {
return new AmazonCognitoIdentity.CognitoUser({
Username: email,
Pool: userPool
});
}
/*
* Event Handlers
*/
$(function onDocReady() {
$('#signinForm').submit(handleSignin);
$('#registrationForm').submit(handleRegister);
$('#verifyForm').submit(handleVerify);
});
function handleSignin(event) {
var email = $('#emailInputSignin').val();
var password = $('#passwordInputSignin').val();
event.preventDefault();
signin(email, password,
function signinSuccess() {
console.log('Successfully Logged In');
window.location.href = 'ride.html';
},
function signinError(err) {
alert(err);
}
);
}
function handleRegister(event) {
var email = $('#emailInputRegister').val();
var password = $('#passwordInputRegister').val();
var password2 = $('#password2InputRegister').val();
var onSuccess = function registerSuccess(result) {
var cognitoUser = result.user;
console.log('user name is ' + cognitoUser.getUsername());
var confirmation = ('Registration successful. Please check your email inbox or spam folder for your verification code.');
if (confirmation) {
window.location.href = 'verify.html';
}
};
var onFailure = function registerFailure(err) {
alert(err);
};
event.preventDefault();
if (password === password2) {
register(email, password, onSuccess, onFailure);
} else {
alert('Passwords do not match');
}
}
function handleVerify(event) {
var email = $('#emailInputVerify').val();
var code = $('#codeInputVerify').val();
event.preventDefault();
verify(email, code,
function verifySuccess(result) {
console.log('call result: ' + result);
console.log('Successfully verified');
alert('Verification successful. You will now be redirected to the login page.');
window.location.href = signinUrl;
},
function verifyError(err) {
alert(err);
}
);
}
}(jQuery));