Skip to content

Commit

Permalink
remove redundancy on supabase helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Farina authored and Daniel Farina committed Nov 23, 2023
1 parent 1db720e commit 97bacc7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 43 deletions.
57 changes: 24 additions & 33 deletions packages/unsaged/utils/app/auth/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,57 +1,48 @@
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;
}
}

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;
}
Expand Down
10 changes: 0 additions & 10 deletions packages/unsaged/utils/app/auth/supabaseClient.ts

This file was deleted.

0 comments on commit 97bacc7

Please sign in to comment.