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

[auth] add useSignInWithCustomToken hook #304

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
25 changes: 25 additions & 0 deletions auth/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ List of Auth hooks:
- [useIdToken](#useidtoken)
- [useCreateUserWithEmailAndPassword](#usecreateuserwithemailandpassword)
- [useSignInWithEmailAndPassword](#usesigninwithemailandpassword)
- [useSignInWithCustomToken](#usesigninwithcustomtoken)
- [useSignInWithApple](#usesigninwithapple)
- [useSignInWithFacebook](#usesigninwithfacebook)
- [useSignInWithGithub](#usesigninwithgithub)
Expand Down Expand Up @@ -266,6 +267,30 @@ Returns:
- `loading`: A `boolean` to indicate whether the the user login is processing
- `error`: Any `Error` returned by Firebase when trying to login the user, or `undefined` if there is no error

### useSignInWithCustomToken

```js
const [
signInWithCustomToken,
user,
loading,
error,
] = useSignInWithCustomToken(auth);
```

Login a user with secure JSON Web Tokens (JWTs) generated by your servers. Wraps the underlying `auth.signInWithCustomToken` method and provides additional `loading` and `error` information.

The `useSignInWithCustomToken` hook takes the following parameters:

- `auth`: `Auth` instance for the app you would like to monitor

Returns:

- `signInWithCustomToken(token: string) => Promise<auth.UserCredential>`: A function you can call to start the login. Returns the `auth.UserCredential` if the user was signed in successfully, or `undefined` if there was an error.
- `user`: The `auth.UserCredential` if the user was logged in or `undefined` if not
- `loading`: A `boolean` to indicate whether the the user login is processing
- `error`: Any `Error` returned by Firebase when trying to login the user, or `undefined` if there is no error

#### Full Example

```jsx
Expand Down
4 changes: 4 additions & 0 deletions auth/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ export type SignInWithEmailLinkHook = AuthActionHook<
(email: string, emailLink?: string) => Promise<UserCredential | undefined>
>;

export type SignInWithCustomTokenHook = AuthActionHook<
(token: string) => Promise<UserCredential | undefined>
>;

export type SignInWithPopupHook = AuthActionHook<
(
scopes?: string[],
Expand Down
34 changes: 34 additions & 0 deletions auth/useSignInWithCustomToken.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import {
Auth,
AuthError,
signInWithCustomToken as firebaseSignInWithCustomToken,
UserCredential,
} from 'firebase/auth';
import { useCallback, useState } from 'react';
import { SignInWithCustomTokenHook } from './types';

export default (auth: Auth): SignInWithCustomTokenHook => {
const [error, setError] = useState<AuthError>();
const [loggedInUser, setLoggedInUser] = useState<UserCredential>();
const [loading, setLoading] = useState<boolean>(false);

const signInWithCustomToken = useCallback(
async (token: string) => {
setLoading(true);
setError(undefined);
try {
const user = await firebaseSignInWithCustomToken(auth, token);
setLoggedInUser(user);

return user;
} catch (err) {
setError(err as AuthError);
} finally {
setLoading(false);
}
},
[auth]
);

return [signInWithCustomToken, loggedInUser, loading, error];
};