Strapi.js: I have struggle with creating custom controller to user registration

System Information
  • Strapi Version:
  • Operating System:
  • Database:
  • Node Version:
  • NPM Version:
  • Yarn Version:

I’ m a beginner in Strapi. I want to post user with payments using one request. Payment table is joined by relationships one-to-one with user table. What I do wrong? What should I change?
My code:

extencions/users-permissions/controllers/Auth.js

const { sanitizeEntity } = require('strapi-utils/lib');
const { createCoreController } = require('@strapi/strapi').factories;


module.exports = createCoreController('plugin::users-permissions.user', ({ strapi }) =>  ({
  async register(ctx) {
    const { username, email, password, payment } = ctx.request.body;

    // Create the user with the provided username, email, and password
    const user = await strapi.plugins['users-permissions'].services.user.create({ username, email, password });

    // Create the payment with the provided payment details
    await strapi.service('api::payment.payment').create({
      data: {
        ...payment,
        user: user.id,
      },
      
    });

    // Add the payment entry to the user
    // await strapi.plugins['users-permissions'].services.user.update({ id: user.id }, { payment: paymentEntry.id});
    var result = sanitizeEntity(user, { model: strapi.getModel('plugin::users-permissions.user') })
    return result;
  },
}));

extencions/users-permissions/config/routes.js

module.exports = {
    routes: [
      {
        method: 'POST',
        path: '/auth/local/register',
        handler: 'Auth.register',
        config: {
          auth: false,
        },
      },
    ],
  };

If I trying to post:
{ "username": "example", "email": "example@domain.com", "password": "123456", "optInSubscription": false, "paymentsProcessing": "payPal", "paymentsOffer": 1, "payment": { "cardName": "name", "cardSname": "sname", "cardNumber": "1234123412341234", "securityCode": 123, "expiryDate": "12/24", "paymentsOffer": 1, "paymentsProcessing": "creditCard" } }

user with no relationship is created.
There is no error message I suppose Strapi don’t see my custom code.

1 Like

As I can understand for Strapi v4 you cant do that. Intead of you can try patch plugin instance directly

export default (plugin) => {


  plugin.controllers.user.register = async (ctx) => {
    const {
      username,
      email,
      password,
      payment
    } = ctx.request.body;

    // Create the user with the provided username, email, and password
    const user = await strapi.plugins['users-permissions'].services.user.create({
      username,
      email,
      password
    });

    // Create the payment with the provided payment details
    await strapi.service('api::payment.payment')
      .create({
        data: {
          ...payment,
          user: user.id,
        },

      });

    // Add the payment entry to the user
    // await strapi.plugins['users-permissions'].services.user.update({ id: user.id }, { payment: paymentEntry.id});
    var result = sanitizeEntity(user, {
      model: strapi.getModel('plugin::users-permissions.user')
    })
    return result;
  };

  plugin.routes["content-api"].routes.push({
    method: 'POST',
    path: '/auth/local/register',
    handler: 'auth.register',
    config: {
      auth: false,
    },
  });

  return plugin;
};

Thanks for the great comunity Strapi — русскоговорящее сообщество

Cheers!