Files
myeasycms-v2/apps/web/app/(dashboard)/home/_lib/load-user-workspace.ts
giancarlo df239e9903 Update user authentication and refactor UI components
Refactored the user authentication method in the dashboard from getSession to getUser, and updated its related variables accordingly. UI components have also been modified, which includes streamlining the importation of the 'cn' function and exporting the 'Stepper' function directly. Lastly, the 'Stepper' function is now included in the package.json for the UI component package.
2024-04-15 19:07:59 +08:00

40 lines
995 B
TypeScript

import { cache } from 'react';
import { getSupabaseServerComponentClient } from '@kit/supabase/server-component-client';
import featureFlagsConfig from '~/config/feature-flags.config';
import { Database } from '~/lib/database.types';
export const loadUserWorkspace = cache(async () => {
const client = getSupabaseServerComponentClient();
const loadAccounts = featureFlagsConfig.enableTeamAccounts;
const accounts = loadAccounts ? await loadUserAccounts(client) : [];
const { data } = await client.auth.getUser();
return {
accounts,
user: data.user,
};
});
async function loadUserAccounts(
client: ReturnType<typeof getSupabaseServerComponentClient<Database>>,
) {
const { data: accounts, error } = await client
.from('user_accounts')
.select(`name, slug, picture_url`);
if (error) {
throw error;
}
return accounts.map(({ name, slug, picture_url }) => {
return {
label: name,
value: slug,
image: picture_url,
};
});
}