Adding Users to Playfab Developer Account into a Title via playfab sdk using NodeJS server

44 views Asked by At

I am new to PlayFab and want to import my users from Auth0 to PlayFab by running my nodejs script which uses PlayFab Admin rights to add users' data like email, password, nickname, full name, and others.

I have already exported users from auth0 via import/export and now I have tried adding users by running the PlayFabClient RegisterPlayFabUser function but instead, I got an error that the player must be logged in and about that I am still confused.

This the query which I have tried

app.get("/register", (req, res) => {
  const resultData = {};
  for (let i = 0; i < users.length; i++) {
    const username = users[i].Email;
    const password = users[i].Nickname;
    console.log(username, " -> ", password);
    // Call the PlayFab RegisterPlayFabUser API

    if (PlayFab.PlayFabAdmin.GetPlayerProfile({ Username: username })) {
      console.log("User already exists");
      continue;
    }

    PlayFab.PlayFabClient.RegisterPlayFabUser(
      {
        TitleId: PlayFab.settings.titleId,
        Username: username,
        Password: password,
      },
      (error, result) => {
        if (error) {
          console.error("Error registering user:", error.errorMessage);
          console.log(i, " -> ", username, " -> ", password);
          return res.status(500).json({ error: "User registration failed" });
        }
        console.log("User registered successfully:", result.data);
        resultData[username] = result.data;
      }
    );

    return res
      .status(200)
      .json({ message: "Users registered successfully", data: resultData });
  }
});

Sample JSON Data is:

const users = [
  {
    Id: "auth0|00a51971-4148-40c2-b282-98779aea9987",
    "Given Name": "Siena",
    "Family Name": "Reijns",
    Nickname: "blueswan947",
    Name: "Siena Reijns",
    Email: "[email protected]",
    "Email Verified": true,
    Picture: "https://randomuser.me/api/portraits/med/women/27.jpg",
    Connection: "SQL",
    "Created At": "2023-05-22T21:49:39.704Z",
    "Updated At": "2023-05-22T21:49:39.704Z",
  },
  {
    Id: "auth0|00eea88f-39cb-4fdf-9c38-81810c3dcbe8",
    "Given Name": "Charline",
    "Family Name": "Marie",
    Nickname: "heavypeacock366",
    Name: "Charline Marie",
    Email: "[email protected]",
    "Email Verified": true,
    Picture: "https://randomuser.me/api/portraits/med/women/89.jpg",
    Connection: "Username-Password-Authentication",
    "Created At": "2023-05-22T21:54:44.994Z",
    "Updated At": "2023-05-22T21:54:44.994Z",
  }
]

I have tried other things also but cannot achieve my result.

Thanks for spending your time!

0

There are 0 answers