System Information
- Strapi Version: 3.4.5
- Operating System: mac OS Big Sur 11.11
- Database: SQLite
- Node Version: 14.15.4
- NPM Version: 6.14.10
- Yarn Version: 1.22.10
Good afternoon everyone, first time poster here.
I have searched the forums for an answer to my question but was only able to find a partial one.
I am creating a registration page for new users in my React application. Aside from the default set of fields (username, email, password), I want to collect more information (gender, address, phone number, etc.) from the user.
I decided against adding new fields to the core User collection type and instead created a new collection type which I called āProfileā. Then I crated a unique 1:1 relation between the User and the Profile content type. Iām hoping that this will allow me to store additional information about users in their Profiles without polluting the core User object.
This works as expected and I am able to successfully associate Profiles with Users using the Strapi administration panel, however, I would like to catch all of this information when a user signs up via my React app and automatically create a corresponding Profile in addition to their User record.
After looking at the forums and the documentation, I think I need to use the Lifecycle hooks for the User.js model, either beforeCreate or afterCreate to achieve this and I managed to set up a basic hook (console.log(āHello worldā)) for the User model but now I have no idea how to progress with this further.
For now I have the following GraphQL mutation inside my SignUp.jsx React component:
const REGISTER_USER = gql`
mutation (
$name: String!
$email: String!
$password: String!
) {
register(input: {
username: $name,
email: $email,
password: $password
}) {
user {
username
email
}
}
}
`;
const [registerUser, { data }] = useMutation(REGISTER_USER);
registerUser({
variables: {
username,
email,
password
}
});
This works as expected and I think I can simply run another GraphQL mutation once the user has been created (createProfile) in my React application, but I would much prefer if the user Profile was created automatically in the backend for me when the first query runs (via the lifecycle hook).
I am a newcomer to Strapi and I know nothing about databases and have only started using GraphQL recently therefore would appreciate if someone could please provide me with a detailed answer on how to achieve this.
Thank you so much and wishing you a lovely day,
John