Skip to content
This repository was archived by the owner on Sep 25, 2025. It is now read-only.

Commit 081dbd6

Browse files
committed
updated Backend
1 parent a4ff4e1 commit 081dbd6

1 file changed

Lines changed: 18 additions & 10 deletions

File tree

backend/functions/user/getUserInfo.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,30 @@
1+
import { CognitoIdentityProviderClient, GetUserCommand } from '@aws-sdk/client-cognito-identity-provider'
12
import { APIGatewayProxyEventV2WithJWTAuthorizer, APIGatewayProxyResultV2 } from 'aws-lambda'
23
import { error, internalServerError, unAuthorized } from '../../utils/httpError'
34

5+
const cognitoClient = new CognitoIdentityProviderClient({})
6+
47
export const handler = async (event: APIGatewayProxyEventV2WithJWTAuthorizer): Promise<APIGatewayProxyResultV2> => {
58
try {
6-
const claims = event.requestContext.authorizer?.jwt?.claims
7-
if (!claims) return error(unAuthorized(), 'ERR_GET_USER_UNAUTHORIZED')
9+
const authHeader = event.headers.authorization || event.headers.Authorization
10+
if (!authHeader) return error(unAuthorized(), 'ERR_GET_USER_NO_AUTH_HEADER')
811

9-
const userInfo = {
10-
// username: claims['cognito:username'],
11-
// email: claims.email,
12-
// sub: claims.sub,
13-
// email_verified: claims.email_verified,
14-
...claims,
15-
}
12+
const token = authHeader.split(' ')[1] // 'Bearer xxx'
13+
if (!token) return error(unAuthorized(), 'ERR_GET_USER_NO_TOKEN')
14+
15+
const getUserCommand = new GetUserCommand({ AccessToken: token })
16+
const response = await cognitoClient.send(getUserCommand)
17+
18+
const userAttributes = Object.fromEntries(
19+
(response.UserAttributes || []).map((attr) => [attr.Name, attr.Value])
20+
)
1621

1722
return {
1823
statusCode: 200,
19-
body: JSON.stringify(userInfo),
24+
body: JSON.stringify({
25+
username: response.Username,
26+
...userAttributes,
27+
}),
2028
}
2129
} catch (err) {
2230
return error(internalServerError((err as Error).message), 'ERR_GET_USER_INTERNAL_SERVER_ERROR')

0 commit comments

Comments
 (0)