Skip to content

Commit 4159933

Browse files
Onboarding getting marked in the backend
1 parent df50f9e commit 4159933

3 files changed

Lines changed: 90 additions & 0 deletions

File tree

supabase/config.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,3 +455,9 @@ enabled = true
455455
verify_jwt = false
456456
import_map = "./functions/create-stripe-portal/deno.json"
457457
entrypoint = "./functions/create-stripe-portal/index.ts"
458+
459+
[functions.get-onboarding-status]
460+
enabled = true
461+
verify_jwt = false
462+
import_map = "./functions/get-onboarding-status/deno.json"
463+
entrypoint = "./functions/get-onboarding-status/index.ts"
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"imports": {
3+
"@supabase/functions-js": "jsr:@supabase/functions-js@^2"
4+
}
5+
}
6+
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import "@supabase/functions-js/edge-runtime.d.ts";
2+
import { createClient } from "https://esm.sh/@supabase/supabase-js@2?dts";
3+
4+
const corsHeaders = {
5+
"Access-Control-Allow-Origin": "*",
6+
"Access-Control-Allow-Headers":
7+
"authorization, x-client-info, apikey, content-type",
8+
};
9+
10+
Deno.serve(async (req) => {
11+
if (req.method === "OPTIONS") {
12+
return new Response("ok", { headers: corsHeaders });
13+
}
14+
15+
const authHeader = req.headers.get("Authorization") ?? "";
16+
17+
const supabaseUserClient = createClient(
18+
Deno.env.get("SUPABASE_URL")!,
19+
Deno.env.get("SUPABASE_ANON_KEY")!,
20+
{
21+
global: {
22+
headers: { Authorization: authHeader },
23+
},
24+
}
25+
);
26+
27+
const {
28+
data: { user },
29+
error: userError,
30+
} = await supabaseUserClient.auth.getUser();
31+
32+
if (userError || !user) {
33+
return new Response(
34+
JSON.stringify({ error: "Not authenticated" }),
35+
{
36+
status: 401,
37+
headers: { "Content-Type": "application/json", ...corsHeaders },
38+
}
39+
);
40+
}
41+
42+
const supabaseAdmin = createClient(
43+
Deno.env.get("SUPABASE_URL")!,
44+
Deno.env.get("SUPABASE_SERVICE_ROLE_KEY")!
45+
);
46+
47+
const { data, error } = await supabaseAdmin
48+
.from("profiles")
49+
.select("is_onboarded")
50+
.eq("id", user.id)
51+
.single();
52+
53+
if (error) {
54+
console.error("get-onboarding-status error", error);
55+
return new Response(
56+
JSON.stringify({ error: "Failed to fetch onboarding status" }),
57+
{
58+
status: 500,
59+
headers: { "Content-Type": "application/json", ...corsHeaders },
60+
}
61+
);
62+
}
63+
64+
const isOnboarded = typeof data?.is_onboarded === "boolean" ? data.is_onboarded : false;
65+
66+
return new Response(
67+
JSON.stringify({
68+
ok: true,
69+
status: true,
70+
is_onboarded: isOnboarded,
71+
}),
72+
{
73+
status: 200,
74+
headers: { "Content-Type": "application/json", ...corsHeaders },
75+
}
76+
);
77+
});
78+

0 commit comments

Comments
 (0)