From 8137c58cd891e68fe93d2900989d50c9e0c09efa Mon Sep 17 00:00:00 2001 From: vergiLgood1 Date: Wed, 19 Feb 2025 00:05:16 +0700 Subject: [PATCH] add some hooks --- sigap-website/.vscode/extensions.json | 3 ++ sigap-website/.vscode/settings.json | 24 +++++++++ sigap-website/hooks/use-navigations.ts | 30 +++++++++++ sigap-website/hooks/use-supabase.ts | 74 ++++++++++++++++++++++++++ supabase/.env | 22 -------- supabase/config.toml | 5 -- supabase/functions/resend/.npmrc | 3 -- supabase/functions/resend/deno.json | 3 -- supabase/functions/resend/index.ts | 32 ----------- supabase/functions/send-email/index.ts | 2 +- 10 files changed, 132 insertions(+), 66 deletions(-) create mode 100644 sigap-website/.vscode/extensions.json create mode 100644 sigap-website/.vscode/settings.json create mode 100644 sigap-website/hooks/use-navigations.ts create mode 100644 sigap-website/hooks/use-supabase.ts delete mode 100644 supabase/.env delete mode 100644 supabase/functions/resend/.npmrc delete mode 100644 supabase/functions/resend/deno.json delete mode 100644 supabase/functions/resend/index.ts diff --git a/sigap-website/.vscode/extensions.json b/sigap-website/.vscode/extensions.json new file mode 100644 index 0000000..74baffc --- /dev/null +++ b/sigap-website/.vscode/extensions.json @@ -0,0 +1,3 @@ +{ + "recommendations": ["denoland.vscode-deno"] +} diff --git a/sigap-website/.vscode/settings.json b/sigap-website/.vscode/settings.json new file mode 100644 index 0000000..af62c23 --- /dev/null +++ b/sigap-website/.vscode/settings.json @@ -0,0 +1,24 @@ +{ + "deno.enablePaths": [ + "supabase/functions" + ], + "deno.lint": true, + "deno.unstable": [ + "bare-node-builtins", + "byonm", + "sloppy-imports", + "unsafe-proto", + "webgpu", + "broadcast-channel", + "worker-options", + "cron", + "kv", + "ffi", + "fs", + "http", + "net" + ], + "[typescript]": { + "editor.defaultFormatter": "denoland.vscode-deno" + } +} diff --git a/sigap-website/hooks/use-navigations.ts b/sigap-website/hooks/use-navigations.ts new file mode 100644 index 0000000..aaa673b --- /dev/null +++ b/sigap-website/hooks/use-navigations.ts @@ -0,0 +1,30 @@ +import { + useParams, + usePathname, + useRouter, + useSearchParams, +} from "next/navigation"; +import { useState } from "react"; + +export const useNavigations = () => { + const [loading, setLoading] = useState(false); + const [open, setOpen] = useState(false); + const [active, setActive] = useState(""); + const router = useRouter(); + const params = useParams(); + const searchParams = useSearchParams(); + const pathname = usePathname(); + + return { + loading, + setLoading, + open, + setOpen, + active, + setActive, + router, + params, + searchParams, + pathname, + }; +}; diff --git a/sigap-website/hooks/use-supabase.ts b/sigap-website/hooks/use-supabase.ts new file mode 100644 index 0000000..2aac5c2 --- /dev/null +++ b/sigap-website/hooks/use-supabase.ts @@ -0,0 +1,74 @@ +/** + * Custom hook to interact with Supabase client. + * + * @returns {Object} An object containing the Supabase client and session management functions. + * + * @function + * @name useSupabase + * + * @example + * const { supabase, getSession, refreshSession, setSession } = useSupabase(); + * + * @property {Object} supabase - The Supabase client instance. + * @property {Function} getSession - Function to get the current session. + * @property {Function} refreshSession - Function to refresh the current session. + * @property {Function} setSession - Function to set a new session with access and refresh tokens. + * + * @typedef {Object} Session + * @property {string} access_token - The access token for the session. + * @property {string} refresh_token - The refresh token for the session. + * + * @throws {Error} Throws an error if there is an issue with getting or setting the session. + */ +// + +import { createClient } from "@/utils/supabase/client"; + +export const useSupabase = () => { + const supabase = createClient(); + + const getSession = async () => { + const { + data: { session }, + error, + } = await supabase.auth.getSession(); + + if (error) { + throw new Error(error.message); + } + + const { access_token, refresh_token }: any = session; + + await setSession(access_token, refresh_token); + + return session; + }; + + const refreshSession = async () => { + const { + data: { session }, + error, + } = await supabase.auth.refreshSession(); + + return session; + }; + + const setSession = async (access_token: string, refresh_token: string) => { + const { data, error } = await supabase.auth.setSession({ + access_token, + refresh_token, + }); + + if (error) { + throw new Error(error.message); + } + + return true; + }; + + return { + getSession, + refreshSession, + setSession, + }; +}; diff --git a/supabase/.env b/supabase/.env deleted file mode 100644 index a69dd76..0000000 --- a/supabase/.env +++ /dev/null @@ -1,22 +0,0 @@ -# Update these with your Supabase details from your project settings > API -# https://app.supabase.com/project/_/settings/api - -# Supabase Production URL -NEXT_PUBLIC_SUPABASE_URL="https://cppejroeyonsqxulinaj.supabase.co" -NEXT_PUBLIC_SUPABASE_ANON_KEY="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImNwcGVqcm9leW9uc3F4dWxpbmFqIiwicm9sZSI6ImFub24iLCJpYXQiOjE3MzkzNjExMjYsImV4cCI6MjA1NDkzNzEyNn0.36XzD3ANlAmAYObYGxXkkydXLjN0VPv2rMNHnQcHoZU" - -# Supabase Local URL -# NEXT_PUBLIC_SUPABASE_URL=http://127.0.0.1:54321 -# NEXT_PUBLIC_SUPABASE_ANON_KEY=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZS1kZW1vIiwicm9sZSI6ImFub24iLCJleHAiOjE5ODM4MTI5OTZ9.CRXP1A7WOeoJeXxjNni43kdQwgnWNReilDMblYTn_I0 - -# Supabase Service Role Secret Key -SERVICE_ROLE_SECRET="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImNwcGVqcm9leW9uc3F4dWxpbmFqIiwicm9sZSI6InNlcnZpY2Vfcm9sZSIsImlhdCI6MTczOTM2MTEyNiwiZXhwIjoyMDU0OTM3MTI2fQ.iYIVeUChLIcC7NRaeJ6dViI9JiUZSMUKufFsDTfAkjA" -RESEND_API_KEY_TES="re_WtXdegYe_Ey3yiShKfZZtjCyY1agkEaSi" -SEND_EMAIL_HOOK_SECRET_TES="jeroAB/CXdS721OiHV0Ac0yRcxO7eNihgjblH62xMhLBNc6OwK3DQnkbrHjTlSw5anml2onNTolG3SzZ" - -# db connection string -# Connect to Supabase via connection pooling with Supavisor. -DATABASE_URL="postgresql://prisma.cppejroeyonsqxulinaj:prisma@aws-0-ap-southeast-1.pooler.supabase.com:6543/postgres?pgbouncer=true" - -# Direct connection to the database. Used for migrations. -DIRECT_URL="postgresql://prisma.cppejroeyonsqxulinaj:prisma@aws-0-ap-southeast-1.pooler.supabase.com:5432/postgres" diff --git a/supabase/config.toml b/supabase/config.toml index 6a31568..532caf8 100644 --- a/supabase/config.toml +++ b/supabase/config.toml @@ -288,8 +288,3 @@ s3_region = "env(S3_REGION)" s3_access_key = "env(S3_ACCESS_KEY)" # Configures AWS_SECRET_ACCESS_KEY for S3 bucket s3_secret_key = "env(S3_SECRET_KEY)" - -[auth.hook.send_email] -enabled = true -uri = "http://host.docker.internal:54321/functions/v1/send-email" -secrets = "v1,whsec_jeroAB/CXdS721OiHV0Ac0yRcxO7eNihgjblH62xMhLBNc6OwK3DQnkbrHjTlSw5anml2onNTolG3SzZ" \ No newline at end of file diff --git a/supabase/functions/resend/.npmrc b/supabase/functions/resend/.npmrc deleted file mode 100644 index 48c6388..0000000 --- a/supabase/functions/resend/.npmrc +++ /dev/null @@ -1,3 +0,0 @@ -# Configuration for private npm package dependencies -# For more information on using private registries with Edge Functions, see: -# https://supabase.com/docs/guides/functions/import-maps#importing-from-private-registries diff --git a/supabase/functions/resend/deno.json b/supabase/functions/resend/deno.json deleted file mode 100644 index f6ca845..0000000 --- a/supabase/functions/resend/deno.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "imports": {} -} diff --git a/supabase/functions/resend/index.ts b/supabase/functions/resend/index.ts deleted file mode 100644 index 6c8dcfa..0000000 --- a/supabase/functions/resend/index.ts +++ /dev/null @@ -1,32 +0,0 @@ -// Follow this setup guide to integrate the Deno language server with your editor: -// https://deno.land/manual/getting_started/setup_your_environment -// This enables autocomplete, go to definition, etc. - -// Setup type definitions for built-in Supabase Runtime APIs -import "jsr:@supabase/functions-js/edge-runtime.d.ts" - -console.log("Hello from Functions!") - -Deno.serve(async (req) => { - const { name } = await req.json() - const data = { - message: `Hello ${name}!`, - } - - return new Response( - JSON.stringify(data), - { headers: { "Content-Type": "application/json" } }, - ) -}) - -/* To invoke locally: - - 1. Run `supabase start` (see: https://supabase.com/docs/reference/cli/supabase-start) - 2. Make an HTTP request: - - curl -i --location --request POST 'http://127.0.0.1:54321/functions/v1/resend' \ - --header 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZS1kZW1vIiwicm9sZSI6ImFub24iLCJleHAiOjE5ODM4MTI5OTZ9.CRXP1A7WOeoJeXxjNni43kdQwgnWNReilDMblYTn_I0' \ - --header 'Content-Type: application/json' \ - --data '{"name":"Functions"}' - -*/ diff --git a/supabase/functions/send-email/index.ts b/supabase/functions/send-email/index.ts index 157ad6e..ede9768 100644 --- a/supabase/functions/send-email/index.ts +++ b/supabase/functions/send-email/index.ts @@ -1,7 +1,7 @@ import { Webhook } from "https://esm.sh/standardwebhooks@1.0.0"; import { Resend } from "npm:resend"; -const resend = new Resend("re_WtXdegYe_Ey3yiShKfZZtjCyY1agkEaSi"); +const resend = new Resend("re_4EyTgztQ_3istGdHQFeSTQsLBtH6oAdub" as string); const hookSecret = "jeroAB/CXdS721OiHV0Ac0yRcxO7eNihgjblH62xMhLBNc6OwK3DQnkbrHjTlSw5anml2onNTolG3SzZ" as string;