Skip to content

Commit 9bf5ff8

Browse files
committed
fix: read notes
further improved version of what I've been doing. This one is better than the previous because it's safer as there's no access_tokens and refresh_tokens flying around through the internets, just the user_id
1 parent c929226 commit 9bf5ff8

File tree

1 file changed

+18
-25
lines changed

1 file changed

+18
-25
lines changed

src/routes/auth/launcher/+server.ts

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,26 @@
11
import { error } from "@sveltejs/kit"
22
import { formatError } from "$lib/utils"
33
import { createStripeCustomer } from "$lib/server/stripe.server"
4+
import { supabaseAdmin } from "$lib/server/supabase.server"
45

5-
export const POST = async ({ request, locals: { supabaseServer } }) => {
6+
export const POST = async ({ request }) => {
67
console.log("💻 Launcher sign up")
78
const data = await request.json()
8-
const access_token = data?.access_token
9-
const refresh_token = data?.refresh_token
9+
const id = data?.user_id
1010

11-
if (!access_token) error(403, "No access_token received.")
12-
if (!refresh_token) error(403, "No refresh_token received.")
11+
if (!id) error(403, "No user_id received.")
1312

14-
const {
15-
data: { user },
16-
error: err
17-
} = await supabaseServer.auth.setSession({ access_token, refresh_token })
18-
if (err) error(401, formatError(err))
19-
if (!user) error(401, "Failed to get user.")
20-
21-
const {
22-
data: { session }
23-
} = await supabaseServer.auth.getSession()
24-
25-
const { count } = await supabaseServer
13+
const { count } = await supabaseAdmin
2614
.schema("profiles")
2715
.from("profiles")
2816
.select("*", { count: "exact", head: true })
29-
.eq("id", user.id)
17+
.eq("id", id)
3018
.single()
3119

3220
if (count) {
3321
return new Response(
3422
JSON.stringify({
35-
success: true,
36-
access_token: session?.access_token,
37-
refresh_token: session?.refresh_token
23+
success: true
3824
}),
3925
{
4026
status: 200,
@@ -46,6 +32,15 @@ export const POST = async ({ request, locals: { supabaseServer } }) => {
4632
)
4733
}
4834

35+
const {
36+
data: { user },
37+
error: getUserErr
38+
} = await supabaseAdmin.auth.admin.getUserById(id)
39+
if (!user) {
40+
console.error("No user with such ID: ", getUserErr)
41+
error(401, "No user with such ID.")
42+
}
43+
4944
if (user.email && user.app_metadata.provider == "discord") {
5045
const discord = user.user_metadata["provider_id"]
5146
const stripe = await createStripeCustomer(
@@ -56,7 +51,7 @@ export const POST = async ({ request, locals: { supabaseServer } }) => {
5651
)
5752
if (!stripe) error(403, "Failed to create stripe user for " + user.id)
5853

59-
const { error: err } = await supabaseServer.schema("profiles").from("profiles").insert({
54+
const { error: err } = await supabaseAdmin.schema("profiles").from("profiles").insert({
6055
id: user.id,
6156
stripe,
6257
discord,
@@ -70,9 +65,7 @@ export const POST = async ({ request, locals: { supabaseServer } }) => {
7065

7166
return new Response(
7267
JSON.stringify({
73-
success: true,
74-
access_token: session?.access_token,
75-
refresh_token: session?.refresh_token
68+
success: true
7669
}),
7770
{
7871
status: 200,

0 commit comments

Comments
 (0)