Skip to content

(DOCSP-27002): @realm/react-ify Create and Delete Users page #2567

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

Merged
merged 2 commits into from
Feb 9, 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// :snippet-start: delete-user
import React from 'react';
import {useApp, useUser} from '@realm/react';
// :remove-start:
import {UserProvider} from '@realm/react';
import {Credentials} from 'realm';
import {App} from 'realm';
import {AppProvider} from '@realm/react';
import {render, fireEvent, waitFor} from '@testing-library/react-native';
import {View, Button} from 'react-native';

const APP_ID = 'example-testers-kvjdy';

function AppWrapper() {
return (
<View>
<AppProvider id={APP_ID}>
<MyApp />
</AppProvider>
</View>
);
}

function MyApp() {
const [loggedIn, setLoggedIn] = React.useState(false);
const app = useApp();

React.useEffect(() => {
app.logIn(Credentials.anonymous()).then(user => user && setLoggedIn(true));
}, []);
// ...
return loggedIn ? (
<View>
<UserProvider>
<DeleteUser />
</UserProvider>
</View>
) : null;
}
// :remove-end:

function DeleteUser() {
const app = useApp();
const user = useUser();

async function deleteUser() {
// Delete the currently logged in user
await app.deleteUser(user);
expect(app.currentUser).toBeNull(); // :remove:
}
// ...
// :remove-start:
return <Button onPress={deleteUser} testID='test-delete-user' title='Test Me!' />;
// :remove-end:
}
// :snippet-end:

afterEach(async () => await App.getApp(APP_ID).currentUser?.logOut());

test('delete user', async () => {
const {getByTestId} = render(<AppWrapper />);
const button = await waitFor(() => getByTestId('test-delete-user'));
fireEvent.press(button);
await waitFor(() => expect(App.getApp(APP_ID).currentUser).toBeNull());
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import React from 'react';
import {useApp, useUser} from '@realm/react';

function DeleteUser() {
const app = useApp();
const user = useUser();

async function deleteUser() {
// Delete the currently logged in user
await app.deleteUser(user);
}
// ...
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,8 @@ Call the :js-sdk:`App.deleteUser() <Realm.App.html#deleteUser>` on a user object
the user's account from your Realm application. This deletes the account from
the server in addition to clearing local data.


.. literalinclude:: /examples/generated/node/authenticate.snippet.delete-user.js
:language: javascript
.. literalinclude:: /examples/generated/react-native/ts/delete-user.test.snippet.delete-user.tsx
:language: typescript

To use your app in the future, the user must sign up for a new account.
They can use the same credentials (depending on the authentication provider),
Expand Down