-
Notifications
You must be signed in to change notification settings - Fork 3
/
handler.js
executable file
·204 lines (188 loc) · 6.79 KB
/
handler.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
'use strict';
const express = require("express");
const serverless = require("serverless-http");
const bodyParser = require("body-parser");
const cors = require("cors");
const app = express();
const AmazonCognitoIdentity = require('amazon-cognito-identity-js');
const AWS = require('aws-sdk');
// global.fetch = require('node-fetch');
const jwt_val = require('./middleware/jwt-validator');
const { poolData } = require('./config/cognito-config');
app.use(cors());
app.use(express.json({ limit: '50mb' }));
app.use(bodyParser.urlencoded({
limit: '50mb',
extended: true
}));
app.get('/api', (req, res) => {
res.status(200).json({ "status": 1, "message": "API Working" });
});
app.post('/api/signup', (req, res) => {
var body = req.body;
var userPool = new AmazonCognitoIdentity.CognitoUserPool(poolData);
var attributeList = [];
attributeList.push(new AmazonCognitoIdentity.CognitoUserAttribute({ Name: "name", Value: body['name'] }));
attributeList.push(new AmazonCognitoIdentity.CognitoUserAttribute({ Name: "custom:role", Value: body['role'] }));
let regex = /^(\+\d{3})?\d{9}$/;
if (body['username'].match(regex)) {
attributeList.push(new AmazonCognitoIdentity.CognitoUserAttribute({ Name: "phone_number", Value: body['username'] }));
} else {
attributeList.push(new AmazonCognitoIdentity.CognitoUserAttribute({ Name: "email", Value: body['username'] }));
}
userPool.signUp(body['username'], body['password'], attributeList, null, (err, result) => {
if (err) {
return;
}
const cognitoUser = result.user;
console.log('user name is ' + cognitoUser.getUsername());
res.status(200).json({ "status": 1, "message": "user: "+cognitoUser.getUsername() +" successfully added" });
});
});
app.post('/api/signin', (req, res) => {
var body = req.body;
var authenticationData = {
Username: body['username'],
Password: body['password'],
};
var authenticationDetails = new AmazonCognitoIdentity.AuthenticationDetails(
authenticationData
);
var userPool = new AmazonCognitoIdentity.CognitoUserPool(poolData);
var userData = {
Username: body['username'],
Pool: userPool,
};
var cognitoUser = new AmazonCognitoIdentity.CognitoUser(userData);
cognitoUser.authenticateUser(authenticationDetails, {
onSuccess: (result) => {
res.status(200).json({
"status": 1, "message": "user signed in successfully ", "data": {
"idToken": result.getIdToken().getJwtToken(),
"accessToken": result.getAccessToken().getJwtToken(),
"refreshToken": result.getRefreshToken().getToken(),
"shopId": "1"
}
});
},
onFailure: (err) => {
res.status(200).json({ "status": 0, "message": "User sign in failed "+err });
},
});
});
app.post('/api/signout', (req, res) => {
var body = req.body;
var cognitoIdentityServiceProvider = new AWS.CognitoIdentityServiceProvider();
var params = {
UserPoolId: poolData['UserPoolId'],
Username: body['username']
};
cognitoIdentityServiceProvider.adminUserGlobalSignOut(params, (err, result) => {
if (err) {
res.status(200).json({ "status": 0, "message": "Signout failed" });
}
else {
res.status(200).json({ "status": 1, "message": "User successfully signed out", "data": result });
}
});
});
app.post('/api/confirmotp', (req, res) => {
var body = req.body;
var userPool = new AmazonCognitoIdentity.CognitoUserPool(poolData);
var userData = {
Pool: userPool,
Username: body['username']
}
var cognitoUser = new AmazonCognitoIdentity.CognitoUser(userData);
cognitoUser.confirmRegistration(body['otp'], true, (err, result) => {
if (err) {
res.status(200).json({ "status": 0, "message": "Unable to verify OTP" });
}
else {
res.status(200).json({ "status": 1, "message": "User successfully verified" });
}
});
});
app.post('/api/resendotp', (req, res) => {
var body = req.body;
var cognitoIdentityServiceProvider = new AWS.CognitoIdentityServiceProvider();
var params = {
ClientId: poolData['ClientId'],
Username: body['username']
};
cognitoIdentityServiceProvider.resendConfirmationCode(params, (err, result) => {
if (err) {
res.status(200).json({ "status": 0, "message": "OTP sent failed" });
}
else {
res.status(200).json({ "status": 1, "message": "OTP send successfully"});
}
});
});
app.post('/api/changepassword', (req, res) => {
var body = req.body;
// var forceUpdate = body.isForceUpdate;
var authenticationDetails = new AmazonCognitoIdentity.AuthenticationDetails({
Username: body['username'],
Password: body['password'],
});
var userPool = new AmazonCognitoIdentity.CognitoUserPool(poolData);
var userData = {
Username: body['username'],
Pool: userPool
};
var cognitoUser = new AmazonCognitoIdentity.CognitoUser(userData);
cognitoUser.authenticateUser(authenticationDetails, {
onSuccess: (result) => {
cognitoUser.changePassword(body['password'], body['newpassword'], (err, result) => {
if (err) {
res.status(200).json({ "status": 0, "message": "Password Change Failed" });
} else {
res.status(200).json({ "status": 1, "message": "Password changed" });
}
});
},
onFailure: (err) => {
res.status(200).json({ "status": 0, "message": "Failed to authenticate" });
},
});
});
app.post('/api/forgotpassword', (req, res) => {
var body = req.body;
var cognitoIdentityServiceProvider = new AWS.CognitoIdentityServiceProvider();
var params = {
ClientId: poolData['ClientId'],
Username: body['username']
};
cognitoIdentityServiceProvider.forgotPassword(params, (err, result) => {
if (err) {
res.status(200).json({ "status": 0, "message": "Unable to send confirmation code", "data": err });
}
else {
res.status(200).json({ "status": 1, "message": "confirmation code send" });
}
});
});
app.post('/api/confirmforgotpassword', (req, res) => {
var body = req.body;
var cognitoIdentityServiceProvider = new AWS.CognitoIdentityServiceProvider();
var params = {
ClientId: poolData['ClientId'],
Username: body['username'],
ConfirmationCode: body['otp'],
Password: body['password']
};
cognitoIdentityServiceProvider.confirmForgotPassword(params, (err, result) => {
if (err) {
res.status(200).json({ "status": 0, "message": "Unable to verify the OTP", "data": err });
}
else {
res.status(200).json({ "status": 1, "message": "User password changed" });
}
});
});
//This API can only be accessed using the token from the signin API.
app.get('/api/helloworld', jwt_val.default(), (req, res) => {
res.status(200).json({ "status": 1, "message": "Hello Simulated World" });
});
module.exports.handler = serverless(app);