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

(DOCSP-33534) Add authentication hooks to React Native docs #3037

Merged
merged 37 commits into from
Oct 19, 2023

Conversation

krollins-mdb
Copy link
Collaborator

@krollins-mdb krollins-mdb commented Oct 2, 2023

Pull Request Info

This is a somewhat large PR, but don't let the lines changed intimidate you: that's mostly from the auto-generated package-lock.json file.

I only address some of the needed auth changes in this PR. I created a small Jira epic to track the other work.

Major changes:

  • New test files
    • Link identities
    • Anonymous login
    • Email/pass login
  • Content pages
    • Authenticate users
    • Manage email/password users
    • Link user identities

The test suite has an issue with randomly aborting. I have a Jira ticket to track fixing it.

Jira

Staged Changes

Much of the content and examples for the new hook reference tables comes from Andrew Meyer's additions to the Realm.js repo.

  • Authenticate Users
    • Revamped these sections: Configure User Authentication in Client, Anonymous User, and Email/Password User.
    • New reference table for useAuth.
  • Manage Email/Password Users
    • Lots of updates to the examples and content throughout this page.
    • New reference table for useEmailPasswordAuth.
  • Link User Identities
    • New procedure with updated examples.
    • Trying a new thing where I break a whole React Native component into a procedure. The idea here is to give devs an idea of what a user management component could look like.

Review Guidelines

REVIEWING.md

Animal Wearing a Hat

@github-actions
Copy link

github-actions bot commented Oct 2, 2023

Readability for Commit Hash: 7aa2f64

You can see any previous Readability scores (if they exist) by looking
at the comment's history.

Readability scores for changed documents:

  • source/sdk/react-native/manage-users/authenticate-users: Grade Level: 9.0, Reading Ease: 51.24
  • source/sdk/react-native/manage-users/manage-email-password-users: Grade Level: 9.2, Reading Ease: 50.84
  • source/sdk/react-native/manage-users/link-user-identities: Grade Level: 6.6, Reading Ease: 62.95

For Grade Level, aim for 8 or below.

For Reading Ease scores, aim for 60 or above:

Score Difficulty
90-100 Very Easy
80-89 Easy
70-79 Fairly Easy
60-69 Medium
50-59 Fairly Hard
30-49 Hard
0-29 Very Hard

For help improving readability, try Hemingway App.

@krollins-mdb krollins-mdb changed the title (DOCSP-27838) Add authentication hooks to React Native docs (DOCSP-33534) Add authentication hooks to React Native docs Oct 11, 2023
@krollins-mdb krollins-mdb marked this pull request as ready for review October 11, 2023 19:18
@krollins-mdb krollins-mdb requested review from takameyer and removed request for takameyer October 11, 2023 19:18
Copy link
Collaborator

@takameyer takameyer left a comment

Choose a reason for hiding this comment

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

Great work here, but I have some requests that should be addressed before this goes live.

#. In the component passed to the ``UserProvider.fallback`` prop, authenticate a
user with the ``useEmailPasswordAuth()`` hook.
This example uses email/password authentication, but you could use any of the
``useAuth`` methods.
Copy link
Collaborator

Choose a reason for hiding this comment

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

The following example is not showing usage of the useEmailPasswordAuth hook.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thank you for noticing! I meant to alter this step and forgot to.

#. Call ``logInWithAnonymous()`` without any arguments.
#. Handle the ``result``.

.. include:: /examples/generated/react-native/v12/LoginWithAnonymous.snippet.login-anonymous.tsx.rst
Copy link
Collaborator

Choose a reason for hiding this comment

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

For these login examples, I think it may be worthwhile to wrap them in a functional component, just to make sure it's clear this is happening within a react component (should be obvious, but you never know).


.. include:: /examples/generated/react-native/v12/LoginWithEmail.snippet.email-password-login.tsx.rst

.. _react-native-api-key-login:
.. _react-native-login-api-key:
Copy link
Collaborator

Choose a reason for hiding this comment

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

There are hooks available for the following login strategies. Should we also show this?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I will! But this PR kept growing, so I wanted to break things up a bit. I created a small Jira epic to track the rest of the work: https://jira.mongodb.org/browse/DOCSP-27838

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Part of why this is so much work is that we're currently pulling examples from the Node.js test suite. And those examples are old and weren't well put together to begin with. So, gotta write new components.


.. _react-native-auth-hook:

useAuth Hook
Copy link
Collaborator

Choose a reason for hiding this comment

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

Really digging this table. Great reference

Copy link
Collaborator

Choose a reason for hiding this comment

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

I do, however, think we should have an explanation about the "operation enum". Since nothing is awaitable, it's necessary to use the operation to determine which auth method is currently being processed.
Also it may be good to show the possible string values for operation, just in case someone has the gall to use plain JS instead of TypeScript.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Great points about the enum. I'll update!

Copy link
Collaborator

Choose a reason for hiding this comment

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

+1 on the table!


