• Home
  • Categories
  • Recent
  • Popular
  • Pricing
  • Contact us
  • Docs
  • Login
FusionAuth
  • Home
  • Categories
  • Recent
  • Popular
  • Pricing
  • Contact us
  • Docs
  • Login

Why does import user with Registration fail?

Scheduled Pinned Locked Moved Solved
Q&A
import wordpress type ritza registration
2
4
2.3k
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F
    fusionauth.qhj5e
    last edited by 29 Feb 2024, 08:48

    I'm creating a Role for the default Application like so, and then trying to import a user:

    import {FusionAuthClient} from '@fusionauth/typescript-client';
    
    const applicationId = 'e9fdb985-9173-4e01-9d73-ac2d60d1dc8e';
    const subscriberRoleId = '635ef5c8-54c5-4605-ba0f-add6ad1578ce';
    const apiKey = '33052c8a-c283-4e96-9d2a-eb1215c69f8f-not-for-prod';
    const fusionauthUrl = 'http://localhost:9011';
    const fa = new FusionAuthClient(apiKey, fusionauthUrl);
    
    await fa.createApplicationRole(applicationId, subscriberRoleId, { role: {
        id: subscriberRoleId,
        isDefault: false,
        isSuperRole: false,
        name: 'Subscriber',
        description: 'Subscriber'
    } });
    
    const importRequest = { users: [user], validateDbConstraints: true };
    const result = await fa.importUsers(importRequest);
    

    Importing without user Registrations works fine, but as soon as I add a Registration, I get this error:

    fieldErrors: {
          'user.registrations.roles': [
            {
              code: '[invalid]user.registrations.roles',
              message: 'Invalid Application role(s) [635ef5c8-54c5-4605-ba0f-add6ad1578ce (app: ExampleNodeApp)].'
            }
          ]
        },
        generalErrors: []
    

    Here is the user I'm importing in JSON:

    {"email":"a@example.com",
    "encryptionScheme":"example-wordpress-phpass",
    "factor":8,"password":"JFAkQi9rQ3pUTURWN2NjQ2xhUlNoSlB6OHN1V1FkS2M1Lw==",
    "salt":"JFAkQi9rQ3pUTURWN2NjQ2xhUlNoSlB6OHN1V1FkS2M1Lw==",
    "uniqueUsername":"a",
    "username":"a",
    "verified":false,
    "active":true,
    "registrations":[{"applicationId":"e9fdb985-9173-4e01-9d73-ac2d60d1dc8e","roles":["635ef5c8-54c5-4605-ba0f-add6ad1578ce"]}],
    "data":{"WordPress_ID":2,"WordPress_user_nicename":"a","WordPress_user_registered":"2024-02-21 10:52:53","WordPress_display_name":"a","WordPress_nickname":"a","WordPress_wp_capabilities":{"subscriber":true},"WordPress_default_password_nag":"1"}}
    

    What must I change in my import user to get this to work please? The applicationId is correct and the roleId, I've checked in the web interface.

    A 1 Reply Last reply 1 Mar 2024, 16:34 Reply Quote 0
    • A
      Alex Patterson @fusionauth.qhj5e
      last edited by 1 Mar 2024, 16:34

      @fusionauth-qhj5e I am checking with the team if you indeed should be able to send in the Id for the role as you are in the example. For now you should be able to add Subscriber which is the name of your new role.

      Here is a full example.

      app.get('/registration', async (req, res, next) => {
      
        let returnResult;
        try {
          const applicationId = 'e9fdb985-9173-4e01-9d73-ac2d60d1dc8e';
          const subscriberRoleId = '635ef5c8-54c5-4605-ba0f-add6ad1578ce';
          const apiKey = '33052c8a-c283-4e96-9d2a-eb1215c69f8f-not-for-prod';
          const fusionauthUrl = 'http://localhost:9011';
          const fa = new FusionAuthClient(apiKey, fusionauthUrl);
      
          // make sure to remove role each time
          await fa.deleteApplicationRole(applicationId,subscriberRoleId);
      
          // add role
          await fa.createApplicationRole(applicationId, subscriberRoleId, { role: {
              id: subscriberRoleId,
              isDefault: false,
              isSuperRole: false,
              name: 'Subscriber',
              description: 'Subscriber'
          } });
      
          const user = {
            "email": "a@example.com",
            "encryptionScheme": "phpass-md5", // I updated this from wordpress
            "factor": 8, "password": "JFAkQi9rQ3pUTURWN2NjQ2xhUlNoSlB6OHN1V1FkS2M1Lw==",
            "salt": "JFAkQi9rQ3pUTURWN2NjQ2xhUlNoSlB6OHN1V1FkS2M1Lw==",
            "uniqueUsername": "a",
            "username": "a",
            "verified": false,
            "active": true,
            "registrations": [{ "applicationId": applicationId, "roles": ['Subscriber'] }],
            "data": { "WordPress_ID": 2, "WordPress_user_nicename": "a", "WordPress_user_registered": "2024-02-21 10:52:53", "WordPress_display_name": "a", "WordPress_nickname": "a", "WordPress_wp_capabilities": { "subscriber": true }, "WordPress_default_password_nag": "1" }
          }
      
          const importRequest = { users: [user], validateDbConstraints: true };
          const result = await fa.importUsers(importRequest);
          console.log("result", result)
          returnResult = result;
        } catch (e) {
          returnResult = JSON.stringify(e);
          console.error("error", returnResult);
        }
      
        res.json(JSON.stringify({
          returnResult
        }))
      });
      

      Please also take note you may need to update your encryptionScheme to phpass-md5 as I believe this is what will come from Wordpress.

      You can find more details https://fusionauth.io/docs/reference/password-hashes

      F 1 Reply Last reply 1 Mar 2024, 16:43 Reply Quote 0
      • F
        fusionauth.qhj5e @Alex Patterson
        last edited by 1 Mar 2024, 16:43

        @alex-patterson Thanks, this works. I thought the uuid would be better in case I ever change the role name though.

        A 1 Reply Last reply 1 Mar 2024, 17:03 Reply Quote 0
        • A Alex Patterson has marked this topic as solved on 1 Mar 2024, 17:02
        • A
          Alex Patterson @fusionauth.qhj5e
          last edited by 1 Mar 2024, 17:03

          @fusionauth-qhj5e I have brought this up internally, for now we are considering adding a PR to make it more clear for users.

          https://github.com/FusionAuth/fusionauth-site/pull/2918

          1 Reply Last reply Reply Quote 0
          1 out of 4
          • First post
            1/4
            Last post