https://supabase.com/ logo
Join Discord
Powered by
# javascript
  • s

    stibbs

    07/29/2021, 5:23 AM
    req.headers.token
    does contain a valid token
  • u

    user

    07/29/2021, 5:25 AM
    I think
    getUserByCookie
    is the incorrect method then? Try the
    getUser
    method instead
  • s

    stibbs

    07/29/2021, 5:26 AM
    Yep this is what I get back with getUserByCookie with a valid JWT...
    {"user":null,"data":null,"error":{}}
  • s

    stibbs

    07/29/2021, 5:26 AM
    Does getUser work via API?
  • s

    stibbs

    07/29/2021, 5:27 AM
    OK getUser is getting me somewhere, although now typescript is having a fit
    Copy code
    Argument of type 'string | string[] | undefined' is not assignable to parameter of type 'string'.
      Type 'undefined' is not assignable to type 'string'.ts(2345)
  • u

    user

    07/29/2021, 5:30 AM
    Yep, in Next.js each header can be a string or an array of strings, so you'll have to narrow that type down to be just a string before you pass into
    getUser
    .
  • u

    user

    07/29/2021, 5:31 AM
    And also ensure that it isn't undefined
  • s

    stibbs

    07/29/2021, 5:38 AM
    Cheers
  • s

    stibbs

    07/29/2021, 5:39 AM
    Ok now the db call is returning nothing
  • s

    stibbs

    07/29/2021, 5:39 AM
    Copy code
    const { data, error } = await supabase
        .from('settings')
        .select('*')
        .eq('user_id', user.id);
  • s

    stibbs

    07/29/2021, 5:42 AM
    Wait how do I type the req? I know I can type the response but the req doesn't let me type it because NextApiRequest isn't generic
  • f

    frubalu

    07/29/2021, 5:52 AM
    I’m away from my computer at the moment so I’m just trying to remember this off the top of my head, but doesn’t ‘getUserByCookie’ just take in ‘req’ as an argument instead of ‘req.headers.token’?
  • s

    stibbs

    07/29/2021, 6:16 AM
    Do I need to do queries differently when using RLS? Or should above be working
  • s

    silentworks

    07/29/2021, 6:17 AM
    Depending on your permission, you might need to be logged in before you can run certain queries
  • s

    stibbs

    07/29/2021, 6:23 AM
    My user is logged in and a valid user id is being passed. My RLS is set up for Select, Update, Insert to require auth.uid = user_id
  • s

    stibbs

    07/29/2021, 6:24 AM
    Query is being made via a working supabase client (insert works...)
  • s

    silentworks

    07/29/2021, 6:30 AM
    Could it be that there are no records in the
    settings
    table for that user?
  • s

    stibbs

    07/29/2021, 7:08 AM
    There's exactly 1 row in the table and its for this user 😦
  • s

    stibbs

    07/29/2021, 7:10 AM
    user_id from console.log output
    2e97bd50-8f02-4e70-a91f-adf9d15fe53b
  • s

    silentworks

    07/29/2021, 7:14 AM
    Can you share your RLS for that table please?
  • s

    stibbs

    07/29/2021, 7:15 AM
    Copy code
    ALTER TABLE settings ENABLE ROW LEVEL SECURITY;
    
    CREATE POLICY "Individuals can create their own settings." ON public.settings FOR
    INSERT
      WITH CHECK ((auth.uid() = user_id));
    
    CREATE POLICY "Individuals can update their own settings." ON public.settings FOR
    UPDATE
      USING ((auth.uid() = user_id));
    
    CREATE POLICY "Individuals can read their own settings." ON public.settings FOR
    SELECT
      USING ((auth.uid() = user_id));
  • s

    silentworks

    07/29/2021, 7:17 AM
    Remove the
    .eq
    from your supabase js code and see if it makes a difference, because in your RLS it should only return the one belonging to that user anyway
  • s

    stibbs

    07/29/2021, 7:18 AM
    No dice 😦
  • s

    silentworks

    07/29/2021, 7:20 AM
    Can you check if any message comes back in the
    error
    variable
  • s

    stibbs

    07/29/2021, 7:22 AM
    The code I'm running
    Copy code
    const { user } = await supabase.auth.api.getUser(req.headers.token);
      console.log(`Settings API user: ${user?.id}`);
      const { settings, error } = await getSettings();
    
      console.log(`Settings API data: ${settings}`);
      console.log(`Settings API error: ${error}`);
    The logged output
    Copy code
    Settings API user: 2e97bd50-8f02-4e70-a91f-adf9d15fe53b
    Settings API data: 
    Settings API error: null
    getSettings()
    Copy code
    export const getSettings = async () => {
      const { data: settings, error } = await supabase.from('settings').select('*');
      return { settings, error };
    };
  • m

    Mark ES

    07/29/2021, 7:24 AM
    Can anyone recommend the best approach using PostgREST via supabase.js for date filtering similar to the BETWEEN clause in SQL? I need to select records where a date is within a date range. Thanks
  • h

    hieu

    07/29/2021, 7:33 AM
    const { data, error } = await supabase .from('songs') .select('name, released') .gte('released', '2001-01-01') .lte('released', '2000-01-30')
  • h

    hieu

    07/29/2021, 7:35 AM
    you can try to disable Select policy on settings table. Run the request again. To confirm it's a RLS issue. It's difficult to get it right at the start. But when you know it, you will love it 😆
  • m

    Mark ES

    07/29/2021, 7:35 AM
    Thanks @User , that was my first thought so will go with that. Maybe that's translated to a BETWEEN when executed anyway.
  • s

    stibbs

    07/29/2021, 7:37 AM
    Deleted the select RLS and its still not working, so not RLS lol
12345...81Latest