Adding and Removing Login Methods
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";
export default function function MyPage(): JSX.ElementMyPage() {
const { anydata: const authMethods: anyauthMethods } = import useListAuthMethodsuseListAuthMethods();
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>;
}
return (
<React.JSX.IntrinsicElements.div: React.DetailedHTMLProps<React.HTMLAttributes<HTMLDivElement>, HTMLDivElement>div>
<React.JSX.IntrinsicElements.div: React.DetailedHTMLProps<React.HTMLAttributes<HTMLDivElement>, HTMLDivElement>div>Email: {const authMethods: anyauthMethods.anyemail}</React.JSX.IntrinsicElements.div: React.DetailedHTMLProps<React.HTMLAttributes<HTMLDivElement>, HTMLDivElement>div>
{const authMethods: anyauthMethods.anyoauthProviders.anymap((oauthProvider: anyoauthProvider) => (
<React.JSX.IntrinsicElements.div: React.DetailedHTMLProps<React.HTMLAttributes<HTMLDivElement>, HTMLDivElement>div React.Attributes.key?: React.Key | null | undefinedkey={oauthProvider: anyoauthProvider.anyproviderId}>
{oauthProvider: anyoauthProvider.anyproviderName}: {oauthProvider: anyoauthProvider.anyuserDisplayName}
</React.JSX.IntrinsicElements.div: React.DetailedHTMLProps<React.HTMLAttributes<HTMLDivElement>, HTMLDivElement>div>
))}
{const authMethods: anyauthMethods.anypasskeys.anymap((passkey: anypasskey) => (
<React.JSX.IntrinsicElements.div: React.DetailedHTMLProps<React.HTMLAttributes<HTMLDivElement>, HTMLDivElement>div 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()}
</React.JSX.IntrinsicElements.div: React.DetailedHTMLProps<React.HTMLAttributes<HTMLDivElement>, HTMLDivElement>div>
))}
</React.JSX.IntrinsicElements.div: React.DetailedHTMLProps<React.HTMLAttributes<HTMLDivElement>, HTMLDivElement>div>
);
}
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";
export default function function MyPage(): JSX.ElementMyPage() {
const { const setEmail: anysetEmail } = import useSetEmailuseSetEmail();
return (
<React.JSX.IntrinsicElements.button: React.DetailedHTMLProps<React.ButtonHTMLAttributes<HTMLButtonElement>, HTMLButtonElement>button React.DOMAttributes<HTMLButtonElement>.onClick?: React.MouseEventHandler<HTMLButtonElement> | undefinedonClick={() => const setEmail: anysetEmail("[email protected]")}>Change email</React.JSX.IntrinsicElements.button: React.DetailedHTMLProps<React.ButtonHTMLAttributes<HTMLButtonElement>, HTMLButtonElement>button>
);
}
Removing email auth
To remove an account’s email, use the useRemoveEmail
hook.
import { import useRemoveEmailuseRemoveEmail } from "@account-kit/react";
export default function function MyPage(): JSX.ElementMyPage() {
const { const removeEmail: anyremoveEmail } = import useRemoveEmailuseRemoveEmail();
return (
<React.JSX.IntrinsicElements.button: React.DetailedHTMLProps<React.ButtonHTMLAttributes<HTMLButtonElement>, HTMLButtonElement>button React.DOMAttributes<HTMLButtonElement>.onClick?: React.MouseEventHandler<HTMLButtonElement> | undefinedonClick={() => const removeEmail: anyremoveEmail())any}>anyRemove anyemail</anybutton>
);
}
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";
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
React.DOMAttributes<HTMLButtonElement>.onClick?: React.MouseEventHandler<HTMLButtonElement> | undefinedonClick={() =>
const addOauthProvider: anyaddOauthProvider({
authProviderId: stringauthProviderId: "google",
mode: stringmode: "popup",
})
}
>
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";
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 (
<React.JSX.IntrinsicElements.button: React.DetailedHTMLProps<React.ButtonHTMLAttributes<HTMLButtonElement>, HTMLButtonElement>button React.DOMAttributes<HTMLButtonElement>.onClick?: React.MouseEventHandler<HTMLButtonElement> | undefinedonClick={const removeFirstOauthProvider: () => voidremoveFirstOauthProvider}>Remove OAuth provider</React.JSX.IntrinsicElements.button: React.DetailedHTMLProps<React.ButtonHTMLAttributes<HTMLButtonElement>, HTMLButtonElement>button>
);
}
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";
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 (
<React.JSX.IntrinsicElements.button: React.DetailedHTMLProps<React.ButtonHTMLAttributes<HTMLButtonElement>, HTMLButtonElement>button React.DOMAttributes<HTMLButtonElement>.onClick?: React.MouseEventHandler<HTMLButtonElement> | undefinedonClick={() => const addPasskey: (variables: void | CredentialCreationOptions | undefined, options?: MutateOptions<string[], Error, void | CredentialCreationOptions | undefined, unknown> | undefined) => voidaddPasskey())any}>anyAdd anypasskey</anybutton>
);
}
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 useRemovePasskeyuseRemovePasskey } from "@account-kit/react";
export default function function MyPage(): JSX.ElementMyPage() {
const { anydata: const authMethods: anyauthMethods } = import useListAuthMethodsuseListAuthMethods();
const { const removePasskey: anyremovePasskey } = import useRemovePasskeyuseRemovePasskey();
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 <React.JSX.IntrinsicElements.button: React.DetailedHTMLProps<React.ButtonHTMLAttributes<HTMLButtonElement>, HTMLButtonElement>button React.DOMAttributes<HTMLButtonElement>.onClick?: React.MouseEventHandler<HTMLButtonElement> | undefinedonClick={const removeFirstPasskey: () => voidremoveFirstPasskey}>Remove passkey</React.JSX.IntrinsicElements.button: React.DetailedHTMLProps<React.ButtonHTMLAttributes<HTMLButtonElement>, HTMLButtonElement>button>;
}
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.