How to create a new entry for a collection which has relation to User upon registration

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

2 Likes

Hi John,
Iā€™m new to this community and pretty new to Strapi as well. Iā€™m finding some gaps in my current setup and your method here may be an approach I will try. Iā€™ve extended the user model, but am finding some limitations with the way I can permit/restrict read access and am thinking a public profile with separate private user data approach may allow for greater controls.

Iā€™m interested to know if you have found a method to create these automatically with hooks, when a user is registering.

I unfortunately am not able to provide any suggestions on that part.

Have a great day!
Sheila

Instead of using register you can use createUser and it lets you add the inputs you added to the user model