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(auth): refractor passwordless auth interface #12718

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
dda135d
feat(auth): reorganize passwordless API types
AllanZhengYP Dec 5, 2023
a7fd772
fix(auth): more strict types
AllanZhengYP Dec 6, 2023
7a68e72
feat(auth): integrate passwordless flow to existing API
AllanZhengYP Dec 7, 2023
7e774a0
feat(auth): attempt to use fn override to reduce return type
AllanZhengYP Dec 11, 2023
ff12951
feat(auth): merge authFlowType with passwordlessFlow(rev2.1)
AllanZhengYP Dec 12, 2023
b7533e1
Revert "feat(auth): merge authFlowType with passwordlessFlow(rev2.1)"
AllanZhengYP Dec 12, 2023
e5ad6e3
feat(auth): attempt to not highlight password when using passwordless
AllanZhengYP Dec 12, 2023
4ce4ef2
Revert "feat(auth): attempt to not highlight password when using pass…
AllanZhengYP Dec 12, 2023
9ebe8be
feat(auth): move passwordless options to options
AllanZhengYP Dec 13, 2023
1587fb6
feat(auth): attempt to make overload to generics
AllanZhengYP Dec 13, 2023
379822f
Revert "feat(auth): attempt to make overload to generics"
AllanZhengYP Dec 13, 2023
627bcf0
feat(auth): make signIn option stricter by disallowing unrelated options
AllanZhengYP Dec 13, 2023
11307bd
feat(auth): rename passwordlessFlow to passwordlessMethod
AllanZhengYP Dec 13, 2023
ece8164
feat(auth): move password connection type to the top-level auth input
AllanZhengYP Dec 14, 2023
b43be2a
feat(auth): move all passwordless options to single top-level option
AllanZhengYP Dec 18, 2023
9a31e50
feat(auth): remove any types in function overloading
AllanZhengYP Dec 20, 2023
42e5368
feat(auth): address feedbacks
AllanZhengYP Dec 22, 2023
1185dc9
fix(auth): keep using SignIn(Up)Input(Output) interface to support pa…
AllanZhengYP Dec 22, 2023
eabe033
chore(auth): fix docs
AllanZhengYP Dec 22, 2023
386068b
fix(auth): revert change to unionize SignIn(Up)Input(Output) interface
AllanZhengYP Dec 22, 2023
2c37458
feat(auth): remove passwordless from passwordless type names
AllanZhengYP Dec 22, 2023
6ec48d2
chore(auth): address feedbacks
AllanZhengYP Dec 28, 2023
c8c8015
chore(auth): apply more naming suggestions
AllanZhengYP Dec 28, 2023
c6a26ee
Revert "chore(auth): apply more naming suggestions"
AllanZhengYP Dec 28, 2023
300aeb5
chore(auth): fix some of typedoc links
AllanZhengYP Dec 28, 2023
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
14 changes: 12 additions & 2 deletions packages/auth/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ export {
forgetDevice,
fetchDevices,
autoSignIn,
signInWithOTP,
confirmSignInWithOTP,
AuthUser,
CodeDeliveryDetails,
UserAttributeKey,
Expand All @@ -46,8 +44,14 @@ export {
ResetPasswordInput,
SignInInput,
SignInWithRedirectInput,
SignInWithEmailAndMagicLinkInput,
SignInWithEmailAndOTPInput,
SignInWithSMSAndOTPInput,
SignOutInput,
SignUpInput,
SignUpWithEmailAndMagicLinkInput,
SignUpWithEmailAndOTPInput,
SignUpWithSMSAndOTPInput,
UpdateMFAPreferenceInput,
UpdatePasswordInput,
UpdateUserAttributesInput,
Expand All @@ -68,7 +72,13 @@ export {
ResetPasswordOutput,
SetUpTOTPOutput,
SignInOutput,
SignInWithEmailAndMagicLinkOutput,
SignInWithEmailAndOTPOutput,
SignInWithSMSAndOTPOutput,
SignUpOutput,
SignUpWithEmailAndMagicLinkOutput,
SignUpWithEmailAndOTPOutput,
SignUpWithSMSAndOTPOutput,
UpdateUserAttributesOutput,
SendUserAttributeVerificationCodeOutput,
UpdateUserAttributeOutput,
Expand Down
102 changes: 0 additions & 102 deletions packages/auth/src/providers/cognito/apis/confirmSignInWithOTP.ts

This file was deleted.

122 changes: 110 additions & 12 deletions packages/auth/src/providers/cognito/apis/signIn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,125 @@ import {
InitiateAuthException,
RespondToAuthChallengeException,
} from '../types/errors';
import { signInPasswordless } from './signInPasswordless';
import { signInWithCustomAuth } from './signInWithCustomAuth';
import { signInWithCustomSRPAuth } from './signInWithCustomSRPAuth';
import { signInWithSRP } from './signInWithSRP';
import { signInWithUserPassword } from './signInWithUserPassword';
import { assertUserNotAuthenticated } from '../utils/signInHelpers';
import {
assertUserNotAuthenticated,
isSignInWithEmailAndMagicLinkInput,
isSignInWithEmailAndOTPInput,
isSignInWithSMSAndOTPInput,
} from '../utils/signInHelpers';

import {
SignInWithPasswordInput,
SignInWithEmailAndMagicLinkInput,
SignInWithEmailAndOTPInput,
SignInWithSMSAndOTPInput,
} from '../types/inputs';
import {
SignInWithEmailAndMagicLinkOutput,
SignInWithEmailAndOTPOutput,
SignInWithPasswordOutput,
SignInWithSMSAndOTPOutput,
} from '../types/outputs';

import type { AuthValidationErrorCode } from '../../../errors/types/validation';
import type { confirmSignIn } from './confirmSignIn';

import { SignInInput, SignInOutput } from '../types';
/**
* Signs a user in
* Signs a user in with optional password. It uses either of the following sign-in flow:
* * 'USER_SRP_AUTH'
* * 'CUSTOM_WITH_SRP'
* * 'CUSTOM_WITHOUT_SRP'
* * 'USER_PASSWORD_AUTH'
*
* @param input - The SignInInput object
* @returns SignInOutput
* @throws service: {@link InitiateAuthException }, {@link RespondToAuthChallengeException }
* - Cognito service errors thrown during the sign-in process.
* @throws validation: {@link AuthValidationErrorCode } - Validation errors thrown when either username or password
* are not defined.
* @throws AuthTokenConfigException - Thrown when the token provider config is invalid.
* @param input - The {@link SignInWithPasswordInput} object
* @returns The {@link SignInWithPasswordOutput} object
* @throws service: {@link InitiateAuthException }, {@link RespondToAuthChallengeException } for Cognito service errors
* during the sign-in process.
* @throws AuthValidationErrorCode when `username` or `password` is invalid.
* see {@link AuthValidationErrorCode}
* @throws AuthTokenConfigException when the token provider config is invalid.
*/
export async function signIn(input: SignInInput): Promise<SignInOutput> {
const authFlowType = input.options?.authFlowType;
export function signIn(
input: SignInWithPasswordInput
): Promise<SignInWithPasswordOutput>;

/**
* Initiates a passwordless sign-in flow by sending a MagicLink to a registered email address. The sign-in flow is
* completed by calling the {@link confirmSignIn} API with the code extracted from the MagicLink delivered to the
* registered email address.
*
* @param input - The {@link SignInWithEmailAndMagicLinkInput} object
* @returns The {@link SignInWithEmailAndMagicLinkOutput} object
* @throws service: {@link InitiateAuthException }, {@link RespondToAuthChallengeException } for Cognito service errors
* during the sign-in process.
* @throws AuthValidationErrorCode when `username` or `passwordless` is invalid.
* see {@link AuthValidationErrorCode}
* @throws AuthTokenConfigException when the token provider config is invalid.
*/
export function signIn(
input: SignInWithEmailAndMagicLinkInput
): Promise<SignInWithEmailAndMagicLinkOutput>;

/**
* Initiates a passwordless sign-in flow by sending a one-time password to a registered email address. The sign-in flow
* is completed by calling the {@link confirmSignIn} API with the one-time password delivered to the registered email
* address.
*
* @param input - The {@link SignInWithEmailAndOTPInput} object
* @returns The {@link SignInWithEmailAndOTPOutput} object
* @throws service: {@link InitiateAuthException }, {@link RespondToAuthChallengeException } for Cognito service errors
* during the sign-in process.
* @throws AuthValidationErrorCode when `username` or `passwordless` is invalid.
* see {@link AuthValidationErrorCode}
* @throws AuthTokenConfigException when the token provider config is invalid.
*/
export function signIn(
input: SignInWithEmailAndOTPInput
): Promise<SignInWithEmailAndOTPOutput>;

/**
* Initiates a passwordless sign-in flow by sending a one-time password to a registered phone number via SMS. The
* sign-in flow is completed by calling the {@link confirmSignIn} API with the one-time password delivered to the
* registered phone number via SMS.
*
* @param input - The {@link SignInWithSMSAndOTPInput} object
* @returns The {@link SignInWithSMSAndOTPOutput} object
* @throws service: {@link InitiateAuthException }, {@link RespondToAuthChallengeException } for Cognito service errors
* during the sign-in process.
* @throws AuthValidationErrorCode when `username` or `passwordless` is invalid.
* see {@link AuthValidationErrorCode}
* @throws AuthTokenConfigException when the token provider config is invalid.
*/
export function signIn(
input: SignInWithSMSAndOTPInput
): Promise<SignInWithSMSAndOTPOutput>;

export async function signIn(
input:
| SignInWithPasswordInput
| SignInWithEmailAndMagicLinkInput
| SignInWithEmailAndOTPInput
| SignInWithSMSAndOTPInput
) {
await assertUserNotAuthenticated();
if (input.passwordless) {
if (isSignInWithEmailAndMagicLinkInput(input)) {
return signInPasswordless(input);
} else if (isSignInWithEmailAndOTPInput(input)) {
return signInPasswordless(input);
} else if (isSignInWithSMSAndOTPInput(input)) {
return signInPasswordless(input);
} else {
// TODO: implement validation error
throw new Error('SMS does not support MagicLink');
}
}
const authFlowType = input.options?.authFlowType;
switch (authFlowType) {
case 'USER_SRP_AUTH':
return signInWithSRP(input);
Expand Down
47 changes: 47 additions & 0 deletions packages/auth/src/providers/cognito/apis/signInPasswordless.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import {
SignInWithEmailAndMagicLinkInput,
SignInWithEmailAndOTPInput,
SignInWithSMSAndOTPInput,
} from '../types/inputs';
import {
SignInWithEmailAndMagicLinkOutput,
SignInWithEmailAndOTPOutput,
SignInWithSMSAndOTPOutput,
} from '../types/outputs';

/**
* @internal
*/
export function signInPasswordless(
input: SignInWithEmailAndMagicLinkInput
): Promise<SignInWithEmailAndMagicLinkOutput>;

/**
* @internal
*/
export function signInPasswordless(
input: SignInWithEmailAndOTPInput
): Promise<SignInWithEmailAndOTPOutput>;

/**
* @internal
*/
export function signInPasswordless(
input: SignInWithSMSAndOTPInput
): Promise<SignInWithSMSAndOTPOutput>;

/**
* @internal
*/
export async function signInPasswordless(
input:
| SignInWithEmailAndMagicLinkInput
| SignInWithEmailAndOTPInput
| SignInWithSMSAndOTPInput
) {
// TODO: needs implementation
return {} as any;
}
Loading
Loading