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

fix: flatten prop types auth #534

Merged
merged 4 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
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
7 changes: 7 additions & 0 deletions .changeset/good-suns-rescue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@aws-amplify/integration-tests': patch
'@aws-amplify/auth-construct-alpha': patch
'@aws-amplify/backend-auth': patch
---

Flatten loginWith type to improve autocompletion.
17 changes: 5 additions & 12 deletions packages/auth-construct/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,17 @@ export type AppleProviderProps = Omit<aws_cognito.UserPoolIdentityProviderAppleP

// @public
export type AuthProps = {
loginWith: BasicLoginOptions & ExternalProviderProps;
loginWith: {
email?: EmailLogin;
phone?: PhoneNumberLogin;
externalProviders?: ExternalProviderOptions;
};
userAttributes?: StandardAttributes;
multifactor?: MFA;
accountRecovery?: keyof typeof aws_cognito.AccountRecovery;
outputStorageStrategy?: BackendOutputStorageStrategy<AuthOutput>;
};

// @public
export type BasicLoginOptions = {
email?: EmailLogin;
phone?: PhoneNumberLogin;
};

// @public
export type EmailLogin = true | EmailLoginSettings;

Expand All @@ -66,11 +64,6 @@ export type ExternalProviderOptions = {
logoutUrls?: string[];
};

// @public
export type ExternalProviderProps = {
externalProviders?: ExternalProviderOptions;
};

// @public
export type FacebookProviderProps = Omit<aws_cognito.UserPoolIdentityProviderFacebookProps, 'userPool'>;

