User authentication

default discord avatar
Steplast year
12

I have my UserFrontend collection, which allows me to register and assign permissions to users, the question is how do I get their password and token because the API does not return this data? The authentication is handled with ReactJS from the Frontend.

  • default discord avatar
    Marťafiixeklast year

    Hi!


    Payload saves password as salt and hash to the database. All find operations are sanitized, and specifically hash and salt are removed from the document when you use any find operations. If you want to query tokens, you can create a custom endpoint and use


    const { docs } = await req.payload.find({
      collection: "users",
      pagination: false,
      showHiddenFields: true,
      queryHiddenFields: true,
    });


    And as Payload uses MongoDB, you can also query the database on your own



    For querying all users with all data you can use this snippet inside

    server.ts
    import { Collection, Db, MongoClient } from "mongodb"; // You need to install MongoDB as a dependency, but don't worry, as Payload already uses MongoDB already, you won't be adding much more in terms of bloat to your app
    
    app.get("/get-users", async (req, res) => {
      const connectionString = ""; // Paste connection string you use in Payload here just to test it, or use environment variable
      const client = new MongoClient(connectionString);
      let conn: any;
      try {
        conn = await client.connect();
      } catch (e) {
        console.error(e);
      }
      let db: Db = conn.db("prod"); // Set to database you use for your CMS
      const collection: Collection = db.collection("users");
      const users = await collection.find().toArray();
      res.json(users);
    });


    As you can see, I got all data



    mz4MTnwT55.png
  • default discord avatar
    Steplast year

    Where should I do the endpoint, next to my collection?

  • default discord avatar
    Steplast year

    I am doing this:


    endpoints: [{
            path: '/users',
            method: 'get',
            handler: async (req, res, next) => {
    
                const docs = await req.payload.find({
                    collection: 'users',
                    pagination: false,
                    showHiddenFields: true,
                    queryHiddenFields: true,
                });
            }
        }]

    But where does

    {docs}

    get unstructured from?

  • default discord avatar
    Marťafiixeklast year

    From the object

    find

    returns



    Also, you are not destructuring the docs right now



    And I would change the path to something different than users



    As right now the endpoint is available at

    api/users/users

    which doesn't make any sense

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.