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
@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
Thanks for answare @thisisnotchris !
How do I get "read" access to the control? Bit new to this 🙂
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,
},
};
Thank you so much! Now its working without to have to be loged in 👏
Happy to help!
.env file help
Star
Discord
online
Get help straight from the Payload team with an Enterprise License.