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

    Why does import user with Registration fail?

    Scheduled Pinned Locked Moved Solved Q&A
    importwordpresstyperitzaregistration
    4 Posts 2 Posters 4.9k Views 1 Watching
    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

      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.

      ? 1 Reply Last reply
      Reply Quote 0
      • ?
        A Former User @fusionauth.qhj5e
        last edited by

        @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
        Reply Quote 0
        • F
          fusionauth.qhj5e @Guest
          last edited by

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

          ? 1 Reply Last reply
          Reply Quote 0
          • System has marked this topic as solved on
          • ?
            A Former User @fusionauth.qhj5e
            last edited by

            @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
            • danD dan moved this topic from Q&A on
            • danD dan moved this topic from Frequently Asked Questions (FAQ) on

            Hello! It looks like you're interested in this conversation, but you don't have an account yet.

            Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

            With your input, this post could be even better 💗

            Register Login
            • First post
              Last post