.env file help

default discord avatar
Kaj
4 months ago
16

Im working localy and have istaled Payload with next.js who I clone on github.


I have create .env -file as I should, but still I have to be loged in to get fetch from api to work?



My fetch is like this:


useEffect(() => {


setLoading(true);


fetch(

${process.env.NEXT_PUBLIC_SERVER_URL}/api/mat-menyer/

)


.then((res) => res.json())


.then((data) => {


setData(data);


setLoading(false);


console.log(data);


});


}, []);



Do I have to put my inlog to admin in the .env-file?



Thank you so much for this cms! 🙏



Best regards,


Kaj

  • default discord avatar
    thisisnotchris
    4 months ago

    @Kaj Hello!



    You may be having issues with fetch for a couple of reasons



    1.) Does the collection or global you're fetching have "read" access controls set?



    2.) If the content you're pulling requires authentication, are you passing "credentials: 'include'" in your fetch request?



    Happy to help troubleshoot

  • default discord avatar
    Kaj
    4 months ago

    Thanks for answare @thisisnotchris !



    How do I get "read" access to the control? Bit new to this 🙂

  • default discord avatar
    thisisnotchris
    4 months ago

    Of course!



    Check out access control on collections here:

    https://payloadcms.com/docs/access-control/collections


    each collection has their access operations listed



    const Posts: CollectionConfig = {
      slug: "posts",
      access: {
        create: ({ req: { user } }) => { ... },
        read: ({ req: { user } }) => { ... },
        update: ({ req: { user } }) => { ... },
        delete: ({ req: { user } }) => { ... },
        admin: ({ req: { user } }) => { ... },
      },
    };


    each one is a function that will return a boolean



    to enable public read of a collection, you can do



    const Posts: CollectionConfig = {
      slug: "posts",
      access: {
        read: ({ req: { user } }) => true,
      },
    };
  • default discord avatar
    Kaj
    4 months ago

    Thank you so much! Now its working without to have to be loged in 👏

  • default discord avatar
    thisisnotchris
    4 months ago

    Happy to help!

  • discord user avatar
    denolfe
    Payload Team
    4 months ago

    .env file help

Open the post
Continue the discussion in Discord
Like what we're doing?
Star us on GitHub!

Star

Connect with the Payload Community on Discord

Discord

online

Can't find what you're looking for?

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