-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcognito.js
130 lines (123 loc) · 4.15 KB
/
cognito.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
require('cross-fetch/polyfill');
var AmazonCognitoIdentity = require('amazon-cognito-identity-js');
var CognitoUserPool = AmazonCognitoIdentity.CognitoUserPool;
const { v4 } = require('uuid');
const ApplicationStorage = require('./storage')
var poolData = {
UserPoolId: process.env.COGNITO_USERPOOL_ID || 'us-east-1_UAf0SxZ5c',
ClientId: process.env.COGNITO_CLIENT_ID || '72nrnku0iajiqlahhvkvhqgln2',
Storage: ApplicationStorage
};
var userPool = process.env.ENABLE_TRACKING_DATA_FOR_ANALYTICS ? new AmazonCognitoIdentity.CognitoUserPool(poolData) : null;
const registerUser = function (name, email, password) {
var attributeList = [];
attributeList.push(new AmazonCognitoIdentity.CognitoUserAttribute({
Name: 'email',
Value: email,
}));
attributeList.push(new AmazonCognitoIdentity.CognitoUserAttribute({
Name: 'name',
Value: name,
}));
attributeList.push(new AmazonCognitoIdentity.CognitoUserAttribute({
Name: 'subscription',
Value: 'Basic',
}));
return new Promise(function (resolve, reject) {
userPool.signUp(v4(), password, attributeList, null, function (err, result) {
if (err) {
reject(err.message || JSON.stringify(err));
} else {
var cognitoUser = result.user;
resolve(cognitoUser);
}
});
})
}
const confirmRegistration = function (username, code) {
var userData = {
Username: username,
Pool: userPool,
Storage: ApplicationStorage
};
var cognitoUser = new AmazonCognitoIdentity.CognitoUser(userData);
return new Promise(function (resolve, reject) {
cognitoUser.confirmRegistration(code, true, function (err, result) {
if (err) {
reject(err.message || JSON.stringify(err));
} else {
resolve(result);
}
});
})
}
const getCurrentSession = function () {
if (process.env.ENABLE_TRACKING_DATA_FOR_ANALYTICS) {
var cognitoUser = userPool.getCurrentUser();
if (cognitoUser != null) {
return new Promise(function (resolve, reject) {
cognitoUser.getSession(function (err, session) {
if (err) {
reject(err.message || JSON.stringify(err));
} else {
if (session.isValid()) {
resolve(session)
} else {
cognitoUser.refreshSession(session.getRefreshToken(), (err, session) => {
if (err) {
reject(err.message || JSON.stringify(err));
} else {
resolve(session)
}
})
}
}
});
})
} else {
return Promise.reject(`Session is not found`)
}
} else {
return Promise.resolve()
}
}
const authenticate = function (email, password) {
var authenticationData = {
Username: email,
Password: password,
};
var authenticationDetails = new AmazonCognitoIdentity.AuthenticationDetails(
authenticationData
);
var userData = {
Username: email,
Pool: userPool,
Storage: ApplicationStorage
};
var cognitoUser = new AmazonCognitoIdentity.CognitoUser(userData);
return new Promise(function (resolve, reject) {
cognitoUser.authenticateUser(authenticationDetails, {
onSuccess: function (result) {
resolve(result)
},
onFailure: function (err) {
reject(err.message || JSON.stringify(err));
},
});
})
}
const userSignOut = function (username) {
var cognitoUser = new AmazonCognitoIdentity.CognitoUser({
Username: username,
Pool: userPool,
Storage: ApplicationStorage
});
cognitoUser.signOut();
}
module.exports = {
authenticate,
registerUser,
confirmRegistration,
getCurrentSession,
userSignOut
}