|
1 | 1 | import { CognitoIdentityProviderClient, GetUserCommand } from '@aws-sdk/client-cognito-identity-provider' |
2 | | -import { APIGatewayProxyEventV2WithJWTAuthorizer, APIGatewayProxyResultV2 } from 'aws-lambda' |
| 2 | +import { APIGatewayProxyEventV2, APIGatewayProxyResultV2 } from 'aws-lambda' |
3 | 3 | import { error, internalServerError, unAuthorized } from '../../utils/httpError' |
4 | 4 |
|
5 | 5 | const cognitoClient = new CognitoIdentityProviderClient({}) |
6 | 6 |
|
7 | | -export const handler = async (event: APIGatewayProxyEventV2WithJWTAuthorizer): Promise<APIGatewayProxyResultV2> => { |
| 7 | +export const handler = async (event: APIGatewayProxyEventV2): Promise<APIGatewayProxyResultV2> => { |
8 | 8 | try { |
9 | 9 | const authHeader = event.headers?.Authorization ?? event.headers?.authorization |
10 | 10 |
|
11 | | - if (!authHeader || !authHeader.startsWith('Bearer ')) |
12 | | - return { |
13 | | - statusCode: 401, |
14 | | - body: JSON.stringify({ |
15 | | - error: 'Unauthorized', |
16 | | - message: 'No valid authorization header provided', |
17 | | - }), |
18 | | - } |
| 11 | + if (!authHeader || !authHeader.startsWith('Bearer ')) return error(unAuthorized(), 'ERR_GET_USER_UNAUTHORIZED') |
19 | 12 |
|
20 | 13 | const accessToken = authHeader.substring('Bearer '.length) |
21 | 14 |
|
22 | 15 | const getUserCommand = new GetUserCommand({ AccessToken: accessToken }) |
23 | | - const response = await cognitoClient.send(getUserCommand) |
| 16 | + const result = await cognitoClient.send(getUserCommand) |
24 | 17 |
|
25 | | - const userAttributes = Object.fromEntries( |
26 | | - (response.UserAttributes ?? []).map((attr) => [attr.Name, attr.Value]) |
27 | | - ) |
| 18 | + const userAttributes = Object.fromEntries((result.UserAttributes ?? []).map((attr) => [attr.Name, attr.Value])) |
28 | 19 |
|
29 | 20 | return { |
30 | 21 | statusCode: 200, |
31 | 22 | body: JSON.stringify({ |
32 | | - username: response.Username, |
| 23 | + username: result.Username, |
33 | 24 | ...userAttributes, |
34 | 25 | }), |
35 | 26 | } |
|
0 commit comments