custom useLogin notification

how to custom useLogin notification
12 Replies
kapa.ai
kapa.ai7mo ago
Hi @Belvis. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- Refine provides a way to customize the error message displayed when the login method resolves the promise with success: false. You can resolve the promise with an error object that has name and message properties. Here's an example:
import { AuthBindings } from "@refinedev/core";

const authProvider: AuthBindings = {
login: async ({ email, password }) => {
return {
success: false,
error: {
name: "Login Failed!",
message: "The email or password that you've entered doesn't match any account.",
},
};
},
};
import { AuthBindings } from "@refinedev/core";

const authProvider: AuthBindings = {
login: async ({ email, password }) => {
return {
success: false,
error: {
name: "Login Failed!",
message: "The email or password that you've entered doesn't match any account.",
},
};
},
};
In this example, the name property will be the title of the notification, and the message property will be the subtitle. If you want to use a custom login page, you can use the useLogin hook. Here's an example:
import { useLogin } from "@refinedev/core";
import { Form } from "antd";

type LoginVariables = {
username: string;
password: string;
};

export const LoginPage = () => {
const { mutate: login } = useLogin<LoginVariables>();

const onSubmit = (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault();

login({
username: event.currentTarget.username.value,
password: event.currentTarget.password.value,
});
};

return (
<Form
onFinish={onSubmit}
>
{/* ... */}
</Form>
);
};
import { useLogin } from "@refinedev/core";
import { Form } from "antd";

type LoginVariables = {
username: string;
password: string;
};

export const LoginPage = () => {
const { mutate: login } = useLogin<LoginVariables>();

const onSubmit = (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault();

login({
username: event.currentTarget.username.value,
password: event.currentTarget.password.value,
});
};

return (
<Form
onFinish={onSubmit}
>
{/* ... */}
</Form>
);
};
In this example, mutate acquired from useLogin can accept any kind of object for values since login method from authProvider doesn't have a restriction on its parameters. For more information, refer to the useLogin documentation (https://refine.dev/docs/authentication/hooks/use-login/) and the AuthProvider documentation (https://refine.dev/docs/tutorial/understanding-authprovider/create-authprovider#login). Sources: - useLogin documentation (https://refine.dev/docs/authentication/hooks/use-login#usage) - AuthProvider documentation (https://refine.dev/docs/tutorial/understanding-authprovider/create-authprovider#login) ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai7mo ago
<@353701348463738885> kapa.ai is still learning and improving, please let us know how it did by reacting below
conscious-sapphire
conscious-sapphire7mo ago
i dont want to show notification, how to turn it off ? @kapa.ai
kapa.ai
kapa.ai7mo ago
Let me check the knowledge base for you. One moment! :books: ---------------------- In Refine, you can disable notifications by setting the successNotification and errorNotification props to false in the data hooks. This can be done for hooks like useUpdate and useForm. Here's an example:
import { useUpdate } from "@refinedev/core";

const { mutate } = useUpdate();

mutate({
// By setting it to `false`, you can disable the notification.
successNotification: false,
errorNotification: false,
});
import { useUpdate } from "@refinedev/core";

const { mutate } = useUpdate();

mutate({
// By setting it to `false`, you can disable the notification.
successNotification: false,
errorNotification: false,
});
Similarly, for useForm:
import { useForm } from "@refinedev/core";

useForm({
// By setting it to `false`, you can disable the notification.
successNotification: false,
errorNotification: false,
});
import { useForm } from "@refinedev/core";

useForm({
// By setting it to `false`, you can disable the notification.
successNotification: false,
errorNotification: false,
});
This will prevent the success and error notifications from being displayed when the hook is called. For more information, refer to the Notification Providers documentation (https://refine.dev/docs/guides-concepts/notifications#notification-providers). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai7mo ago
<@353701348463738885> kapa.ai is still learning and improving, please let us know how it did by reacting below
conscious-sapphire
conscious-sapphire7mo ago
i dont want to show notification for useLogin, how to turn it off ? @kapa.ai
kapa.ai
kapa.ai7mo ago
Let me check the knowledge base for you. One moment! :books: ---------------------- To disable notifications for the useLogin hook, you can customize the login method in your authProvider to not return an error object when the login is unsuccessful. Here's an example:
import { AuthBindings } from "@refinedev/core";

const authProvider: AuthBindings = {
login: async ({ email, password }) => {
// ---
return {
success: false,
// Do not return an error object
};
},
// ---
};
import { AuthBindings } from "@refinedev/core";

const authProvider: AuthBindings = {
login: async ({ email, password }) => {
// ---
return {
success: false,
// Do not return an error object
};
},
// ---
};
In this example, when the login is unsuccessful (success: false), no error object is returned, so no error notification will be displayed. For more information, refer to the AuthProvider documentation (https://refine.dev/docs/tutorial/understanding-authprovider/create-authprovider#login). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai7mo ago
<@353701348463738885> kapa.ai is still learning and improving, please let us know how it did by reacting below
conscious-sapphire
conscious-sapphire7mo ago
@kapa.ai it doesn't work, I edited the error object but the notification didn't change, something else is controlling it not the authProvider @@
kapa.ai
kapa.ai7mo ago
Let me check the knowledge base for you. One moment! :books: ---------------------- I apologize for the confusion. The useLogin hook in Refine automatically handles error notifications and it's not currently possible to disable these notifications directly from the hook or the authProvider. However, you can create a custom login page and handle the login process manually to avoid automatic notifications. Here's an example:
import { useLogin, useNotify } from "@pankod/refine";

const LoginPage = () => {
const { mutate: login } = useLogin();
const notify = useNotify();

const handleLogin = async (values) => {
const { error } = await login(values);

if (error) {
// Handle error manually
// notify.error("Custom error message");
}
};

return (
<form onSubmit={handleLogin}>
{/* Your form fields */}
</form>
);
};
import { useLogin, useNotify } from "@pankod/refine";

const LoginPage = () => {
const { mutate: login } = useLogin();
const notify = useNotify();

const handleLogin = async (values) => {
const { error } = await login(values);

if (error) {
// Handle error manually
// notify.error("Custom error message");
}
};

return (
<form onSubmit={handleLogin}>
{/* Your form fields */}
</form>
);
};
In this example, we're using the useLogin hook to manually handle the login process. If there's an error, we can handle it manually and decide whether to show a notification or not. Please note that this is a workaround and might not be the best practice. It's always recommended to handle errors and notify users about them to improve the user experience. For more information, refer to the useLogin documentation (https://refine.dev/docs/authentication/hooks/use-login) and the useNotify documentation (https://refine.dev/docs/api-reference/hooks/notification/useNotify). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai7mo ago
<@353701348463738885> kapa.ai is still learning and improving, please let us know how it did by reacting below
other-emerald
other-emerald7mo ago
Hello @Belvis, auth hooks will show notification if error happens and if you have notification provider. Currently, you can't disable that.