From 97bacc79a637fc75243276571e8dfe7b8d4314a5 Mon Sep 17 00:00:00 2001 From: Daniel Farina Date: Wed, 22 Nov 2023 19:38:57 -0500 Subject: [PATCH] remove redundancy on supabase helpers --- packages/unsaged/utils/app/auth/helpers.ts | 57 ++++++++----------- .../unsaged/utils/app/auth/supabaseClient.ts | 10 ---- 2 files changed, 24 insertions(+), 43 deletions(-) delete mode 100644 packages/unsaged/utils/app/auth/supabaseClient.ts diff --git a/packages/unsaged/utils/app/auth/helpers.ts b/packages/unsaged/utils/app/auth/helpers.ts index 39d22242..73fea378 100644 --- a/packages/unsaged/utils/app/auth/helpers.ts +++ b/packages/unsaged/utils/app/auth/helpers.ts @@ -1,27 +1,23 @@ -import { useEffect, useState } from 'react'; import { createClientComponentClient } from '@supabase/auth-helpers-nextjs'; +import { Database } from '@/types/supabase.types'; -// Assuming you have initialized your Supabase client somewhere in your code -const supabase = createClientComponentClient(); +// Initialize your Supabase client once here +const supabaseClient = createClientComponentClient(); export async function getClientSession() { try { - const { data: sessionWrapper } = await supabase.auth.getSession(); - const session = sessionWrapper?.session; - - if (!session) { - return null; - } + const { + data: { session }, + } = await supabaseClient.auth.getSession(); const sessionData = { - user: session.user?.email || '', - expires: session.expires_at || '', - customAccessToken: session.access_token || '', + user: session?.user?.email || '', + expires: session?.expires_at || '', + customAccessToken: session?.access_token || '', }; return sessionData; } catch (error) { - // Handle the error here, e.g., log it or return an error response console.error(error); return null; } @@ -29,29 +25,24 @@ export async function getClientSession() { export async function getUser() { try { - const { data: sessionWrapper } = await supabase.auth.getSession(); - const session = sessionWrapper?.session; - - let user; - - if (session?.user) { - const { email, user_metadata } = session.user; - user = { - email: email || 'default_user', - image: '', - name: user_metadata?.full_name || 'Default User', - }; - } else { - user = { - email: 'default_user', - image: '', - name: 'Default User', - }; - } + const { + data: { session }, + } = await supabaseClient.auth.getSession(); + + const user = session?.user + ? { + email: session.user.email || 'default_user', + image: session.user.user_metadata?.avatar_url || '', + name: session.user.user_metadata?.full_name || 'Default User', + } + : { + email: 'default_user', + image: '', + name: 'Default User', + }; return user; } catch (error) { - // Handle the error here, e.g., log it or return an error response console.error(error); return null; } diff --git a/packages/unsaged/utils/app/auth/supabaseClient.ts b/packages/unsaged/utils/app/auth/supabaseClient.ts deleted file mode 100644 index 9ef1b61f..00000000 --- a/packages/unsaged/utils/app/auth/supabaseClient.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { createClient } from '@supabase/supabase-js'; - -const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL; -const supabaseAnonKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY; - -if (!supabaseUrl || !supabaseAnonKey) { - throw new Error("Supabase URL or Anonymous Key is undefined"); -} - -export const supabase = createClient(supabaseUrl, supabaseAnonKey);