.. tabs-realm-languages::
.. include:: /examples/generated/react-native/v12/RealmWrapper.snippet.configure-user-provider.tsx.rst
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same comment as above for useAuth. Perhaps if all the auth examples were wrapped in a Login component to show that this is what is being rendered in the fallback. We could simply say, the Login fallback examples are listed further down the document.

#. Pass the user's email and password to ``register()`` as an object.
#. Confirm the user's email address.

.. include:: /examples/generated/react-native/v12/LoginWithEmail.snippet.email-password-register.tsx.rst
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should we also get the result in this example?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Since register is not yet "awaitable", perhaps showing this flow would be helpful:
https://github.com/realm/realm-js/blob/main/examples/rn-todo-list/frontend/app/screens/LoginScreen.tsx#L50-L54

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yep. Good catch!

Calling the function in this example performs the entire password reset process.
.. _react-native-email-pass-hook:

useEmailPasswordAuth Hook
Copy link
Collaborator

Choose a reason for hiding this comment

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

result should also be added here.

.. step:: Create UI and Link Identities

The following example renders the user ID for each identity associated
with the app's current user. Initially, only the anonymou user
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
with the app's current user. Initially, only the anonymou user
with the app's current user. Initially, only the anonymous user

identity renders, but you can create an email/password identity and
link it to the anonymous user.

.. include:: /examples/generated/react-native/v12/LinkIdentities.snippet.user-identities.tsx.rst
Copy link
Collaborator

Choose a reason for hiding this comment

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

Upon reading this example, it occurred to me that we should probably have a linkCredentials method returned from useAuth.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

That would be helpful, for sure!

Copy link
Collaborator

@cbullinger cbullinger left a comment

Choose a reason for hiding this comment

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

overall LGTM -- a few typos and non-blocking suggestions


.. literalinclude:: /examples/generated/node/authenticate.snippet.email-password-login.js
:language: javascript
#. Destructure ``logIn`` and ``result`` from the ``useEmailPasswordAuth`` hook.
Copy link
Collaborator

Choose a reason for hiding this comment

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

I know you mention it at the top of the page, but do we want to link out to the useEmailPasswordAuth hook table here too?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I've been trying to avoid links in procedure steps. Probably makes sense to include a mention of useEmailPasswordAuth in the intro and link to it. What do you think?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yep, that makes sense! 👍


.. _react-native-auth-hook:

useAuth Hook
Copy link
Collaborator

Choose a reason for hiding this comment

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

+1 on the table!

#. Handle ``result``. If the authentication operation doesn't succeed, you can
write error handling based on ``result``.

.. include:: /examples/generated/react-native/v12/Login.snippet.user-provider-fallback.tsx.rst

User Sessions
Copy link
Collaborator

Choose a reason for hiding this comment

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

Since there are so many H2s on this page, would it help to nest all of the auth methods as H3s under an "Authenticate Users" (or something) parent heading? to better visually group them and separate from the other headings?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'll experiment! I was wondering the same thing at some point, but didn't get around to it.

application can call :js-sdk:`EmailPasswordAuth.resetPassword()
<Realm.Auth.EmailPasswordAuth.html#resetPassword>` to complete the password
reset flow.
application can use the :realm-react-sdk:`useEmailPasswordAuth hook
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this mostly applies to the Authenticate Users page, but it seemed like we weren't linking out to the API ref much/at all when referring to the useAuth and useUser hooks

krollins-mdb and others added 3 commits October 17, 2023 12:49
- Updated link identities sample
- Figuring out new babel bug
Copy link
Collaborator

@takameyer takameyer left a comment

Choose a reason for hiding this comment

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

LGTM :shipit:

Comment on lines +20 to +22
onPress={() => {
performAnonymousLogin();
}}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
onPress={() => {
performAnonymousLogin();
}}
onPress={logInWithAnonymous}

This could be simplified.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah, I've been omitting the UI bits of of components for the auth stuff. Bluehawk removes everything between :remove-start: and :remove-end: so that we get samples like what's on the page: https://preview-mongodbkrollinsmdb.gatsbyjs.io/realm/DOCSP-27838/sdk/react-native/manage-users/authenticate-users/#anonymous-user

In this case, we could probably include the UI, as there's not much. But in other cases, the UI can be a lot and isn't really what we're focusing on. I think I'll keep this one as-is so it's consistent with the other examples.

I'd be happy to hear your thoughts on the balance between concise examples and "complete" examples though!

Copy link
Collaborator

Choose a reason for hiding this comment

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

I guess this urge to comment spawned on this block:

  const performAnonymousLogin = () => {
    logInWithAnonymous();
  };

One could simply provide logInWithAnonymous without wrapping it in a function.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Oh. Yeah, that's a great point. ha! Thank you. I'll update other spots in the code that follow this pattern. Not sure why I got so wrapper happy.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think I initially misunderstood what you meant. Thank you for clarifying!

@krollins-mdb krollins-mdb merged commit 2e7a3ee into mongodb:master Oct 19, 2023
5 of 6 checks passed
@krollins-mdb krollins-mdb deleted the DOCSP-27838 branch October 19, 2023 20:42
@docs-builder-bot
Copy link

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.

4 participants