-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added something to the new API routes
- Loading branch information
1 parent
e9345ce
commit 99fa938
Showing
1 changed file
with
70 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import * as validation from "$lib/helper/formValidation"; | ||
import { error, type RequestHandler } from "@sveltejs/kit"; | ||
|
||
export const GET: RequestHandler = async ({ url, locals }) => { | ||
const supabase = locals.supabase; | ||
|
||
const id = url.searchParams.get("id"); | ||
|
||
if (!id) { | ||
error(400, 'URL parameter "id" is missing.'); | ||
} | ||
|
||
const { data, error: resError } = await supabase | ||
.from("users") | ||
.select() | ||
.eq("id", id) | ||
.single(); | ||
|
||
if (resError) { | ||
error(500, resError.message); | ||
} | ||
|
||
return new Response(data); | ||
}; | ||
|
||
export const POST: RequestHandler = async ({ locals, request }) => { | ||
const supabase = locals.supabase; | ||
const { user: authUser } = await locals.safeGetSession(); | ||
|
||
if (!authUser) { | ||
error(400, "You have to be signed in"); | ||
} | ||
|
||
const { data: currentUserDbData } = await supabase | ||
.from("users") | ||
.select() | ||
.eq("id", authUser.id); | ||
|
||
if (currentUserDbData && currentUserDbData.length > 0) { | ||
error(400, "A user with this auth id already exists"); | ||
} | ||
|
||
const { urlUsername, displayedUsername, bio } = await request.json(); | ||
|
||
const usernameCheck = await validation.usernameCheck(urlUsername); | ||
const displayedUsernameCheck = | ||
validation.displayedNameCheck(displayedUsername); | ||
const bioCheck = validation.bioCheck(bio); | ||
|
||
if (!usernameCheck.isValid) { | ||
error(400, usernameCheck.message); | ||
} else if (!displayedUsernameCheck.isValid) { | ||
error(400, displayedUsernameCheck.message); | ||
} else if (!bioCheck.isValid) { | ||
error(400, bioCheck.message); | ||
} | ||
|
||
const { error: resError } = await supabase.from("users").insert({ | ||
id: authUser.id, | ||
url_username: urlUsername, | ||
displayed_username: displayedUsername, | ||
bio, | ||
}); | ||
|
||
if (resError) { | ||
error(500, resError.message); | ||
} | ||
|
||
return new Response("Inserted successfully"); | ||
}; |