-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
137 lines (114 loc) · 4.12 KB
/
index.ts
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
import AWS from 'aws-sdk';
AWS.config.update({
region: "eu-north-1"
});
const dynamoDB = new AWS.DynamoDB.DocumentClient();
const dynamoDBTableName = 'UserInfo';
const signupPath = '/signUp';
const loginPath = '/LogIn';
exports.handler = async function(event) {
console.log('Request Event: ', event);
let response;
switch (true) {
case event.httpMethod === 'POST' && event.path === signupPath:
response = await signUp(JSON.parse(event.body));
break;
case event.httpMethod === 'POST' && event.path === loginPath:
response = await LogIn(JSON.parse(event.body));
break;
default:
response = buildResponse(404, '404 Not Found');
}
return response;
};
async function signUp(requestBody) {
const { UserName: userName, Role: role, Name: name, Email: email, Password: password } = requestBody;
const validEmail = isValidEmail(email);
if (!validEmail) {
return buildResponse(400, { Operation: 'SignUp', Message: 'Invalid Email', Error: 'Invalid email address' });
}
const getParams = {
TableName: dynamoDBTableName,
Key: {
'UserName': userName,
'Role' : role
}
};
try {
const data = await dynamoDB.get(getParams).promise();
if (data.Item) {
return buildResponse(400, { Operation: 'SignUp', Message: 'Username exists', Error: 'User already exists' });
} else {
const putParams = {
TableName: dynamoDBTableName,
Item: {
'UserName': userName,
'Role' : role,
'Name' : name,
'Email': email,
'Password': password}
};
await dynamoDB.put(putParams).promise();
const body = {
Operation: 'SignUp',
Message: 'Signed in successfully',
Item: { 'UserName': userName, 'Role' : role, 'Email': email }
};
return buildResponse(200, body);
}
} catch (error) {
console.error('Error: ', error);
return buildResponse(500, { Operation: 'SignUp', Message: 'FAILURE', Error: error.toString() });
}
}
async function LogIn(requestBody) {
const username = requestBody.UserName;
const password = requestBody.Password;
const role = requestBody.Role;
const getParams = {
TableName: dynamoDBTableName,
Key: {
'UserName': username,
'Role': role
}
};
try {
const data = await dynamoDB.get(getParams).promise();
if (data.Item) {
if (password === data.Item.Password) {
const body = {
Operation: 'LogIn',
Message: 'Logged in successfully, Welcome!',
Item: { 'UserName': username, 'Role' : role, 'Email': requestBody.Email }
};
return buildResponse(200, body);
} else {
return buildResponse(400, { Operation: 'LogIn', Message: 'Invalid Credentials.', Error: 'Invalid Credentials' });
}
} else {
return buildResponse(400, { Operation: 'LogIn', Message: 'Username does not exists.', Error: 'User does not exist' });
}
} catch (error) {
console.error('Error: ', error);
return buildResponse(500, { Operation: 'LogIn', Message: 'FAILURE', Error: error.toString() });
}
}
function buildResponse(statusCode, body) {
return {
statusCode: statusCode,
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(body)
};
}
function isValidEmail(email) {
const atposition = email.indexOf('@');
const dotposition = email.lastIndexOf('.');
if (atposition < 1 || dotposition < atposition + 2 || dotposition + 2 >= email.length) {
console.error("Invalid Email.");
return false;
} else {
return true;
}
}