Refactor authentication flow and improve code organization
The update implemented a redirect functionality in the multi-factor authentication flow for a better user experience. It also involved a refactoring of some parts of the code, substituting direct routing paths with path configs for easier future modifications. Import statements were adjusted for better code organization and readability.
This commit is contained in:
@@ -17,10 +17,14 @@
|
||||
"@kit/prettier-config": "0.1.0",
|
||||
"@kit/tailwind-config": "0.1.0",
|
||||
"@kit/tsconfig": "0.1.0",
|
||||
"@kit/ui": "*"
|
||||
"@kit/ui": "*",
|
||||
"@kit/supabase": "*",
|
||||
"@supabase/supabase-js": "2.40.0",
|
||||
"lucide-react": "^0.363.0"
|
||||
},
|
||||
"exports": {
|
||||
".": "./src/index.ts"
|
||||
".": "./src/index.ts",
|
||||
"./components/*": "./src/components/*"
|
||||
},
|
||||
"eslintConfig": {
|
||||
"root": true,
|
||||
|
||||
@@ -7,7 +7,7 @@ interface Data {
|
||||
trialSubscriptions: number;
|
||||
}
|
||||
|
||||
function AdminDashboard({
|
||||
export function AdminDashboard({
|
||||
data,
|
||||
}: React.PropsWithChildren<{
|
||||
data: Data;
|
||||
@@ -70,8 +70,6 @@ function AdminDashboard({
|
||||
);
|
||||
}
|
||||
|
||||
export default AdminDashboard;
|
||||
|
||||
function Figure(props: React.PropsWithChildren) {
|
||||
return <div className={'text-3xl font-bold'}>{props.children}</div>;
|
||||
}
|
||||
@@ -1,22 +1,23 @@
|
||||
import { notFound } from 'next/navigation';
|
||||
|
||||
import isUserSuperAdmin from '../../../app/admin/utils/is-user-super-admin';
|
||||
import { getSupabaseServerComponentClient } from '@kit/supabase/server-component-client';
|
||||
|
||||
import { isSuperAdmin } from '../lib/is-super-admin';
|
||||
|
||||
type LayoutOrPageComponent<Params> = React.ComponentType<Params>;
|
||||
|
||||
function AdminGuard<Params extends object>(
|
||||
export function AdminGuard<Params extends object>(
|
||||
Component: LayoutOrPageComponent<Params>,
|
||||
) {
|
||||
return async function AdminGuardServerComponentWrapper(params: Params) {
|
||||
const isAdmin = await isUserSuperAdmin();
|
||||
const client = getSupabaseServerComponentClient();
|
||||
const isUserSuperAdmin = await isSuperAdmin(client);
|
||||
|
||||
// if the user is not a super-admin, we redirect to a 404
|
||||
if (!isAdmin) {
|
||||
if (!isUserSuperAdmin) {
|
||||
notFound();
|
||||
}
|
||||
|
||||
return <Component {...params} />;
|
||||
};
|
||||
}
|
||||
|
||||
export default AdminGuard;
|
||||
@@ -5,11 +5,10 @@ import { ArrowLeft } from 'lucide-react';
|
||||
import { Button } from '@kit/ui/button';
|
||||
import { PageHeader } from '@kit/ui/page';
|
||||
|
||||
function AdminHeader({
|
||||
export function AdminHeader({
|
||||
children,
|
||||
paths,
|
||||
}: React.PropsWithChildren<{
|
||||
appHome: string;
|
||||
paths: {
|
||||
appHome: string;
|
||||
};
|
||||
@@ -28,5 +27,3 @@ function AdminHeader({
|
||||
</PageHeader>
|
||||
);
|
||||
}
|
||||
|
||||
export default AdminHeader;
|
||||
@@ -1,10 +1,8 @@
|
||||
'use client';
|
||||
|
||||
import { Home, User, Users } from 'lucide-react';
|
||||
|
||||
import { Sidebar, SidebarContent, SidebarItem } from '@kit/ui/sidebar';
|
||||
|
||||
function AdminSidebar(props: { Logo: React.ReactNode }) {
|
||||
export function AdminSidebar(props: { Logo: React.ReactNode }) {
|
||||
return (
|
||||
<Sidebar>
|
||||
<SidebarContent className={'mb-6 mt-4 pt-2'}>{props.Logo}</SidebarContent>
|
||||
@@ -28,5 +26,3 @@ function AdminSidebar(props: { Logo: React.ReactNode }) {
|
||||
</Sidebar>
|
||||
);
|
||||
}
|
||||
|
||||
export default AdminSidebar;
|
||||
1
packages/features/admin/src/index.ts
Normal file
1
packages/features/admin/src/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from './lib/is-super-admin';
|
||||
19
packages/features/admin/src/lib/is-super-admin.ts
Normal file
19
packages/features/admin/src/lib/is-super-admin.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import { SupabaseClient } from '@supabase/supabase-js';
|
||||
|
||||
import { Database } from '@kit/supabase/database';
|
||||
|
||||
export async function isSuperAdmin(client: SupabaseClient<Database>) {
|
||||
const { data, error } = await client.auth.getUser();
|
||||
|
||||
if (error) {
|
||||
throw error;
|
||||
}
|
||||
|
||||
if (!data.user) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const appMetadata = data.user.app_metadata;
|
||||
|
||||
return appMetadata?.role === 'super-admin';
|
||||
}
|
||||
Reference in New Issue
Block a user