Skip to content

Commit 2d7afbf

Browse files
authored
Merge pull request #264 from RobertSasak/master
Create hooks useDeleteUser and useSignOut
2 parents a4f5af8 + b494d5a commit 2d7afbf

File tree

4 files changed

+166
-0
lines changed

4 files changed

+166
-0
lines changed

auth/README.md

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ List of Auth hooks:
2525
- [useUpdateProfile](#useupdateprofile)
2626
- [useSendPasswordResetEmail](#usesendpasswordresetemail)
2727
- [useSendEmailVerification](#usesendemailverification)
28+
- [useSignOut](#usesignout)
29+
- [useDeleteUser](#usedeleteuser)
2830

2931
### useAuthState
3032

@@ -748,3 +750,105 @@ const SendEmailVerification = () => {
748750
);
749751
};
750752
```
753+
754+
### useSignOut
755+
756+
```js
757+
const [signOut, loading, error] = useSignOut(auth);
758+
```
759+
760+
Sign out current user. Wraps the underlying `auth.signOut` method and provides additional `loading` and `error` information.
761+
762+
The `useSignOut` hook takes the following parameters:
763+
764+
- `auth`: `Auth` instance for the app you would like to monitor
765+
766+
Returns:
767+
768+
- `signOut()`: A `() => Promise<void>` function you can call to sign out current user
769+
- `loading`: A `boolean` to indicate whether the user is being signed out
770+
- `error`: Any `Error` returned by Firebase when trying to sign out user, or `undefined` if there is no error
771+
772+
#### Full Example
773+
774+
```jsx
775+
import { useSignOut } from 'react-firebase-hooks/auth';
776+
777+
const SignOut = () => {
778+
const [signOut, loading, error] = useSignOut(auth);
779+
780+
if (error) {
781+
return (
782+
<div>
783+
<p>Error: {error.message}</p>
784+
</div>
785+
);
786+
}
787+
if (loading) {
788+
return <p>Loading...</p>;
789+
}
790+
return (
791+
<div className="App">
792+
<button
793+
onClick={async () => {
794+
await signOut();
795+
alert('You are sign out');
796+
}}
797+
>
798+
Sign out
799+
</button>
800+
</div>
801+
);
802+
};
803+
```
804+
805+
### useDeleteUser
806+
807+
```js
808+
const [deleteUser, loading, error] = useDeleteUser(auth);
809+
```
810+
811+
Delete current user. Wraps the underlying `auth.currrentUser.delete` method and provides additional `loading` and `error` information.
812+
813+
The `useDeleteUser` hook takes the following parameters:
814+
815+
- `auth`: `Auth` instance for the app you would like to monitor
816+
817+
Returns:
818+
819+
- `deleteUser()`: A `() => Promise<void>` function you can call to delete the current user
820+
- `loading`: A `boolean` to indicate whether the deletion is processing
821+
- `error`: Any `Error` returned by Firebase when trying to delete user, or `undefined` if there is no error
822+
823+
#### Full Example
824+
825+
```jsx
826+
import { useDeleteUser } from 'react-firebase-hooks/auth';
827+
828+
const DeleteUser = () => {
829+
const [deleteUser, loading, error] = useDeleteUser(auth);
830+
831+
if (error) {
832+
return (
833+
<div>
834+
<p>Error: {error.message}</p>
835+
</div>
836+
);
837+
}
838+
if (loading) {
839+
return <p>Loading...</p>;
840+
}
841+
return (
842+
<div className="App">
843+
<button
844+
onClick={async () => {
845+
await deleteUser();
846+
alert('You have been deleted');
847+
}}
848+
>
849+
Delete current user
850+
</button>
851+
</div>
852+
);
853+
};
854+
```

auth/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ export {
88
default as useSendPasswordResetEmail,
99
SendPasswordResetEmailHook,
1010
} from './useSendPasswordResetEmail';
11+
export { default as useSignOut, SignOutHook } from './useSignOut';
12+
export { default as useDeleteUser, DeleteUserHook } from './useDeleteUser';
1113
export { default as useSignInWithEmailAndPassword } from './useSignInWithEmailAndPassword';
1214
export {
1315
useSignInWithApple,

auth/useDeleteUser.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { Auth, AuthError } from 'firebase/auth';
2+
import { useMemo, useState } from 'react';
3+
4+
export type DeleteUserHook = [
5+
() => Promise<void>,
6+
boolean,
7+
AuthError | Error | undefined
8+
];
9+
10+
export default (auth: Auth): DeleteUserHook => {
11+
const [error, setError] = useState<AuthError>();
12+
const [loading, setLoading] = useState<boolean>(false);
13+
14+
const deleteUser = async () => {
15+
setLoading(true);
16+
setError(undefined);
17+
try {
18+
if (auth.currentUser) {
19+
await auth.currentUser.delete();
20+
} else {
21+
setError(new Error('No user is logged in') as AuthError);
22+
}
23+
} catch (err) {
24+
setError(err as AuthError);
25+
} finally {
26+
setLoading(false);
27+
}
28+
};
29+
30+
const resArray: DeleteUserHook = [deleteUser, loading, error];
31+
return useMemo<DeleteUserHook>(() => resArray, resArray);
32+
};

auth/useSignOut.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { Auth, AuthError } from 'firebase/auth';
2+
import { useMemo, useState } from 'react';
3+
4+
export type SignOutHook = [
5+
() => Promise<void>,
6+
boolean,
7+
AuthError | Error | undefined
8+
];
9+
10+
export default (auth: Auth): SignOutHook => {
11+
const [error, setError] = useState<AuthError>();
12+
const [loading, setLoading] = useState<boolean>(false);
13+
14+
const signOut = async () => {
15+
setLoading(true);
16+
setError(undefined);
17+
try {
18+
await auth.signOut();
19+
} catch (err) {
20+
setError(err as AuthError);
21+
} finally {
22+
setLoading(false);
23+
}
24+
};
25+
26+
const resArray: SignOutHook = [signOut, loading, error];
27+
return useMemo<SignOutHook>(() => resArray, resArray);
28+
};

0 commit comments

Comments
 (0)