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

Convert useAuthenticator hook & services to public APIs #532

Merged
merged 34 commits into from
Oct 21, 2021

Conversation

ericclemmons
Copy link
Contributor

@ericclemmons ericclemmons commented Oct 19, 2021

Description of changes:

This PR paves the way for custom components & public APIs using React's useAuthenticator hook: #552

  • <AuthenticatorProvider {...initialState, components, loginMechanisms, services } />
  • useAuthenticator no longer returns [state, send] and instead returns only public APIs.
  • Replace state.matches(...) with route (signIn | verifyUser | ...). This doesn't include pending states, which will be indicated via an isPending facade.
  • Still providing _send and _state but /** @deprecated */ so we can track & remove these private values.
  • Add isPending facade. We want to avoid any usage of state.matches(...) in components!
  • Rename signIn and signUp facades to transitionToSignIn or toSignIn or gotoSignIn or something for @wlee221
  • Get React tests passing with the current refactor
  • Get all tests working

Future Changes

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@ericclemmons ericclemmons self-assigned this Oct 19, 2021
@ericclemmons ericclemmons added Authenticator An issue or a feature-request for an Authenticator UI Component feature-request Request a new feature labels Oct 19, 2021
@ericclemmons ericclemmons force-pushed the useAuthenticator-refactor branch 2 times, most recently from 8e61ae5 to 2d7d0b3 Compare October 19, 2021 20:31
@wlee221 wlee221 self-requested a review October 19, 2021 21:00
Comment on lines +20 to +21
const useAuthenticatorValue = ({
components: customComponents,
Copy link
Contributor

Choose a reason for hiding this comment

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

Will this now run on import, instead of when the Authenticator component is initialized?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good question – this is still created at run-time when someone provides:

<AuthenticatorProvider  {...components, services} />

It's not invoked as a side-effect here.

@ericclemmons ericclemmons marked this pull request as ready for review October 20, 2021 21:32
@ericclemmons
Copy link
Contributor Author

I'm going to get tests passing on this refactor and then do a PR documenting & supporting this use-case.

That way, that PR can be done to support multiple frameworks.

@ericclemmons
Copy link
Contributor Author

ericclemmons commented Oct 21, 2021

@ErikCH One of the changes I need to make for Vue is so that there are both <form @input @submit.prevent> handlers so that we only call updateForm({ name, value }) & submitForm, respectively:

const submit = (e: Event): void => {
const formData = new FormData(<HTMLFormElement>e.target);
send({
type: 'SUBMIT',
//@ts-ignore
data: {
//@ts-ignore
...Object.fromEntries(formData),
username,
},
});
};

@ericclemmons ericclemmons temporarily deployed to ci October 21, 2021 17:21 Inactive
@ericclemmons ericclemmons temporarily deployed to ci October 21, 2021 17:21 Inactive
@ericclemmons ericclemmons temporarily deployed to ci October 21, 2021 17:21 Inactive
@ericclemmons ericclemmons changed the title Refactor useAuthenticator to support custom Sign Up fields Convert useAuthenticator hook & services to public APIs Oct 21, 2021
@aws-amplify aws-amplify deleted a comment from lgtm-com bot Oct 21, 2021
@aws-amplify aws-amplify deleted a comment from lgtm-com bot Oct 21, 2021
@aws-amplify aws-amplify deleted a comment from lgtm-com bot Oct 21, 2021
@aws-amplify aws-amplify deleted a comment from lgtm-com bot Oct 21, 2021
@aws-amplify aws-amplify deleted a comment from lgtm-com bot Oct 21, 2021
@aws-amplify aws-amplify deleted a comment from lgtm-com bot Oct 21, 2021
@aws-amplify aws-amplify deleted a comment from lgtm-com bot Oct 21, 2021
@aws-amplify aws-amplify deleted a comment from lgtm-com bot Oct 21, 2021
@aws-amplify aws-amplify deleted a comment from lgtm-com bot Oct 21, 2021
@aws-amplify aws-amplify deleted a comment from lgtm-com bot Oct 21, 2021
@aws-amplify aws-amplify deleted a comment from lgtm-com bot Oct 21, 2021
@aws-amplify aws-amplify deleted a comment from lgtm-com bot Oct 21, 2021
@aws-amplify aws-amplify deleted a comment from lgtm-com bot Oct 21, 2021
@aws-amplify aws-amplify deleted a comment from lgtm-com bot Oct 21, 2021
@@ -3,6 +3,7 @@
<base-wrapper v-bind="$attrs">
<base-form
data-amplify-authenticator-confirmsignin
@input="onInput"
Copy link
Contributor

Choose a reason for hiding this comment

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

This looks fine


export type ProviderProps = AuthenticatorMachineOptions & {
components?: typeof defaultComponents;
services?: undefined;
Copy link
Contributor

Choose a reason for hiding this comment

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

What is this going to be?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Something like:

services: {
  signUp({ username, password }) {
    return Auth.signUp({ username, password }, myCustomOptions);
  },
  validateSignUp(data) {
    // run custom validation
  }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This will be used in #552 to solve #246

Copy link
Contributor

@ErikCH ErikCH left a comment

Choose a reason for hiding this comment

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

The Vue changes looks good. Looks like this will take some further updates on the Vue side to start using this new updateForm and submitForm

Copy link
Contributor

@wlee221 wlee221 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, no blockers!

@@ -38,6 +38,9 @@ jobs:
- name: Install packages
run: yarn --frozen-lockfile

- name: Lint packages
run: yarn workspace ${{ matrix.package }} lint
Copy link
Contributor

Choose a reason for hiding this comment

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

Then should this be done after yarn [framework]-example build on line 102?


const { challengeName, remoteError } = actorState.context as SignInContext;
Copy link
Contributor

Choose a reason for hiding this comment

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

NON_BLOCKING: How do you envision exposing validationErrors? I thought validate* as fire and forget functions from customer-end that doesn't return back to the customer:

const services = { validateSignUp };

return withAuthenticator(app, { services});


const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
const { name, value } = event.target;
updateForm({ name, value });
Copy link
Contributor

Choose a reason for hiding this comment

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

I love this!

]);

const isPending =
state.hasTag('pending') || getActorState(state)?.hasTag('pending');
Copy link
Contributor

Choose a reason for hiding this comment

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

Comment on lines -391 to +399
const { confirmation_code: code } = event.data;
const { confirmation_code: code } = context.formValues;
Copy link
Contributor

Choose a reason for hiding this comment

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

Right, but I'd be careful about this though because we were inconsistent about sending CHANGE events.

I see that you've bridged that gap on React/Vue -- I'll follow it up from Angular as well.

<Form
data-amplify-authenticator-confirmsignin=""
method="post"
onSubmit={(event) => {

@ericclemmons
Copy link
Contributor Author

@wlee221 To answer this:

NON_BLOCKING: How do you envision exposing validationErrors? I thought validate* as fire and forget functions from customer-end that doesn't return back to the customer:

It would be available via:

const { error, validationErrors /* or formErrors? */ } = useAuthenticator();

The service is still fire-and-forget, but on submit validation still has to pass and there validationErrors will be {} or { username: 'Invalid length' } or something.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Authenticator An issue or a feature-request for an Authenticator UI Component feature-request Request a new feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants