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

chore(auth): add authenticated client side validation #12033

Merged

Conversation

israx
Copy link
Member

@israx israx commented Sep 13, 2023

Description of changes

This PR adds client side validation for authenticated users that try to sign-in again.

Other changes:
Improve error messaging for users that call APIs that require the user to be authenticated.

Issue #, if available

Description of how you validated changes

tested change in a sample app, where I was able to get an error when trying to call sign-in while authenticated.

Checklist

  • PR description included
  • yarn test passes
  • Tests are changed or added
  • Relevant documentation is changed or added (and PR referenced)

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@israx israx requested a review from a team as a code owner September 13, 2023 20:56
Copy link
Contributor

@elorzafe elorzafe left a comment

Choose a reason for hiding this comment

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

minor thing to add

Copy link
Contributor

@elorzafe elorzafe left a comment

Choose a reason for hiding this comment

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

Sorry @israx

can we add also this check for signInWithRedirect

Thanks!

@israx israx force-pushed the sign-in-authenticated-validation branch from aff0a0a to b3be14e Compare September 14, 2023 13:34
elorzafe
elorzafe previously approved these changes Sep 14, 2023
Copy link
Contributor

@jimblanc jimblanc left a comment

Choose a reason for hiding this comment

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

Looks good, some nits

packages/auth/src/providers/cognito/utils/signInHelpers.ts Outdated Show resolved Hide resolved
packages/auth/src/providers/cognito/utils/signInHelpers.ts Outdated Show resolved Hide resolved
packages/auth/src/providers/cognito/utils/types.ts Outdated Show resolved Hide resolved
packages/auth/src/providers/cognito/utils/types.ts Outdated Show resolved Hide resolved
packages/auth/src/providers/cognito/utils/types.ts Outdated Show resolved Hide resolved
@@ -630,3 +633,19 @@ export function isMFATypeEnabled(
if (!mfaTypes) return false;
return mfaTypes.includes(mfaType);
}

export async function isUserAuthenticated() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit pick but the current name makes it sound like this behaves like a predicate (i.e. returns a bool to know if user is signed in).

Suggested change
export async function isUserAuthenticated() {
export async function assertUserNotAuthenticated() {

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah agree. Sounds more like a predicate. I changed it already. Thanks

@israx israx force-pushed the sign-in-authenticated-validation branch from fe68ddf to e33e7c7 Compare September 15, 2023 13:18
let authUser: AuthUser | undefined;
try {
authUser = await getCurrentUser();
} catch (error) {}
Copy link
Member

Choose a reason for hiding this comment

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

Should we do something with this catch? Maybe log?

Copy link
Member Author

Choose a reason for hiding this comment

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

good point. If getCurrentUser throws it's because no tokens were found. So in the context of sign-in that is expected always. So logging something doesn't really help here.

@israx israx merged commit c7dfaf2 into aws-amplify:next/release Sep 15, 2023
14 checks passed
@TeoChi-CF
Copy link

To bad as this change made no longer possible to revalidate user password :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants