Update API

This commit is contained in:
Korbs 2024-07-16 23:39:14 -04:00
parent c2c1acf5c6
commit da8882b162
14 changed files with 329 additions and 0 deletions

View file

@ -0,0 +1,22 @@
---
import Base from "@layouts/Base.astro"
const CreatorId = Astro.url.href.split("add?=").pop()
import { supabase } from "../../../library/supabase"
const { data: { user } } = await supabase.auth.getUser()
const { data, error } = await supabase
.from('subs')
.insert([
{
UserSubscribed: user?.id,
Id: CreatorId,
Platform: "YouTube"
},
])
.select()
---
<Base Title="Dashboard">
<div class="content">
<p>One moment...</p>
</div>
</Base>

View file

@ -0,0 +1,18 @@
import type { APIRoute } from "astro"
import { supabase } from "@library/supabase"
export const POST: APIRoute = async ({ redirect }) => {
// https://supabase.com/docs/reference/javascript/auth-updateuser?example=update-the-users-metadata
const { error } = await supabase.auth.updateUser({
data: {
beta: false
}
})
if (error) {
return new Response(error.message, { status: 500 })
}
return redirect("/account")
}

View file

@ -0,0 +1,18 @@
import type { APIRoute } from "astro"
import { supabase } from "@library/supabase"
export const POST: APIRoute = async ({ redirect }) => {
// https://supabase.com/docs/reference/javascript/auth-updateuser?example=update-the-users-metadata
const { error } = await supabase.auth.updateUser({
data: {
beta: true
}
})
if (error) {
return new Response(error.message, { status: 500 })
}
return redirect("/account")
}

View file

@ -0,0 +1,14 @@
import type { APIRoute } from "astro"
import { supabase } from "@library/supabase"
export const POST: APIRoute = async ({ redirect }) => {
// https://supabase.com/docs/reference/javascript/auth-updateuser?example=update-the-users-metadata
const { error } = await supabase.rpc('delete_user');
if (error) {
return new Response(error.message, { status: 500 })
}
return redirect("/signout")
}

View file

@ -0,0 +1,19 @@
---
import Base from "@layouts/Base.astro"
const CreatorId = Astro.url.href.split("remove?=").pop()
import { supabase } from "../../../library/supabase"
const { data: { user } } = await supabase.auth.getUser()
const id = user?.id
const { data, error } = await supabase
.from('subs')
.delete()
.eq('UserSubscribed', id)
.eq('Id', CreatorId)
.eq('Platform', 'YouTube')
---
<Base Title="Dashboard">
<div class="content">
<p>One moment...</p>
</div>
</Base>

View file

@ -0,0 +1,21 @@
import type { APIRoute } from "astro"
import { supabase } from "@library/supabase"
export const POST: APIRoute = async ({ request, redirect }) => {
const formData = await request.formData()
const password = formData.get("password")?.toString()
if (!password) {
return new Response("Error.", { status: 400 })
}
const { error } = await supabase.auth.updateUser({
password: password,
})
if (error) {
return new Response(error.message, { status: 500 })
}
return redirect("/signin")
}

View file

@ -0,0 +1,24 @@
import type { APIRoute } from "astro"
import { supabase } from "@library/supabase"
export const POST: APIRoute = async ({ request, redirect }) => {
const formData = await request.formData()
const avatar_url = formData.get("avatar_url")?.toString()
if (!avatar_url) {
return new Response("Error.", { status: 400 })
}
// https://supabase.com/docs/reference/javascript/auth-updateuser?example=update-the-users-metadata
const { error } = await supabase.auth.updateUser({
data: {
avatar_url: avatar_url
}
})
if (error) {
return new Response(error.message, { status: 500 })
}
return redirect("/account?=AvatarUpdated")
}

View file

@ -0,0 +1,24 @@
import type { APIRoute } from "astro"
import { supabase } from "@library/supabase"
export const POST: APIRoute = async ({ request, redirect }) => {
const formData = await request.formData()
const NewEmail = formData.get("email")?.toString()
if (!NewEmail) {
return new Response("Error.", { status: 400 })
}
const { error } = await supabase.auth.updateUser({
email: NewEmail,
data: {
email_verified: false
}
})
if (error) {
return new Response(error.message, { status: 500 })
}
return redirect("/account?=CheckEmail")
}

View file

@ -0,0 +1,22 @@
import type { APIRoute } from "astro"
import { supabase } from "@library/supabase"
export const POST: APIRoute = async ({ request, redirect }) => {
const formData = await request.formData()
const name = formData.get("name")?.toString()
if (!name) {
return new Response("Error.", { status: 400 })
}
// https://supabase.com/docs/reference/javascript/auth-updateuser?example=update-the-users-metadata
const { error } = await supabase.auth.updateUser({
data: {full_name: name}
})
if (error) {
return new Response(error.message, { status: 500 })
}
return redirect("/account?=NameUpdated")
}

View file

@ -0,0 +1,21 @@
import type { APIRoute } from "astro"
import { supabase } from "@library/supabase"
export const POST: APIRoute = async ({ request, redirect }) => {
const formData = await request.formData()
const password = formData.get("password")?.toString()
if (!password) {
return new Response("Error.", { status: 400 })
}
const { error } = await supabase.auth.updateUser({
password: password,
})
if (error) {
return new Response(error.message, { status: 500 })
}
return redirect("/account?=PasswordReset")
}

View file

@ -0,0 +1,31 @@
import type { APIRoute } from "astro"
import { supabase } from "@library/supabase"
export const GET: APIRoute = async ({ url, cookies, redirect }) => {
const authCode = url.searchParams.get("code")
if (!authCode) {
return new Response("No code provided", { status: 400 })
}
// https://supabase.com/docs/reference/javascript/auth-mfa-api
const { data, error } = await supabase.auth.exchangeCodeForSession(authCode)
if (error) {
return new Response(error.message, { status: 500 })
}
const { access_token, refresh_token } = data.session
cookies.set("sb-access-token", access_token, {
path: "/",
secure: true,
httpOnly: true,
})
cookies.set("sb-refresh-token", refresh_token, {
path: "/",
secure: true,
httpOnly: true,
})
return redirect("/")
}

View file

@ -0,0 +1,36 @@
import type { APIRoute } from "astro"
import { supabase } from "@library/supabase"
export const POST: APIRoute = async ({ request, redirect }) => {
const formData = await request.formData()
const avatar_url = formData.get("avatar_url")?.toString()
const name = formData.get("name")?.toString()
const email = formData.get("email")?.toString()
const password = formData.get("password")?.toString()
if (!email || !password || !name || !avatar_url) {
return new Response("Email and password are required", { status: 400 })
}
// https://supabase.com/docs/reference/javascript/auth-signup?example=sign-up-with-additional-user-metadata
const { error } = await supabase.auth.signUp({
options: {
data: {
full_name: name,
avatar_url: avatar_url,
plan: "Free",
theme: "Dark",
color_scheme: "Green",
beta: true
}
},
email,
password,
})
if (error) {
return new Response(error.message, { status: 500 })
}
return redirect("/")
}

View file

@ -0,0 +1,52 @@
import type { APIRoute } from "astro"
import { supabase } from "@library/supabase"
import type { Provider } from "@supabase/supabase-js"
export const POST: APIRoute = async ({ request, cookies, redirect }) => {
const formData = await request.formData()
const email = formData.get("email")?.toString()
const password = formData.get("password")?.toString()
const provider = formData.get("provider")?.toString()
// https://supabase.com/docs/reference/javascript/auth-signinwithoauth?example=sign-in-using-a-third-party-provider
if (provider) {
const { data, error } = await supabase.auth.signInWithOAuth({
provider: provider as Provider,
options: {redirectTo: "http://localhost:4321/api/auth/callback"},
})
if (error) {
return new Response(error.message, { status: 500 })
}
return redirect(data.url)
}
if (!email || !password) {
return new Response("Email and password are required", { status: 400 })
}
// https://supabase.com/docs/reference/javascript/auth-signinwithpassword?example=sign-in-with-email-and-password
const { data, error } = await supabase.auth.signInWithPassword({
email,
password,
})
if (error) {
return new Response(error.message, { status: 500 })
}
const { access_token, refresh_token } = data.session
cookies.set("sb-access-token", access_token, {
sameSite: "strict",
path: "/",
secure: true,
})
cookies.set("sb-refresh-token", refresh_token, {
sameSite: "strict",
path: "/",
secure: true,
})
return redirect("/")
}

View file

@ -0,0 +1,7 @@
import type { APIRoute } from "astro"
export const GET: APIRoute = async ({ cookies, redirect }) => {
cookies.delete("sb-access-token", { path: "/" })
cookies.delete("sb-refresh-token", { path: "/" })
return redirect("/signin")
}