Expand Down
2 changes: 0 additions & 2 deletions packages/auth-construct/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
export {
AuthProps,
BasicLoginOptions,
EmailLogin,
EmailLoginSettings,
GoogleProviderProps,
Expand All @@ -10,7 +9,6 @@ export {
OidcProviderProps,
SamlProviderProps,
ExternalProviderOptions,
ExternalProviderProps,
MFA,
MFASettings,
PhoneNumberLogin,
Expand Down
55 changes: 22 additions & 33 deletions packages/auth-construct/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,26 +52,6 @@ export type PhoneNumberLogin =
*/
verificationMessage?: (code: string) => string;
};
/**
* Basic login options require at least email or phone number.
* Additional settings may be configured, such as email messages or sms verification messages.
*/
export type BasicLoginOptions = {
/**
* Email login options.
*
* If true, email login will be enabled with default settings.
* If settings are provided, email login will be enabled with the specified settings.
*/
email?: EmailLogin;
/**
* Phone number login options.
*
* If true, phone number login will be enabled with default settings.
* If settings are provided, phone number login will be enabled with the specified settings.
*/
phone?: PhoneNumberLogin;
};

/**
* Configure the MFA types that users can use. Ignored if MFA mode is set to OFF.
Expand Down Expand Up @@ -207,16 +187,6 @@ export type ExternalProviderOptions = {
logoutUrls?: string[];
};

/**
* External auth provider.
*/
export type ExternalProviderProps = {
/**
* Configure OAuth, OIDC, and SAML login providers
*/
externalProviders?: ExternalProviderOptions;
};

/**
* Union type of all supported auth trigger events
*/
Expand All @@ -227,9 +197,28 @@ export type TriggerEvent = (typeof triggerEvents)[number];
*/
export type AuthProps = {
/**
* Specify the allowed login mechanisms
*/
loginWith: BasicLoginOptions & ExternalProviderProps;
* Specify how you would like users to log in. You can choose from email, phone, and even external providers such as LoginWithAmazon.
*/
loginWith: {
/**
* Email login options.
*
* If true, email login will be enabled with default settings.
* If settings are provided, email login will be enabled with the specified settings.
*/
email?: EmailLogin;
/**
* Phone number login options.
*
* If true, phone number login will be enabled with default settings.
* If settings are provided, phone number login will be enabled with the specified settings.
*/
phone?: PhoneNumberLogin;
/**
* Configure OAuth, OIDC, and SAML login providers
*/
externalProviders?: ExternalProviderOptions;
};
/**
* The set of attributes that are required for every user in the user pool. Read more on attributes here - https://docs.aws.amazon.com/cognito/latest/developerguide/user-pool-settings-attributes.html
* @default - email/phone will be added as required user attributes if they are included as login methods
Expand Down
11 changes: 3 additions & 8 deletions packages/backend-auth/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ import { AppleProviderProps } from '@aws-amplify/auth-construct-alpha';
import { AuthProps } from '@aws-amplify/auth-construct-alpha';
import { AuthResources } from '@aws-amplify/plugin-types';
import { BackendSecret } from '@aws-amplify/plugin-types';
import { BasicLoginOptions } from '@aws-amplify/auth-construct-alpha';
import { ConstructFactory } from '@aws-amplify/plugin-types';
import { ExternalProviderOptions } from '@aws-amplify/auth-construct-alpha';
import { ExternalProviderProps } from '@aws-amplify/auth-construct-alpha';
import { FacebookProviderProps } from '@aws-amplify/auth-construct-alpha';
import { FunctionResources } from '@aws-amplify/plugin-types';
import { GoogleProviderProps } from '@aws-amplify/auth-construct-alpha';
Expand Down Expand Up @@ -41,16 +39,13 @@ export type AppleProviderFactoryProps = Omit<AppleProviderProps, 'clientId' | 't
};

// @public
export type AuthLoginWithFactoryProps = BasicLoginOptions & ExternalProviderFactoryProps;
export type AuthLoginWithFactoryProps = Omit<AuthProps['loginWith'], 'externalProviders'> & {
externalProviders?: ExternalProviderSpecificFactoryProps;
};

// @public
export const defineAuth: (props: AmplifyAuthFactoryProps) => ConstructFactory<AmplifyAuth & ResourceProvider<AuthResources>>;

// @public
export type ExternalProviderFactoryProps = Omit<ExternalProviderProps, 'externalProviders'> & {
externalProviders?: ExternalProviderSpecificFactoryProps;
};

// @public
export type ExternalProviderGeneralFactoryProps = Omit<ExternalProviderOptions, 'signInWithApple' | 'loginWithAmazon' | 'facebook' | 'oidc' | 'google'>;

Expand Down
12 changes: 4 additions & 8 deletions packages/backend-auth/src/translate_auth_props.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@ import {
import { describe, it } from 'node:test';
import { AuthLoginWithFactoryProps } from './types.js';
import { Construct } from 'constructs';
import {
BasicLoginOptions,
ExternalProviderProps,
PhoneNumberLogin,
} from '@aws-amplify/auth-construct-alpha';
import { AuthProps, PhoneNumberLogin } from '@aws-amplify/auth-construct-alpha';
import { SecretValue } from 'aws-cdk-lib';
import assert from 'node:assert';
import { translateToAuthConstructLoginWith } from './translate_auth_props.js';
Expand Down Expand Up @@ -91,7 +87,7 @@ void describe('translateToAuthConstructLoginWith', () => {
backendResolver
);

const expected: BasicLoginOptions & ExternalProviderProps = {
const expected: AuthProps['loginWith'] = {
phone,
externalProviders: {
google: {
Expand Down Expand Up @@ -136,7 +132,7 @@ void describe('translateToAuthConstructLoginWith', () => {
backendResolver
);

const expected: BasicLoginOptions & ExternalProviderProps = {
const expected: AuthProps['loginWith'] = {
phone,
externalProviders: {
callbackUrls: callbackUrls,
Expand All @@ -155,7 +151,7 @@ void describe('translateToAuthConstructLoginWith', () => {
backendResolver
);

const expected: BasicLoginOptions & ExternalProviderProps = {
const expected: AuthProps['loginWith'] = {
phone,
};
assert.deepStrictEqual(translated, expected);
Expand Down
9 changes: 4 additions & 5 deletions packages/backend-auth/src/translate_auth_props.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import {
AmazonProviderProps,
AppleProviderProps,
BasicLoginOptions,
ExternalProviderProps,
AuthProps,
FacebookProviderProps,
GoogleProviderProps,
OidcProviderProps,
Expand All @@ -26,9 +25,9 @@ import {
export const translateToAuthConstructLoginWith = (
authFactoryLoginWith: AuthLoginWithFactoryProps,
backendSecretResolver: BackendSecretResolver
): BasicLoginOptions & ExternalProviderProps => {
const result: BasicLoginOptions & ExternalProviderProps =
authFactoryLoginWith as BasicLoginOptions;
): AuthProps['loginWith'] => {
const result: AuthProps['loginWith'] =
authFactoryLoginWith as AuthProps['loginWith'];
if (!authFactoryLoginWith.externalProviders) {
return result;
}
Expand Down
18 changes: 7 additions & 11 deletions packages/backend-auth/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import {
AmazonProviderProps,
AppleProviderProps,
BasicLoginOptions,
AuthProps,
ExternalProviderOptions,
ExternalProviderProps,
FacebookProviderProps,
GoogleProviderProps,
OidcProviderProps,
Expand Down Expand Up @@ -133,17 +132,14 @@ export type ExternalProviderSpecificFactoryProps =
};

/**
* External provider properties.
* Auth factory loginWith attribute.
*/
export type ExternalProviderFactoryProps = Omit<
ExternalProviderProps,
export type AuthLoginWithFactoryProps = Omit<
AuthProps['loginWith'],
'externalProviders'
> & {
/**
* External provider properties.
*/
externalProviders?: ExternalProviderSpecificFactoryProps;
};

/**
* Auth factory loginWith attribute.
*/
export type AuthLoginWithFactoryProps = BasicLoginOptions &
ExternalProviderFactoryProps;
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@
{
"Fn::Sub": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}"
},
"/6ba9f860a2dc910090281e5a118e15a973a885091f83213d560125a18786aa31.json"
"/f0d3060f91da8f5b8062d42780fd3fba7c2b533136e30e7880ac9fc32efdf58c.json"
]
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@
"backendId": "testAppId",
"branchName": "testBranchName",
"secretName": "testAmazonId",
"secretLastUpdated": 1698693218594
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems problematic. The integration tests should be passing in args to synth such that this is deterministic

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created an issue: #539
Should be a quick fix if you want to pick it up :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agreed, i thought it was odd; looks like Hoang has picked it up

