csdojo-defaang / defaang

A website that will curate recently-asked interview questions from FAANG+. Currently inactive. Check out: https://github.com/ykdojo/OpenStream
MIT License
509 stars 120 forks source link

[FEATURE] A question submission box (backend) #108

Closed ykdojo closed 2 years ago

ykdojo commented 2 years ago

Description

This is the backend portion of #59.

ykdojo commented 2 years ago

The first step for this task: design the database schema.

ykdojo commented 2 years ago

I'm going to start working on this one soon.

ykdojo commented 2 years ago

Users table:

Image

ykdojo commented 2 years ago

I'm going to un-assign myself since I'll be away - in case anyone else wants to take it. I'll assign myself when I start working on it again.

ykdojo commented 2 years ago

From my own comment on #59:

The required fields:

ykdojo commented 2 years ago

For the question table, this setup should be replicated:

image

created_by is a foreign key to the auth.users table.

image
ykdojo commented 2 years ago

A rough plan for this feature:

This whole process may seem a bit complicated, but I think this is the right way to do it to make it as secure as possible.

I've even thought about switching to Firebase because it's complicated, but the workflow with it would be essentially the same.

There may be other ways of going about it (e.g., using row level security / RLS), but the above approach should be the simplest one. I'll document everything as I go here.

ykdojo commented 2 years ago

Documentation references:

(Extra - thinking of sending a PR to update the Supabase doc here once I'm done with this whole process)

ykdojo commented 2 years ago

I did some more research on this.

ykdojo commented 2 years ago
ykdojo commented 2 years ago

update: verified that supabase.auth.session().access_token worked for getting the JWT.

next step: set up a Next.js API and send it there.

ykdojo commented 2 years ago

note: the current user can be retrieved with supabase.auth.user()

ykdojo commented 2 years ago

I'm going to unassign myself for now to work on https://github.com/ykdojo/defaang/issues/122 first.

iShibi commented 2 years ago

I can pick the work from here

ykdojo commented 2 years ago

@iShibi sure! That'd be great.

ykdojo commented 2 years ago

Note: we should add a position field to the table.

ykdojo commented 2 years ago

@iShibi any progress? If not, I was thinking of working on this one

iShibi commented 2 years ago

Didn't get time to start the work, you can take it @ykdojo

ykdojo commented 2 years ago

I'll let you know if/when I start working on it

ykdojo commented 2 years ago

updated schema:

Image

ykdojo commented 2 years ago

reference: supabase insert

ykdojo commented 2 years ago

note on supabase service role: https://github.com/supabase/supabase/discussions/1284

ykdojo commented 2 years ago

After some more digging, I found that Supabase has a Next.js auth component - I'll look into this one next: https://github.com/supabase/auth-helpers/blob/main/packages/nextjs/README.md

ykdojo commented 2 years ago

I thought this might work, but it doesn't quite work the way it's described in the doc.

ykdojo commented 2 years ago

This one worked!

ykdojo commented 2 years ago

Note: I'm currently checking this example to see how they solved it.

ykdojo commented 2 years ago

I've found this comment to be helpful, too.

subhoghoshX commented 2 years ago

I wrote an SQL query to generate the above table automatically if anyone here doesn't want to do it manually every time. Just go to the SQL Editor tab in supabase and run the query.

If you find any mistake in the query below, please let me know.

-- Create a table for Public Questions
create table questions (
  id bigint generated by default as identity primary key,
  created_at timestamp with time zone default timezone('utc'::text, now()) not null,
  created_by uuid references auth.users(id) not null,
  company text,
  location text,
  asked_date date,
  question text,
  question_details text,
  stay_anonymous boolean,
  is_approved boolean,
  position text
);
ykdojo commented 2 years ago

update: I'll be working off of this draft PR