Cannot invoke an object which is possibly 'undefined'

Hi everyone, can anyone help me with this? I trying to integrate Access Control with Supabase and when I'm trying invoke authProvider.getPermissions it throws an error. here is my code for getPermissions getPermissions: async () => { const user = await supabaseClient.auth.getUser(); if (user) { return user.data.user?.role; } const { data } = await supabaseClient.rpc('get_my_claim', { claim: 'role', }); return Promise.resolve(data); },
No description
4 Replies
kapa.ai
kapa.ai2y ago
Hey @franzdev! I'm the kapa.ai support bot. I've searched through the docs to try to answer your question. :wave: ---------------------- It seems like you're trying to use getPermissions with Supabase and facing an issue. Based on the information extracts provided, I can see that there's a slight difference in the implementation of getPermissions in your code compared to the example provided in the refine-pixels-6 blog post (https://refine.dev/blog/refine-pixels-6/). Here's the adjusted getPermissions() method from the blog post:
getPermissions: async () => {
const user = supabaseClient.auth.user();

if (!user) {
return Promise.reject();
}

const { data } = await supabaseClient.rpc('get_my_claim', { claim: 'role' });
return Promise.resolve(data);
},
getPermissions: async () => {
const user = supabaseClient.auth.user();

if (!user) {
return Promise.reject();
}

const { data } = await supabaseClient.rpc('get_my_claim', { claim: 'role' });
return Promise.resolve(data);
},
In your code, you're using await supabaseClient.auth.getUser() instead of supabaseClient.auth.user(). Also, you're trying to access user.data.user?.role, which is not present in the example provided. Try updating your getPermissions method to match the example provided in the blog post and see if it resolves the issue. ----------------------
kapa.ai
kapa.ai2y ago
<@619660544319684609> kapa.ai is still learning and improving, please let us how it did by reacting below
fascinating-indigo
fascinating-indigo2y ago
Hey @franzdev, You can try like that:
...
const role = await authProvider.getPermissions?.()
...
...
const role = await authProvider.getPermissions?.()
...
Because getPermission is optional on authProvider type.
secure-lavender
secure-lavender2y ago
Hey @salihozdemir Thank you so much