Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(web): Added functions to confirm user and get user attributes #151

Merged
merged 6 commits into from
Feb 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 100 additions & 8 deletions client/cognito-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,54 @@ export async function respondToAuthChallenge({
).then(extractChallengeResponse);
}

/**
* Confirms the sign-up of a user in Amazon Cognito.
*
* @param params - The parameters for confirming the sign-up.
* @param params.username - The username or alias (e-mail, phone number) of the user.
* @param params.confirmationCode - The confirmation code received by the user.
* @param [params.clientMetadata] - Additional metadata to be passed to the server.
* @param [params.abort] - An optional AbortSignal object that can be used to abort the request.
* @returns A promise that resolves to the response of the confirmation request.
*/
export async function confirmSignUp({
username,
confirmationCode,
clientMetadata,
abort,
}: {
username: string;
confirmationCode: string;
clientMetadata?: Record<string, string>;
abort?: AbortSignal;
}) {
const { fetch, cognitoIdpEndpoint, proxyApiHeaders, clientId, clientSecret } =
configure();
return fetch(
cognitoIdpEndpoint.match(AWS_REGION_REGEXP)
? `https://cognito-idp.${cognitoIdpEndpoint}.amazonaws.com/`
: cognitoIdpEndpoint,
{
headers: {
"x-amz-target": "AWSCognitoIdentityProviderService.ConfirmSignUp",
"content-type": "application/x-amz-json-1.1",
...proxyApiHeaders,
},
method: "POST",
body: JSON.stringify({
Username: username,
ConfirmationCode: confirmationCode,
ClientId: clientId,
ClientMetadata: clientMetadata,
...(clientSecret && {
SecretHash: await calculateSecretHash(username),
}),
}),
signal: abort,
}
).then(throwIfNot2xx);
}

export async function revokeToken({
refreshToken,
abort,
Expand Down Expand Up @@ -303,6 +351,42 @@ export async function getId({
.then((res) => res.json() as Promise<GetIdResponse | ErrorResponse>);
}

/**
* Retrieves the user attributes from the Cognito Identity Provider.
*
* @param abort - An optional `AbortSignal` object that can be used to abort the request.
* @returns A promise that resolves to an array of user attributes, where each attribute is represented by an object with `Name` and `Value` properties.
*/
export async function getUserAttributes({
abort,
accessToken,
}: {
abort?: AbortSignal;
accessToken?: string;
}): Promise<{ Name: string; Value: string }[]> {
const { fetch, cognitoIdpEndpoint, proxyApiHeaders } = configure();
const token = accessToken ?? (await retrieveTokens())?.accessToken;
return await fetch(
cognitoIdpEndpoint.match(AWS_REGION_REGEXP)
? `https://cognito-idp.${cognitoIdpEndpoint}.amazonaws.com/`
: cognitoIdpEndpoint,
{
headers: {
"x-amz-target": "AWSCognitoIdentityProviderService.GetUserAttributes",
"content-type": "application/x-amz-json-1.1",
...proxyApiHeaders,
},
method: "POST",
body: JSON.stringify({
AccessToken: token,
}),
signal: abort,
}
)
.then(throwIfNot2xx)
.then((res) => res.json() as Promise<{ Name: string; Value: string }[]>);
}

export async function getCredentialsForIdentity({
identityId,
abort,
Expand Down Expand Up @@ -402,13 +486,15 @@ export async function updateUserAttributes({
clientMetadata,
userAttributes,
abort,
accessToken,
}: {
userAttributes: { name: string; value: string }[];
clientMetadata?: Record<string, string>;
abort?: AbortSignal;
accessToken?: string;
}) {
const { fetch, cognitoIdpEndpoint, proxyApiHeaders } = configure();
const tokens = await retrieveTokens();
const token = accessToken ?? (await retrieveTokens())?.accessToken;
await fetch(
cognitoIdpEndpoint.match(AWS_REGION_REGEXP)
? `https://cognito-idp.${cognitoIdpEndpoint}.amazonaws.com/`
Expand All @@ -422,7 +508,7 @@ export async function updateUserAttributes({
},
method: "POST",
body: JSON.stringify({
AccessToken: tokens?.accessToken,
AccessToken: token,
ClientMetadata: clientMetadata,
UserAttributes: userAttributes.map(({ name, value }) => ({
Name: name,
Expand All @@ -438,13 +524,15 @@ export async function getUserAttributeVerificationCode({
attributeName,
clientMetadata,
abort,
accessToken,
}: {
attributeName: string;
clientMetadata?: Record<string, string>;
abort?: AbortSignal;
accessToken?: string;
}) {
const { fetch, cognitoIdpEndpoint, proxyApiHeaders } = configure();
const tokens = await retrieveTokens();
const token = accessToken ?? (await retrieveTokens())?.accessToken;
await fetch(
cognitoIdpEndpoint.match(AWS_REGION_REGEXP)
? `https://cognito-idp.${cognitoIdpEndpoint}.amazonaws.com/`
Expand All @@ -458,7 +546,7 @@ export async function getUserAttributeVerificationCode({
},
method: "POST",
body: JSON.stringify({
AccessToken: tokens?.accessToken,
AccessToken: token,
ClientMetadata: clientMetadata,
AttributeName: attributeName,
}),
Expand All @@ -471,13 +559,15 @@ export async function verifyUserAttribute({
attributeName,
code,
abort,
accessToken,
}: {
attributeName: string;
code: string;
abort?: AbortSignal;
accessToken?: string;
}) {
const { fetch, cognitoIdpEndpoint, proxyApiHeaders } = configure();
const tokens = await retrieveTokens();
const token = accessToken ?? (await retrieveTokens())?.accessToken;
await fetch(
cognitoIdpEndpoint.match(AWS_REGION_REGEXP)
? `https://cognito-idp.${cognitoIdpEndpoint}.amazonaws.com/`
Expand All @@ -490,7 +580,7 @@ export async function verifyUserAttribute({
},
method: "POST",
body: JSON.stringify({
AccessToken: tokens?.accessToken,
AccessToken: token,
AttributeName: attributeName,
Code: code,
}),
Expand All @@ -503,13 +593,15 @@ export async function setUserMFAPreference({
smsMfaSettings,
softwareTokenMfaSettings,
abort,
accessToken,
}: {
smsMfaSettings?: { enabled?: boolean; preferred?: boolean };
softwareTokenMfaSettings?: { enabled?: boolean; preferred?: boolean };
abort?: AbortSignal;
accessToken?: string;
}) {
const { fetch, cognitoIdpEndpoint, proxyApiHeaders } = configure();
const tokens = await retrieveTokens();
const token = accessToken ?? (await retrieveTokens())?.accessToken;
await fetch(
cognitoIdpEndpoint.match(AWS_REGION_REGEXP)
? `https://cognito-idp.${cognitoIdpEndpoint}.amazonaws.com/`
Expand All @@ -523,7 +615,7 @@ export async function setUserMFAPreference({
},
method: "POST",
body: JSON.stringify({
AccessToken: tokens?.accessToken,
AccessToken: token,
SMSMfaSettings: smsMfaSettings && {
Enabled: smsMfaSettings.enabled,
PreferredMfa: smsMfaSettings.preferred,
Expand Down
Loading