Why does import user with Registration fail?
-
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.
-
@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
tophpass-md5
as I believe this is what will come from Wordpress.You can find more details https://fusionauth.io/docs/reference/password-hashes
-
@alex-patterson Thanks, this works. I thought the uuid would be better in case I ever change the role name though.
-
-
@fusionauth-qhj5e I have brought this up internally, for now we are considering adding a PR to make it more clear for users.