S
Supabase•2mo ago
Bhagi

Guest users not able to place an order from the website. Also, signed in user

1. I have check out as a guest option where any customer can place an order without sign in. I'm facing below error for the same.Supabase error, it would be greate if anyone helps as my system is currently in prod. Fetch error from https://wnocqkyhnsckgpyzmgsj.supabase.co/rest/v1/orders?columns=%22user_id%22%2C%22cart_items%22%2C%22subtotal%22%2C%22delivery_fee%22%2C%22hst%22%2C%22total%22%2C%22delivery_info%22%2C%22payment_info%22%2C%22status%22&select=*: { "code": "42501", "details": null, "hint": null, "message": "new row violates row-level security policy for table "orders"" }
No description
No description
39 Replies
garyaustin
garyaustin•2mo ago
Your RLS error is pretty clear. You don't meet RLS for, I assume an insert, on the table order. What is your insert policy (also your select policy) if you are using select on the insert? How are you managing users that are not logged in?
Bhagi
BhagiOP•2mo ago
I am new to supabase earlier using shopify store and I don't have mandatory login for the guest users but they need to enter phone number or email for delivering the food.
Bhagi
BhagiOP•2mo ago
No description
garyaustin
garyaustin•2mo ago
This does not seem very secure. Anyone could provide anyone's phone number even not using your app. But maybe you check later for a credit card so it is safe in the end... Your policy seems to be both email and phone are required if user_id is null, but you said or in your text above. If you used .insert().select() you also have to meet the select policy.
Bhagi
BhagiOP•2mo ago
so what should be the correct policy here?
garyaustin
garyaustin•2mo ago
Are you wanting email or phone not null? If so change your AND to an OR. Probably need to surround the two with parenthesis also. You also did not answer on select.
Bhagi
BhagiOP•2mo ago
phone number Can I create 2 policies one for authenticated users and one for guest users for inserting. Also, select and insert for authenticated
garyaustin
garyaustin•2mo ago
You can create separate policies they will be OR'd. You still have not answered on your select policy and if you are doing insert().select() in your code. If you are then you have to allow even non logged in users to access the table which will be a problem. You would likely need to remove the .select() part.
Bhagi
BhagiOP•2mo ago
yes I will remove it but what should be the policy query?
garyaustin
garyaustin•2mo ago
You had a policy that was close I think. You just needed to change AND to OR between email and phone. Then wrap in a parenthesis.
Bhagi
BhagiOP•2mo ago
(((user_id IS NOT NULL) AND (auth.uid() = user_id)) OR ((user_id IS NULL) AND (guest_email IS NOT NULL)) OR (guest_phone IS NOT NULL)) is it correct I'm still facing the error below Fetch error from https://wnocqkyhnsckgpyzmgsj.supabase.co/rest/v1/orders?columns=%22user_id%22%2C%22cart_items%22%2C%22subtotal%22%2C%22delivery_fee%22%2C%22hst%22%2C%22total%22%2C%22delivery_info%22%2C%22payment_info%22%2C%22status%22%2C%22guest_email%22%2C%22guest_phone%22&select=*: { "code": "42501", "details": null, "hint": null, "message": "new row violates row-level security policy for table "orders"" }
garyaustin
garyaustin•2mo ago
(((user_id IS NOT NULL) AND (auth.uid() = user_id)) OR ((user_id IS NULL) AND ((guest_email IS NOT NULL)) OR (guest_phone IS NOT NULL))) I would add a set of parathesis around the last pair of conditions in the OR. Do you do .select() on the insert()?
Bhagi
BhagiOP•2mo ago
whatever is good for food website I'm new to this policies
garyaustin
garyaustin•2mo ago
And I can only try and help you fix the a bug.... NOT tell you how to design your website security.
Bhagi
BhagiOP•2mo ago
sure 100%, I will add insert for now
garyaustin
garyaustin•2mo ago
The policy I showed (I THINK as I have no way to test) should allow an authenticated user to insert and if user_id is not provided allow access if email or phone is set.
Bhagi
BhagiOP•2mo ago
I'm still getting the same error with this policy
No description
garyaustin
garyaustin•2mo ago
Show your insert code. And you did not change the parenthesis like I showed.
Bhagi
BhagiOP•2mo ago
I updated the one you pinged (((user_id IS NOT NULL) AND (auth.uid() = user_id)) OR (((user_id IS NULL) AND (guest_email IS NOT NULL)) OR (guest_phone IS NOT NULL)))
garyaustin
garyaustin•2mo ago
This is what I showed: (((user_id IS NOT NULL) AND (auth.uid() = user_id)) OR ((user_id IS NULL) AND ((guest_email IS NOT NULL)) OR (guest_phone IS NOT NULL))) That is not what you are showing. Looks like what I provided still does not have the ()'s set right, sigh. (((user_id IS NOT NULL) AND (auth.uid() = user_id)) OR ((user_id IS NULL) AND ((guest_email IS NOT NULL) OR (guest_phone IS NOT NULL)))) And it could also be you still doing a .select() as you have not shown your call. Or it could be you are not passing in the correct insert with no user_id and a phone or email address.
Bhagi
BhagiOP•2mo ago
Can I use this and test (((user_id IS NOT NULL) AND (auth.uid() = user_id)) OR ((user_id IS NULL) AND ((guest_email IS NOT NULL) OR (guest_phone IS NOT NULL)))) still not working Supabase error Fetch error from https://wnocqkyhnsckgpyzmgsj.supabase.co/rest/v1/orders?columns=%22user_id%22%2C%22cart_items%22%2C%22subtotal%22%2C%22delivery_fee%22%2C%22hst%22%2C%22total%22%2C%22delivery_info%22%2C%22payment_info%22%2C%22status%22%2C%22guest_email%22%2C%22guest_phone%22&select=*: { "code": "42501", "details": null, "hint": null, "message": "new row violates row-level security policy for table "orders"" } Supabase error Order placement error: { "code": "42501", "details": null, "hint": null, "message": "new row violates row-level security policy for table "orders"" }
garyaustin
garyaustin•2mo ago
You have a select on the end still?
Bhagi
BhagiOP•2mo ago
this is my another select policy
No description
garyaustin
garyaustin•2mo ago
Show your insert call. That only allows authenticated users to read. The problem if you change it to allow all to read is then all entries in your table can be seen. So you need to remove the .select() from the end of the insert call you are making in your code.
Bhagi
BhagiOP•2mo ago
No description
garyaustin
garyaustin•2mo ago
If you have .insert().select() in your client code then both the insert and the select policy have to be met. You won't meet your select policy right now as it is for authenticated users. Show your insert call in your client code.
Bhagi
BhagiOP•2mo ago
garyaustin
garyaustin•2mo ago
I don't see an insert or any operation to the orders table in that code.
Bhagi
BhagiOP•2mo ago
I have this file only for authentication on the front end
garyaustin
garyaustin•2mo ago
Can you show me in there the orders table being used? I don't see any request to that. So some other piece of code is making the request.
Bhagi
BhagiOP•2mo ago
I see orders file here import React from 'react'; import { Helmet } from 'react-helmet'; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { useToast } from "@/components/ui/use-toast"; const AdminOrders = () => { const { toast } = useToast(); const comingSoonToast = () => { toast({ title: "🚧 Feature In Progress", description: "This page is currently under construction. Full functionality is coming soon!", }); };
comingSoonToast(); return ( <div> <Helmet> <title>Admin: Orders - BhagiBhavan</title> </Helmet> <h1 className="text-3xl font-bold text-gray-800 dark:text-white mb-6">Orders</h1> <Card> <CardHeader> <CardTitle>Order Management</CardTitle> </CardHeader> <CardContent> <p className='text-muted-foreground'>This section will display all customer orders, including paid, pending, and abandoned checkouts. You'll be able to filter and manage them from here. Coming soon!</p> </CardContent> </Card> </div> ); }; export default AdminOrders; // This is the line that tries to create the order in your database const { data: savedOrder, error } = await supabase .from('orders') .insert([orderData]) // <-- The error happens here .select() .single();
garyaustin
garyaustin•2mo ago
You have to remove that part. But then if the code after depends on some value being returned in savedOrder that will fail and you need to come up with a way to read your orders table to get the data (like maybe an order id the insert automatically creates?).
Bhagi
BhagiOP•2mo ago
so what should I remove? select? or insert?
garyaustin
garyaustin•2mo ago
You have to remove the select and single. BUT like I said your code after may need that value and then you and your AI will need to solve that with maybe an RPC call to get the value you need by email/phone. BUT you can't make your select policy work for non signed in users without letting users see all rows in your table which I assume would be very bad.
garyaustin
garyaustin•2mo ago
No idea. But like I said if you were expecting some info back from the insert then it will not be there now. If you called send-order-confirmation with something from savedOrder that would likely fail.
Bhagi
BhagiOP•2mo ago
ok thank you very much for your help so far, I really appreciate it
Bhagi
BhagiOP•2mo ago
No description
Bhagi
BhagiOP•2mo ago
this fixed the issue now don't see any error

Did you find this page helpful?