MED-109: add doctor role and basic view (#45)
* MED-109: add doctor role and basic view * add role to accounts * remove old super admin and doctor sql
This commit is contained in:
28
packages/features/doctor/src/components/doctor-guard.tsx
Normal file
28
packages/features/doctor/src/components/doctor-guard.tsx
Normal file
@@ -0,0 +1,28 @@
|
||||
import { notFound } from 'next/navigation';
|
||||
|
||||
import { getSupabaseServerClient } from '@kit/supabase/server-client';
|
||||
import { isDoctor } from '../lib/server/utils/is-doctor';
|
||||
|
||||
|
||||
type LayoutOrPageComponent<Params> = React.ComponentType<Params>;
|
||||
|
||||
/**
|
||||
* DoctorGuard is a server component wrapper that checks if the user is a doctor before rendering the component.
|
||||
* If the user is not a doctor, we redirect to a 404.
|
||||
* @param Component - The Page or Layout component to wrap
|
||||
*/
|
||||
export function DoctorGuard<Params extends object>(
|
||||
Component: LayoutOrPageComponent<Params>,
|
||||
) {
|
||||
return async function DoctorGuardServerComponentWrapper(params: Params) {
|
||||
const client = getSupabaseServerClient();
|
||||
const isUserDoctor = await isDoctor(client);
|
||||
|
||||
// if the user is not a super-admin, we redirect to a 404
|
||||
if (!isUserDoctor) {
|
||||
notFound();
|
||||
}
|
||||
|
||||
return <Component {...params} />;
|
||||
};
|
||||
}
|
||||
1
packages/features/doctor/src/index.ts
Normal file
1
packages/features/doctor/src/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from './lib/server/utils/is-doctor';
|
||||
24
packages/features/doctor/src/lib/server/utils/is-doctor.ts
Normal file
24
packages/features/doctor/src/lib/server/utils/is-doctor.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import { SupabaseClient } from '@supabase/supabase-js';
|
||||
|
||||
import { Database } from '@kit/supabase/database';
|
||||
|
||||
/**
|
||||
* @name isDoctor
|
||||
* @description Check if the current user is a doctor.
|
||||
* @param client
|
||||
*/
|
||||
export async function isDoctor(client: SupabaseClient<Database>) {
|
||||
try {
|
||||
const { data, error } = await client
|
||||
.schema('medreport')
|
||||
.rpc('is_doctor');
|
||||
|
||||
if (error) {
|
||||
throw error;
|
||||
}
|
||||
|
||||
return data;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user