Update API
This commit is contained in:
parent
c2c1acf5c6
commit
da8882b162
14 changed files with 329 additions and 0 deletions
22
src/pages/api/account/add.astro
Normal file
22
src/pages/api/account/add.astro
Normal 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>
|
18
src/pages/api/account/beta-off.ts
Normal file
18
src/pages/api/account/beta-off.ts
Normal 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")
|
||||||
|
}
|
18
src/pages/api/account/beta-on.ts
Normal file
18
src/pages/api/account/beta-on.ts
Normal 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")
|
||||||
|
}
|
14
src/pages/api/account/delete.ts
Normal file
14
src/pages/api/account/delete.ts
Normal 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")
|
||||||
|
}
|
19
src/pages/api/account/remove.astro
Normal file
19
src/pages/api/account/remove.astro
Normal 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>
|
21
src/pages/api/account/reset-password.ts
Normal file
21
src/pages/api/account/reset-password.ts
Normal 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")
|
||||||
|
}
|
24
src/pages/api/account/update-avatar.ts
Normal file
24
src/pages/api/account/update-avatar.ts
Normal 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")
|
||||||
|
}
|
24
src/pages/api/account/update-email.ts
Normal file
24
src/pages/api/account/update-email.ts
Normal 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")
|
||||||
|
}
|
22
src/pages/api/account/update-name.ts
Normal file
22
src/pages/api/account/update-name.ts
Normal 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")
|
||||||
|
}
|
21
src/pages/api/account/update-password.ts
Normal file
21
src/pages/api/account/update-password.ts
Normal 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")
|
||||||
|
}
|
31
src/pages/api/auth/callback.ts
Normal file
31
src/pages/api/auth/callback.ts
Normal 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("/")
|
||||||
|
}
|
36
src/pages/api/auth/register.ts
Normal file
36
src/pages/api/auth/register.ts
Normal 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("/")
|
||||||
|
}
|
52
src/pages/api/auth/signin.ts
Normal file
52
src/pages/api/auth/signin.ts
Normal 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("/")
|
||||||
|
}
|
7
src/pages/api/auth/signout.ts
Normal file
7
src/pages/api/auth/signout.ts
Normal 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")
|
||||||
|
}
|
Loading…
Reference in a new issue