-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetDreamsDate.ts
33 lines (31 loc) ยท 1.13 KB
/
getDreamsDate.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
import { Context, APIGatewayProxyResult, APIGatewayEvent } from 'aws-lambda';
import { getDreamsDate, getUser } 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 username = decoded.sub;
const userData = await getUser(username)
if (userData?.username !== username || userData?.username === undefined) {
return {
statusCode:401,
body: JSON.stringify({
message: 'Invalid credentials.',
result: false,
})
}
}
const allData = await getDreamsDate()
return {
statusCode: 200,
body: JSON.stringify(allData)
}
} catch (err) {
console.error(err);
return {
statusCode : 401,
body : JSON.stringify({ message : err instanceof Error ? err.message : err }),
};
}
};