Skip to content

Commit bc88f7e

Browse files
committed
Modify backend queries & set user session after login to handle RLS
1 parent 6c36d1c commit bc88f7e

File tree

5 files changed

+21
-21
lines changed

5 files changed

+21
-21
lines changed

course-matrix/backend/src/controllers/coursesController.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Request, Response } from "express";
22
import asyncHandler from "../middleware/asyncHandler";
3-
import { supabaseCourseClient } from "../db/setupDb";
3+
import { supabase } from "../db/setupDb";
44

55
const DEFAULT_COURSE_LIMIT = 1000;
66

@@ -26,7 +26,8 @@ export default {
2626
} = req.query;
2727

2828
// Query the courses, offerings tables from the database
29-
let coursesQuery = supabaseCourseClient
29+
let coursesQuery = supabase
30+
.schema('course')
3031
.from("courses")
3132
.select()
3233
.limit(Number(limit || DEFAULT_COURSE_LIMIT));
@@ -36,12 +37,14 @@ export default {
3637
`code.ilike.%${search}%,name.ilike.%${search}%`,
3738
);
3839
}
39-
let offeringsQuery = supabaseCourseClient.from("offerings").select();
40+
let offeringsQuery = supabase
41+
.schema('course')
42+
.from("offerings")
43+
.select();
4044

4145
// Get the data and errors from the queries
4246
const { data: coursesData, error: coursesError } = await coursesQuery;
43-
const { data: offeringsData, error: offeringsError } =
44-
await offeringsQuery;
47+
const { data: offeringsData, error: offeringsError } = await offeringsQuery;
4548

4649
// Set the courses and offerings data
4750
const courses = coursesData || [];

course-matrix/backend/src/controllers/departmentsController.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Request, Response } from "express";
22

3-
import { supabaseCourseClient } from "../db/setupDb";
3+
import { supabase } from "../db/setupDb";
44
import asyncHandler from "../middleware/asyncHandler";
55

66
export default {
@@ -14,7 +14,10 @@ export default {
1414
getDepartments: asyncHandler(async (req: Request, res: Response) => {
1515
try {
1616
// Query the departments table from the database
17-
let departmentsQuery = supabaseCourseClient.from("departments").select();
17+
let departmentsQuery = supabase
18+
.schema('course')
19+
.from("departments")
20+
.select();
1821

1922
// Get the data and errors from the query
2023
const { data: departmentsData, error: departmentsError } =

course-matrix/backend/src/controllers/offeringsController.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Request, Response } from "express";
22
import asyncHandler from "../middleware/asyncHandler";
3-
import { supabaseCourseClient } from "../db/setupDb";
3+
import { supabase } from "../db/setupDb";
44

55
export default {
66
/**
@@ -14,7 +14,8 @@ export default {
1414
try {
1515
const { course_code, semester } = req.query;
1616

17-
let offeringsQuery = supabaseCourseClient
17+
let offeringsQuery = supabase
18+
.schema('course')
1819
.from("offerings")
1920
.select()
2021
.eq("code", course_code)

course-matrix/backend/src/controllers/userController.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ export const login = asyncHandler(async (req: Request, res: Response) => {
7878

7979
res.cookie("refresh_token", data.session?.refresh_token, COOKIE_OPTIONS);
8080

81+
await supabase.auth.setSession({
82+
access_token: data.session.access_token,
83+
refresh_token: data.session.refresh_token
84+
})
85+
8186
res.status(200).json({
8287
message: "Login Success!",
8388
access_token: data.session?.access_token,

course-matrix/backend/src/db/setupDb.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,4 @@ export const supabase = createClient(
1313
config.DATABASE_KEY!,
1414
);
1515

16-
/**
17-
* Initializes and exports the Supabase course client.
18-
*
19-
* This client is used to interact with the Supabase backend services for the course schema.
20-
* The client is configured using the Supabase URL and API key from the config file.
21-
*/
22-
export const supabaseCourseClient = createClient(
23-
config.DATABASE_URL!,
24-
config.DATABASE_KEY!,
25-
{ db: { schema: "course" } },
26-
);
27-
2816
console.log("Connected to Supabase Client!");

0 commit comments

Comments
 (0)