Adding and Removing Auth Methods
This guide assumes you have already followed the Setup Guide and have set up the Alchemy Account Provider using this guide. Please refer to the guides above for more information on how to properly setup your project.
For a complete example of how we can setup a project and use the various available authentication methods, please refer to our quickstart example.
If your user has already authenticated with email, social auth, or a passkey, you can add additional login methods to their account or remove currently enabled methods from their account. This is useful in the case that you want to give your users the ability to customize their login methods after their account is created.
Viewing the currently enabled auth methods
To view the enabled auth methods for the currently logged in user, use the useListAuthMethods
hook:
import { import useListAuthMethodsuseListAuthMethods } from "@account-kit/react";
import { class TextText, class ViewView } from "react-native";
export default function function MyPage(): JSX.ElementMyPage() {
const { anydata: const authMethods: anyauthMethods } = import useListAuthMethodsuseListAuthMethods();
if (!const authMethods: anyauthMethods) {
return <class ViewView>Loading…</class ViewView>;
}
return (
<class ViewView>
<class TextText>Email: {const authMethods: anyauthMethods.anyemail}</class TextText>
{const authMethods: anyauthMethods.anyoauthProviders.anymap((oauthProvider: anyoauthProvider) => (
<class TextText React.Attributes.key?: React.Key | null | undefinedkey={oauthProvider: anyoauthProvider.anyproviderId}>
{oauthProvider: anyoauthProvider.anyproviderName}: {oauthProvider: anyoauthProvider.anyuserDisplayName}
</class TextText>
))}
{const authMethods: anyauthMethods.anypasskeys.anymap((passkey: anypasskey) => (
<class TextText React.Attributes.key?: React.Key | null | undefinedkey={passkey: anypasskey.anyauthenticatorId}>
Passkey created at: {new var Date: DateConstructor
new (value: number | string | Date) => Date (+3 overloads)Date(passkey: anypasskey.anycreatedAt).Date.toLocaleString(locales?: Intl.LocalesArgument, options?: Intl.DateTimeFormatOptions): string (+2 overloads)Converts a date and time to a string by using the current or specified locale.
toLocaleString()}
</class TextText>
))}
</class ViewView>
);
}
Setting email auth
To set an account’s email, use the useSetEmail
hook.
If you set a user’s email, it is critical that you verify that they own the email first, to prevent malicious users from squatting on emails that have not yet been added to accounts.
Because of the security implications, the ability to set emails on existing accounts is disabled by default. If you would like it enabled for your team, please reach out.
An account may have at most one email address associated with it. If an account already has an email and you call this function, then the existing email will be removed.
import { import useSetEmailuseSetEmail } from "@account-kit/react";
import { class ButtonButton } from "react-native";
export default function function MyPage(): JSX.ElementMyPage() {
const { const setEmail: anysetEmail } = import useSetEmailuseSetEmail();
return (
<class ButtonButton onPress?: ((event: GestureResponderEvent) => void) | undefinedCalled when the touch is released, but not if cancelled (e.g. by a scroll that steals the responder lock).
onPress={() => const setEmail: anysetEmail("[email protected]")}>Change email</class ButtonButton>
);
}
Removing email auth
To remove an account’s email, use the useRemoveEmail
hook.
import { import useRemoveEmailuseRemoveEmail } from "@account-kit/react";
import { class ButtonButton } from "react-native";
export default function function MyPage(): JSX.ElementMyPage() {
const { const removeEmail: anyremoveEmail } = import useRemoveEmailuseRemoveEmail();
return (
<class ButtonButton onPress?: ((event: GestureResponderEvent) => void) | undefinedCalled when the touch is released, but not if cancelled (e.g. by a scroll that steals the responder lock).
onPress={() => const removeEmail: anyremoveEmail())any}>anyRemove anyemail</class ButtonButton>
);
}
Note that you cannot remove the last auth method from an account. If removing email login would leave no auth methods, then this call will fail.
Adding OAuth providers
To add an OAuth provider, use the useAddOauthProvider
hook.
import { import useAddOauthProvideruseAddOauthProvider } from "@account-kit/react";
import { class ButtonButton } from "react-native";
export default function function MyPage(): JSX.ElementMyPage() {
const { const addOauthProvider: anyaddOauthProvider } = import useAddOauthProvideruseAddOauthProvider();
return (
<React.JSX.IntrinsicElements.button: React.DetailedHTMLProps<React.ButtonHTMLAttributes<HTMLButtonElement>, HTMLButtonElement>button
onPress: () => anyonPress={() =>
const addOauthProvider: anyaddOauthProvider({
redirectUrl: stringredirectUrl: "<your-app-scheme>://<your-auth-callback-route>",
mode: stringmode: "redirect",
authProviderId: stringauthProviderId: "google",
})
}
>
Add Google
</React.JSX.IntrinsicElements.button: React.DetailedHTMLProps<React.ButtonHTMLAttributes<HTMLButtonElement>, HTMLButtonElement>button>
);
}
When this button is clicked, the user will be prompted to log in to a social account. Once they do, that account will be added as an auth method to the current user.
The options that can be passed to addOauthProvider
match those which can be passed to authenticate
when logging in with an OAuth provider. For information on these options and the required setup for enabling OAuth providers, see the Social Login documentation.
Removing OAuth providers
To remove an OAuth provider, use the removeOauthProvider()
hook, then pass the OAuth provider’s provider id to removeOauth
. To find the provider id, you can examine the value returned from useListAuthMethods()
.
import { import useListAuthMethodsuseListAuthMethods, import useRemoveOauthProvideruseRemoveOauthProvider } from "@account-kit/react";
import { class ButtonButton } from "react-native";
export default function function MyPage(): JSX.ElementMyPage() {
const { anydata: const authMethods: anyauthMethods } = import useListAuthMethodsuseListAuthMethods();
const { const removeOauthProvider: anyremoveOauthProvider } = import useRemoveOauthProvideruseRemoveOauthProvider();
if (!const authMethods: anyauthMethods) {
return <React.JSX.IntrinsicElements.div: React.DetailedHTMLProps<React.HTMLAttributes<HTMLDivElement>, HTMLDivElement>div>Loading…</React.JSX.IntrinsicElements.div: React.DetailedHTMLProps<React.HTMLAttributes<HTMLDivElement>, HTMLDivElement>div>;
}
const const removeFirstOauthProvider: () => voidremoveFirstOauthProvider = () => {
const removeOauthProvider: anyremoveOauthProvider(const authMethods: anyauthMethods.anyoauthProviders[0].anyproviderId);
};
return (
<class ButtonButton onPress?: ((event: GestureResponderEvent) => void) | undefinedCalled when the touch is released, but not if cancelled (e.g. by a scroll that steals the responder lock).
onPress={const removeFirstOauthProvider: () => voidremoveFirstOauthProvider}>Remove OAuth provider</class ButtonButton>
);
}
Note that you cannot remove the last auth method from an account. If removing a social login would leave no auth methods, then this call will fail.
Adding passkeys
To add a passkey, use the useAddPasskey
hook.
import { function useAddPasskey(mutationArgs?: UseAddPasskeyMutationArgs): UseAddPasskeyResultA custom hook to handle the addition of a passkey to an already authenticated account, which includes executing a mutation with optional parameters.
useAddPasskey } from "@account-kit/react";
import { class ButtonButton } from "react-native";
export default function function MyPage(): JSX.ElementMyPage() {
const { const addPasskey: UseMutateFunction<string[], Error, void | CredentialCreationOptions | undefined, unknown>addPasskey } = function useAddPasskey(mutationArgs?: UseAddPasskeyMutationArgs): UseAddPasskeyResultA custom hook to handle the addition of a passkey to an already authenticated account, which includes executing a mutation with optional parameters.
useAddPasskey();
return (
<class ButtonButton onPress?: ((event: GestureResponderEvent) => void) | undefinedCalled when the touch is released, but not if cancelled (e.g. by a scroll that steals the responder lock).
onPress={() => const addPasskey: (variables: void | CredentialCreationOptions | undefined, options?: MutateOptions<string[], Error, void | CredentialCreationOptions | undefined, unknown> | undefined) => voidaddPasskey())any}>anyAdd anypasskey</class ButtonButton>
);
}
This will prompt the user to create a passkey which will then be added as a login method to the account.
Removing passkeys
To remove a passkey, use the useRemovePasskey
hook and pass the passkey’s authenticator id to the removePasskey
function. To find the authenticator id, you can examine the value returned from useListAuthMethods()
.
import { import useListAuthMethodsuseListAuthMethods, import useRemoveOauthProvideruseRemoveOauthProvider } from "@account-kit/react";
import { class ButtonButton } from "react-native";
export default function function MyPage(): JSX.ElementMyPage() {
const { anydata: const authMethods: anyauthMethods } = import useListAuthMethodsuseListAuthMethods();
const { const removePasskey: anyremovePasskey } = anyuseRemovePasskey();
if (!const authMethods: anyauthMethods) {
return <React.JSX.IntrinsicElements.div: React.DetailedHTMLProps<React.HTMLAttributes<HTMLDivElement>, HTMLDivElement>div>Loading…</React.JSX.IntrinsicElements.div: React.DetailedHTMLProps<React.HTMLAttributes<HTMLDivElement>, HTMLDivElement>div>;
}
const const removeFirstPasskey: () => voidremoveFirstPasskey = () => {
const removePasskey: anyremovePasskey(const authMethods: anyauthMethods.anypasskeys[0].anyauthenticatorId);
};
return <class ButtonButton onClick: () => voidonClick={const removeFirstPasskey: () => voidremoveFirstPasskey}>Remove passkey</class ButtonButton>;
}
Note that you cannot remove the last auth method from an account. If removing a passkey would leave no auth methods, then this call will fail.