kucherenko / strapi-plugin-passwordless

A plugin for Strapi Headless CMS that provides ability to sign-in/sign-up to an application by link had sent to email.
MIT License
77 stars 27 forks source link

Lost params on user creation using strapi-plugin-passwordless #9

Closed gundall closed 1 year ago

gundall commented 1 year ago

Hi! I noticed that if you use the plugin's /api/passwordless/send-link endpoint with non-existing user data, it will create a new user with that data. I'm taking advantage of this feature in my project's registration view, but I need to add an avatar as an additional parameter:

import axios from "axios";

// Stuff...

axios
    .post(`${BASE_URL}api/passwordless/send-link`, {
        ...{
        avatar: "1",
        email: "john@doe.com",
        username: "JohnnyDoe21"
        },
        ...{
            headers: {
                cors: "*"
            }
        }
    })

The problem is that when I send the data to the endpoint (avatar, username, and email), the "avatar" field is ignored by the plugin. If I check the users-permissions's beforeCreate life cycle method, the "params" field doesn't contain "avatar"; it is removed:

{
  action: 'beforeCreate',
  model: { ... },
    lifecycles: {},
    indexes: [ [Object], [Object] ],
    columnToAttribute: { ... }
  },
  state: {},
  params: {
    data: {
      email: 'john@doe.com',
      username: 'JohnnyDoe21',
      role: [Object],
      createdAt: 2022-09-07T10:59:29.049Z,
      updatedAt: 2022-09-07T10:59:29.049Z
    },
    populate: [ 'role' ]
  }
}

Is there a way to make the plugin to keep certain params if required?

Thank you in advance!

gundall commented 1 year ago

I have found a working solution for this, and commited it into my forked version of this repository. I'll create a pull request in order to let you decide if it is worthwhile, or not.

This solution works this way: if you need more params (e.g. "avatar"), there are two things to do:

  1. Add that param to the data object.
  2. Add an additional param called "keepParams". This param has to be a list (array) of the param names you want the plugin to keep in the user creation; in this case, it will be ["avatar"].

This is how it would look in the previous example:

import axios from "axios";

// Stuff...

axios
    .post(`${BASE_URL}api/passwordless/send-link`, {
        ...{
        avatar: "1",
        email: "john@doe.com",
        username: "JohnnyDoe21",
            keepParams: ["avatar"]
        },
        ...{
            headers: {
                cors: "*"
            }
        }
    })
tadinski commented 1 year ago

@gundall thanks for the solution, works well.