Skip to content

Commit 19aba88

Browse files
committed
address auth review feedback
1 parent e13d9ff commit 19aba88

5 files changed

Lines changed: 25 additions & 8 deletions

File tree

src/components/ListingRead/ListingRead.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"use client";
2-
import { Fragment, useState, memo, useEffect, useMemo } from "react";
2+
import { Fragment, useState, memo, useEffect, useMemo, useRef } from "react";
33
import type { ReactNode } from "react";
44
import type { User } from "@supabase/supabase-js";
55

@@ -102,6 +102,8 @@ const ListingRead = memo(function Listing({
102102
: null,
103103
[rawRealListing, user]
104104
);
105+
const realListingRef = useRef(realListing);
106+
realListingRef.current = realListing;
105107
const listingForDisplay = demoListing ?? realListing;
106108

107109
// Load existing thread if any (only if not in demo mode). Depend on the
@@ -149,13 +151,13 @@ const ListingRead = memo(function Listing({
149151

150152
setExistingThread({
151153
...thread,
152-
listing: realListing,
154+
listing: realListingRef.current,
153155
messages: messages ?? [],
154156
});
155157
}
156158

157159
loadExistingThread();
158-
}, [listingId, listingOwnerId, userId, isDemo, supabase, realListing]);
160+
}, [listingId, listingOwnerId, userId, isDemo, supabase]);
159161

160162
const initialZoomLevel = 14;
161163
const listingDisplayNameCopy = useMemo(

supabase/functions/_shared/auth.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export function getBearerToken(
2+
authorizationHeader: string | null
3+
): string | null {
4+
if (!authorizationHeader) {
5+
return null;
6+
}
7+
8+
const match = authorizationHeader.match(/^\s*Bearer\s+(.+?)\s*$/i);
9+
return match?.[1] ?? null;
10+
}

supabase/functions/_shared/storage-utils.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import { SupabaseClient } from "https://esm.sh/@supabase/supabase-js@2";
22

3+
type ListingMedia = {
4+
avatar: string | null;
5+
photos: string[] | null;
6+
};
7+
38
export async function deleteStorageObject(
49
supabase: SupabaseClient,
510
bucket: string,
@@ -26,7 +31,7 @@ export async function deleteListingMedia(
2631
.from("listings")
2732
.select("avatar, photos")
2833
.eq("slug", slug)
29-
.maybeSingle();
34+
.maybeSingle<ListingMedia>();
3035

3136
if (fetchError) throw fetchError;
3237
if (!listing) return;

supabase/functions/delete-account/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { serve } from "https://deno.land/std@0.168.0/http/server.ts";
22
import { createClient } from "https://esm.sh/@supabase/supabase-js@2";
3+
import { getBearerToken } from "../_shared/auth.ts";
34
import { deleteListingMedia } from "../_shared/storage-utils.ts";
45

56
function jsonResponse(body: Record<string, unknown>, status: number) {
@@ -32,8 +33,7 @@ serve(async (req) => {
3233
return jsonResponse({ error: "Method not allowed" }, 405);
3334
}
3435

35-
const authHeader = req.headers.get("Authorization");
36-
const accessToken = authHeader?.replace("Bearer ", "");
36+
const accessToken = getBearerToken(req.headers.get("Authorization"));
3737

3838
if (!accessToken) {
3939
return jsonResponse({ error: "Missing access token" }, 401);

supabase/functions/delete-listing/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { serve } from "https://deno.land/std@0.168.0/http/server.ts";
22
import { createClient } from "https://esm.sh/@supabase/supabase-js@2";
3+
import { getBearerToken } from "../_shared/auth.ts";
34
import { deleteListingMedia } from "../_shared/storage-utils.ts";
45

56
function jsonResponse(body: Record<string, unknown>, status: number) {
@@ -36,8 +37,7 @@ serve(async (req) => {
3637
return jsonResponse({ error: "Missing listing slug" }, 400);
3738
}
3839

39-
const authHeader = req.headers.get("Authorization");
40-
const accessToken = authHeader?.replace("Bearer ", "");
40+
const accessToken = getBearerToken(req.headers.get("Authorization"));
4141

4242
if (!accessToken) {
4343
return jsonResponse({ error: "Missing access token" }, 401);

0 commit comments

Comments
 (0)