How can I make authenticated requests from a Next....
# help
j
How can I make authenticated requests from a Next.js API route? I have a write policy for auth.uid() = user_id and Client side works fine but not Server side
n
Hello @João Vitor! This thread has been automatically created from your message in #843999948717555735 a few seconds ago. We have already mentioned the @User so that they can see your message and help you as soon as possible! 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.
g
You have to get your user jwt to the api and set the clent on the server side to use it. One common function as part of that: https://supabase.com/docs/reference/javascript/auth-setauth
j
But I don't wanna set the Auth on every request
Is there a way that I can do this only once?
g
There should be no extra db calls there. setAuth just sets the jwt in the client object.
j
I mean for every API route
g
I don't use server side stuff, but somehow does the server side have to potentially deal with a different users on each call so must set the client jwt? Here is a library of stuff Supabase has come up with, but may be overkill for you as it also deals with serverside login. https://github.com/supabase-community/auth-helpers
j
Yeah, I didn't want to use auth-helpers cause I think it's over engineering and I would also like to learn how things work with supabase on the server side
And yes, I'm building a small e-commerce app
It's just to put it on my portfolio but it should have more than one user
g
You might want to re ask a new question specifically with what you are doing and ask if there is a better way with multiple routes. You could certainly have a function call to clean up code, but still would do the same stuff in each API route. There may also be something in next.js that understands sharing info among API routes, but I don't use it.
j
Okay, thank you Do you know if this was recorded ?https://twitter.com/supabase/status/1507024485592932358
g
I've not seen a general repository of videos, so all I could do is google to search.
j
I couldn't find it in the official youtube page, but it's fine
Thank you
@garyaustin Do you know database modelling ?
I'm wondering if the tables I created for this project are okay
g
within reason.
j
This is the schema I thought of
It doesn't have payments, address or any other information
Just users and products
And then we have the
cart_items
table and the
orders
table
g
The user and product are no brainers. Something does not seem right about Order and Cart.
j
Could the column be an array?
g
So you have an order and want more detail of all items in that order?
j
Yes
g
It might work if you have an incrementing id and then a non-unique cart id.
You will have several cart entries with same cart id in the cart table.
j
I would like to have a schema like this
Copy code
js
var user = { id, email, first_name, last_name }
var product = { id, name, description, price, stock }
var cart = { id, user_id, products: [], sub_total }
var order = { id, user_id, cart_id }
I think that makes more sense
But I don't know how to get an array of products on a db table
g
Yes, so if your cart table allows has a unique id column, just for index (or I guess maybe no primary key... not sure) then you can have multiple entries with the same cart_id so works like an array.
When you do a join with cart_id you would get all the products in that card.
j
got it, I could maybe make the
cart.id
a foreign key to
user.id
and a non unique key
If postgres allows it
g
Well it is just like a message table with messages pointing to a topic.
your topic is the order table actually. So I guess an order_id in cart with fk back to order id
j
I'll try this
actually
g
So not cart_id reference in order, the other way around
j
Do you know what this is?
I just marked it as true
g
You can have an array column, but you can't have array of fks.
j
Gotcha
g
You would have to do another select on the array values
j
A web dev trying to design a database, lol
n
Thread was archived by @João Vitor. Anyone can send a message to unarchive it.