-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyDreams.ts
43 lines (41 loc) ยท 1.55 KB
/
myDreams.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
import { Context, APIGatewayProxyResult, APIGatewayEvent } from 'aws-lambda';
import { getUser, myDreams } from './mongo-modules';
import jwt from 'jsonwebtoken';
export const handler = async (event: APIGatewayEvent, context: Context): Promise<APIGatewayProxyResult> => {
try {
const token = event.headers.authorization?.split(' ')[1];
const decoded = jwt.verify(token, process.env.JWT_SECRET);
let user = decoded.sub;
const userData = await getUser(user)
if (userData?.username !== user || userData?.username === undefined) {
return {
statusCode:401,
headers: {
"Access-Control-Allow-Headers" : "*",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "OPTIONS,POST,GET"
},
body: JSON.stringify({
message: 'Invalid credentials.',
result: false,
})
}
}
const data = await myDreams(user)
return {
statusCode : 200,
headers: {
"Access-Control-Allow-Headers" : "*",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "OPTIONS,POST,GET"
},
body : JSON.stringify(data),
};
} catch (err) {
console.error(err);
return {
statusCode : 500,
body : JSON.stringify({ message : err instanceof Error ? err.message : err }),
};
}
};