diff --git a/auth/README.md b/auth/README.md index 4a9b2f2..9f63f1c 100644 --- a/auth/README.md +++ b/auth/README.md @@ -14,6 +14,7 @@ List of Auth hooks: - [useIdToken](#useidtoken) - [useCreateUserWithEmailAndPassword](#usecreateuserwithemailandpassword) - [useSignInWithEmailAndPassword](#usesigninwithemailandpassword) +- [useSignInWithCustomToken](#usesigninwithcustomtoken) - [useSignInWithApple](#usesigninwithapple) - [useSignInWithFacebook](#usesigninwithfacebook) - [useSignInWithGithub](#usesigninwithgithub) @@ -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`: 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 diff --git a/auth/types.ts b/auth/types.ts index ac7c658..0f4dc32 100644 --- a/auth/types.ts +++ b/auth/types.ts @@ -23,6 +23,10 @@ export type SignInWithEmailLinkHook = AuthActionHook< (email: string, emailLink?: string) => Promise >; +export type SignInWithCustomTokenHook = AuthActionHook< + (token: string) => Promise +>; + export type SignInWithPopupHook = AuthActionHook< ( scopes?: string[], diff --git a/auth/useSignInWithCustomToken.ts b/auth/useSignInWithCustomToken.ts new file mode 100644 index 0000000..8fa6d5d --- /dev/null +++ b/auth/useSignInWithCustomToken.ts @@ -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(); + const [loggedInUser, setLoggedInUser] = useState(); + const [loading, setLoading] = useState(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]; +};