other-emerald
other-emerald5mo ago

Example of register with Supabase and Ant Design's form component?

I need an example of how I could register users where they're able to add extra information that would be added to Supabases' metadata such as name, phone number, etc. In my authProvider.ts file, I have the following action:
register: async ({ email, password, phone, name }) => {
try {
const { data, error } = await adminSupabaseClient.auth.admin.createUser({
email,
email_confirm: true, // No need to verify user's email address.
password,
phone,
user_metadata: { name },
});

if (error) {
return {
success: false,
error,
};
}

if (data) {
return {
success: true,
redirectTo: '/',
};
}
} catch (error: any) {
return {
success: false,
error,
};
}
register: async ({ email, password, phone, name }) => {
try {
const { data, error } = await adminSupabaseClient.auth.admin.createUser({
email,
email_confirm: true, // No need to verify user's email address.
password,
phone,
user_metadata: { name },
});

if (error) {
return {
success: false,
error,
};
}

if (data) {
return {
success: true,
redirectTo: '/',
};
}
} catch (error: any) {
return {
success: false,
error,
};
}
I looked at using useForm, but I think it needs to be wrapped with Create, Edit, or Clone?
8 Replies
kapa.ai
kapa.ai5mo ago
Hi @James. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- Based on the information provided in the extracts, you can use the register method in your authProvider.ts file to register users with additional metadata such as name and phone number. Your current implementation seems to be on the right track. Regarding the use of useForm, it is indeed a hook provided by Refine that helps manage methods such as create, edit, and clone within a form. However, it's not explicitly mentioned in the extracts how to use it for user registration with additional metadata. Here's an example of how you might use useForm in a Create component:
import { Create, useForm, IResourceComponentsProps } from "@pankod/refine";

export const UserCreate: React.FC<IResourceComponentsProps> = () => {
const { formProps, saveButtonProps } = useForm();
return (
<Create saveButtonProps={saveButtonProps}>
<Form {...formProps}>
<Form.Item
name="email"
label="Email"
rules={[
{
required: true,
message: "Please input your email!",
},
]}
>
<Input />
</Form.Item>
<Form.Item
name="password"
label="Password"
rules={[
{
required: true,
message: "Please input your password!",
},
]}
>
<Input.Password />
</Form.Item>
<Form.Item
name="phone"
label="Phone"
rules={[
{
required: true,
message: "Please input your phone number!",
},
]}
>
<Input />
</Form.Item>
<Form.Item
name="name"
label="Name"
rules={[
{
required: true,
message: "Please input your name!",
},
]}
>
<Input />
</Form.Item>
</Form>
</Create>
);
};
import { Create, useForm, IResourceComponentsProps } from "@pankod/refine";

export const UserCreate: React.FC<IResourceComponentsProps> = () => {
const { formProps, saveButtonProps } = useForm();
return (
<Create saveButtonProps={saveButtonProps}>
<Form {...formProps}>
<Form.Item
name="email"
label="Email"
rules={[
{
required: true,
message: "Please input your email!",
},
]}
>
<Input />
</Form.Item>
<Form.Item
name="password"
label="Password"
rules={[
{
required: true,
message: "Please input your password!",
},
]}
>
<Input.Password />
</Form.Item>
<Form.Item
name="phone"
label="Phone"
rules={[
{
required: true,
message: "Please input your phone number!",
},
]}
>
<Input />
</Form.Item>
<Form.Item
name="name"
label="Name"
rules={[
{
required: true,
message: "Please input your name!",
},
]}
>
<Input />
</Form.Item>
</Form>
</Create>
);
};
In this example, useForm is used to generate formProps and saveButtonProps which are then passed to the Form and Create components respectively. The Form.Item components are used to create the individual form fields. However, please note that this is a general example and might need to be adjusted to fit your specific use case and to work with Supabase. For more information, you can refer to the following documentation: - Refine's useForm hook (https://refine.dev/docs/ui-integrations/ant-design/hooks/use-form/) - Refine's AuthProvider (https://refine.dev/docs/authentication/auth-provider/) - Supabase Auth API (https://supabase.com/docs/guides/auth) ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai5mo ago
<@204362818592440323> kapa.ai is still learning and improving, please let us know how it did by reacting below
other-emerald
other-emerald5mo ago
@kapa.ai Could you provide an example on how I could use it with useRegister()?
kapa.ai
kapa.ai5mo ago
Let me check the knowledge base for you. One moment! :books: ---------------------- Sure, you can use the useRegister hook in conjunction with Ant Design's form component to register a new user. Here's an example:
import { useRegister } from "@refinedev/core";
import { Form, Input, Button } from "antd";

type RegisterVariables = {
email: string;
password: string;
phone: string;
name: string;
};

export const RegisterPage = () => {
const { mutate: register } = useRegister<RegisterVariables>();

const onFinish = (values: RegisterVariables) => {
register(values);
};

return (
<Form onFinish={onFinish}>
<Form.Item
name="email"
rules={[{ required: true, message: "Please input your email!" }]}
>
<Input placeholder="Email" />
</Form.Item>
<Form.Item
name="password"
rules={[{ required: true, message: "Please input your password!" }]}
>
<Input.Password placeholder="Password" />
</Form.Item>
<Form.Item
name="phone"
rules={[{ required: true, message: "Please input your phone number!" }]}
>
<Input placeholder="Phone" />
</Form.Item>
<Form.Item
name="name"
rules={[{ required: true, message: "Please input your name!" }]}
>
<Input placeholder="Name" />
</Form.Item>
<Form.Item>
<Button type="primary" htmlType="submit">
Register
</Button>
</Form.Item>
</Form>
);
};
import { useRegister } from "@refinedev/core";
import { Form, Input, Button } from "antd";

type RegisterVariables = {
email: string;
password: string;
phone: string;
name: string;
};

export const RegisterPage = () => {
const { mutate: register } = useRegister<RegisterVariables>();

const onFinish = (values: RegisterVariables) => {
register(values);
};

return (
<Form onFinish={onFinish}>
<Form.Item
name="email"
rules={[{ required: true, message: "Please input your email!" }]}
>
<Input placeholder="Email" />
</Form.Item>
<Form.Item
name="password"
rules={[{ required: true, message: "Please input your password!" }]}
>
<Input.Password placeholder="Password" />
</Form.Item>
<Form.Item
name="phone"
rules={[{ required: true, message: "Please input your phone number!" }]}
>
<Input placeholder="Phone" />
</Form.Item>
<Form.Item
name="name"
rules={[{ required: true, message: "Please input your name!" }]}
>
<Input placeholder="Name" />
</Form.Item>
<Form.Item>
<Button type="primary" htmlType="submit">
Register
</Button>
</Form.Item>
</Form>
);
};
In this example, the useRegister hook is used to get the register function. This function is then called when the form is submitted with the form values as its argument. The form values are expected to be of the RegisterVariables type. Please note that this is a general example and might need to be adjusted to fit your specific use case and to work with Supabase. For more information, you can refer to the following documentation: - Refine's useRegister hook (https://refine.dev/docs/authentication/hooks/use-register) - Ant Design's Form component (https://ant.design/components/form/) - Supabase Auth API (https://supabase.com/docs/guides/auth) ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai5mo ago
<@204362818592440323> kapa.ai is still learning and improving, please let us know how it did by reacting below
other-emerald
other-emerald5mo ago
@kapa.ai That works, do you have an example of how I could modify my register action so that Supabase returns the new id of the user, and then redirects to /users/edit/:id?
kapa.ai
kapa.ai5mo ago
Let me check the knowledge base for you. One moment! :books: ---------------------- To modify your register action to return the new user's ID and redirect to the /users/edit/:id route, you can adjust your register method in your authProvider.ts file to return the user's ID in the data field of the response object. Then, you can use this ID to redirect to the edit page of the user. Here's how you can modify your register method:
register: async ({ email, password, phone, name }) => {
try {
const { data, error } = await adminSupabaseClient.auth.admin.createUser({
email,
email_confirm: true, // No need to verify user's email address.
password,
phone,
user_metadata: { name },
});

if (error) {
return {
success: false,
error,
};
}

if (data) {
return {
success: true,
data: data.id, // Return the user's ID in the data field
};
}
} catch (error: any) {
return {
success: false,
error,
};
}
}
register: async ({ email, password, phone, name }) => {
try {
const { data, error } = await adminSupabaseClient.auth.admin.createUser({
email,
email_confirm: true, // No need to verify user's email address.
password,
phone,
user_metadata: { name },
});

if (error) {
return {
success: false,
error,
};
}

if (data) {
return {
success: true,
data: data.id, // Return the user's ID in the data field
};
}
} catch (error: any) {
return {
success: false,
error,
};
}
}
Then, in your registration form, you can use the useRegister hook's onSuccess option to redirect to the edit page of the user when the registration is successful:
import { useRegister } from "@refinedev/core";
import { Form, Input, Button } from "antd";
import { useHistory } from "react-router-dom";

type RegisterVariables = {
email: string;
password: string;
phone: string;
name: string;
};

export const RegisterPage = () => {
const history = useHistory();
const { mutate: register } = useRegister<RegisterVariables>({
onSuccess: (data) => {
// Redirect to the edit page of the user when the registration is successful
history.push(`/users/edit/${data}`);
},
});
const onFinish = (values: RegisterVariables) => {
register(values);
};

// ... rest of the form
};
import { useRegister } from "@refinedev/core";
import { Form, Input, Button } from "antd";
import { useHistory } from "react-router-dom";

type RegisterVariables = {
email: string;
password: string;
phone: string;
name: string;
};

export const RegisterPage = () => {
const history = useHistory();
const { mutate: register } = useRegister<RegisterVariables>({
onSuccess: (data) => {
// Redirect to the edit page of the user when the registration is successful
history.push(`/users/edit/${data}`);
},
});
const onFinish = (values: RegisterVariables) => {
register(values);
};

// ... rest of the form
};
In this example, the useHistory hook from react-router-dom is used to get the history object, which is then used to redirect to the edit page of the user when the registration is successful. Please note that this is a general example and might need to be adjusted to fit your specific use case and to work with Supabase. For more information, you can refer to the following documentation: - Refine's useRegister hook (https://refine.dev/docs/authentication/hooks/use-register) - React Router's useHistory hook (https://reactrouter.com/web/api/Hooks/usehistory) - Supabase Auth API (https://supabase.com/docs/guides/auth) ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai5mo ago
<@204362818592440323> kapa.ai is still learning and improving, please let us know how it did by reacting below