"secretLastUpdated": 1698712367508
},
"UpdateReplacePolicy": "Delete",
"DeletionPolicy": "Delete"
Expand All @@ -222,7 +222,7 @@
"backendId": "testAppId",
"branchName": "testBranchName",
"secretName": "testAmazonSecret",
"secretLastUpdated": 1698693218595
"secretLastUpdated": 1698712367509
},
"UpdateReplacePolicy": "Delete",
"DeletionPolicy": "Delete"
Expand All @@ -239,7 +239,7 @@
"backendId": "testAppId",
"branchName": "testBranchName",
"secretName": "testFacebookId",
"secretLastUpdated": 1698693218595
"secretLastUpdated": 1698712367509
},
"UpdateReplacePolicy": "Delete",
"DeletionPolicy": "Delete"
Expand All @@ -256,7 +256,7 @@
"backendId": "testAppId",
"branchName": "testBranchName",
"secretName": "testFacebookSecret",
"secretLastUpdated": 1698693218595
"secretLastUpdated": 1698712367509
},
"UpdateReplacePolicy": "Delete",
"DeletionPolicy": "Delete"
Expand All @@ -273,7 +273,7 @@
"backendId": "testAppId",
"branchName": "testBranchName",
"secretName": "testGoogleId",
"secretLastUpdated": 1698693218595
"secretLastUpdated": 1698712367509
},
"UpdateReplacePolicy": "Delete",
"DeletionPolicy": "Delete"
Expand All @@ -290,7 +290,7 @@
"backendId": "testAppId",
"branchName": "testBranchName",
"secretName": "testGoogleSecret",
"secretLastUpdated": 1698693218595
"secretLastUpdated": 1698712367510
},
"UpdateReplacePolicy": "Delete",
"DeletionPolicy": "Delete"
Expand Down