https://supabase.com/ logo
#help
Title
# help
a

Anorok

03/11/2022, 8:11 PM
Hey everyone! Currently using Supabase in a Nextjs app. I'm trying to find some example code utilizing the service_key in order to bypass RLS (Building an admin panel to view/edit all profiles). I've found reference to it, and a warning to only use it serverside. I'm supanew. Does anyone have an example of using a service key to display all profiles?
n

Needle

03/11/2022, 8:11 PM
Hello @Anorok! This thread has been automatically created from your message in #843999948717555735 a ``few seconds ago``. Pinging @User so that they see this as well! Want to unsubscribe from this thread? Right-click the thread in Discord (or use the ... menu) and select Leave Thread to unsubscribe from future updates. Want to change the title? Use the
/title
command! We have solved your problem? Click the button below to archive it.
o

Olyno

03/11/2022, 8:17 PM
Hi Welcome to the community! So a service key and anon key are working together, you just have one which bypass all permissions, and shouldn't be public at all, and the other one which can be public, but it depends of RLS. The setup is the same for both, you can take the js example for that: https://supabase.com/docs/reference/javascript/initializing#createclient Just don't put in a public file if you want to use the service key :p @User
n

Needle

03/11/2022, 8:17 PM
Anorok (2022-03-11)
a

Anorok

03/11/2022, 8:22 PM
Hi! Thanks for the mega-fast response. So, I currently have a utils file where I currently call the public URL and anon key. Would I create a second constant, replace the anon key and call this instead? Sorry for being daft! Just got the swing of things, and this bypass has thrown me for a bit of a loop 😛
o

Olyno

03/11/2022, 8:27 PM
Yeah no problem! So yes you would need to use another file for that, and this other file should be only be used in the server side part, not be render
a

Anorok

03/11/2022, 8:28 PM
Right-o! I didn't realize it, but Next already has the support key in env variables
o

Olyno

03/11/2022, 8:30 PM
What a good news!
a

Anorok

03/11/2022, 8:31 PM
Thanks! Gonna give this a whirl! 😛
o

Olyno

03/11/2022, 8:35 PM
Don't hesitate to archive the thread if we solve your issue :p
a

Anorok

03/11/2022, 8:35 PM
Will do! Thanks again
o

Olyno

03/11/2022, 8:35 PM
No problem!
a

Anorok

03/11/2022, 8:46 PM
Me again! So, I've implemented a new util that is very similar to the public version. I've added the support key to my local env. However, I'm getting an error: "supabaseKey is required". Do I need to add an option that may not exist in the util created for public consumption?
here's what I have created:
Copy code
import { createClient } from '@supabase/supabase-js'

export const supabaseAdmin = createClient(
    process.env.NEXT_PUBLIC_SUPABASE_URL,
    process.env.SUPABASE_SERVICE_ROLE_KEY
)
o

Olyno

03/11/2022, 8:58 PM
it should work. Be sure ``process.env.SUPABASE_SERVICE_ROLE_KEY`` is defined correctly
I guess Nextjs didn't get it correctly
a

Anorok

03/11/2022, 9:03 PM
Welp, I guess I'm blind. Looked the same, but still copied and pasted. Magic happened! 😛
Thanks for your help Olyno! I'm in business!
n

Needle

03/11/2022, 9:04 PM
Thread was archived by @Anorok. Anyone can send a message to unarchive it.