mirror of https://github.com/buster-so/buster.git
hide setting if a user is not valid
This commit is contained in:
parent
c6adb313c3
commit
aa6ce9f40e
|
@ -7,12 +7,15 @@ import { ClientRedirect } from '../../components/ui/layouts/ClientRedirect';
|
|||
import { prefetchGetMyUserInfo } from '@/api/buster_rest';
|
||||
import { getSupabaseUserContext } from '@/lib/supabase';
|
||||
import { AppProviders } from '@/context/AppProviders';
|
||||
import { headers } from 'next/headers';
|
||||
|
||||
export default async function Layout({
|
||||
children
|
||||
}: Readonly<{
|
||||
children: React.ReactNode;
|
||||
}>) {
|
||||
const headersList = headers();
|
||||
const pathname = headersList.get('x-pathname');
|
||||
const supabaseContext = await getSupabaseUserContext();
|
||||
const { accessToken } = supabaseContext;
|
||||
const { initialData: userInfo, queryClient } = await prefetchGetMyUserInfo({
|
||||
|
@ -23,7 +26,8 @@ export default async function Layout({
|
|||
|
||||
if (
|
||||
(!userInfo?.organizations?.[0]?.id || !userInfo?.user?.name) &&
|
||||
!supabaseContext.user?.is_anonymous
|
||||
!supabaseContext.user?.is_anonymous &&
|
||||
pathname !== newUserRoute
|
||||
) {
|
||||
return <ClientRedirect to={newUserRoute} />;
|
||||
}
|
||||
|
|
|
@ -120,6 +120,7 @@ const tryGroup = (
|
|||
|
||||
export const SidebarPrimary = React.memo(() => {
|
||||
const isAdmin = useUserConfigContextSelector((x) => x.isAdmin);
|
||||
const isUserRegistered = useUserConfigContextSelector((x) => x.isUserRegistered);
|
||||
const { data: favorites } = useGetUserFavorites();
|
||||
const currentRoute = useAppLayoutContextSelector((x) => x.currentRoute);
|
||||
const onToggleInviteModal = useInviteModalStore((s) => s.onToggleInviteModal);
|
||||
|
@ -132,6 +133,8 @@ export const SidebarPrimary = React.memo(() => {
|
|||
});
|
||||
|
||||
const sidebarItems: SidebarProps['content'] = useMemo(() => {
|
||||
if (!isUserRegistered) return [];
|
||||
|
||||
const items = [topItems];
|
||||
|
||||
if (isAdmin) {
|
||||
|
@ -147,11 +150,14 @@ export const SidebarPrimary = React.memo(() => {
|
|||
items.push(tryGroup(onToggleInviteModal, () => setOpenSupportModal(true)));
|
||||
|
||||
return items;
|
||||
}, [isAdmin, favorites, currentRoute, onFavoritesReorder]);
|
||||
}, [isAdmin, isUserRegistered, favorites, currentRoute, onFavoritesReorder]);
|
||||
|
||||
const onCloseSupportModal = useMemoizedFn(() => setOpenSupportModal(false));
|
||||
|
||||
const HeaderMemoized = useMemo(() => <SidebarPrimaryHeader />, []);
|
||||
const HeaderMemoized = useMemo(
|
||||
() => <SidebarPrimaryHeader hideActions={!isUserRegistered} />,
|
||||
[isUserRegistered]
|
||||
);
|
||||
const FooterMemoized = useMemo(() => <SidebarUserFooter />, []);
|
||||
|
||||
return (
|
||||
|
@ -170,7 +176,7 @@ export const SidebarPrimary = React.memo(() => {
|
|||
|
||||
SidebarPrimary.displayName = 'SidebarPrimary';
|
||||
|
||||
const SidebarPrimaryHeader: React.FC = () => {
|
||||
const SidebarPrimaryHeader: React.FC<{ hideActions?: boolean }> = ({ hideActions = false }) => {
|
||||
const onChangePage = useAppLayoutContextSelector((s) => s.onChangePage);
|
||||
useHotkeys('C', () => {
|
||||
onChangePage(BusterRoutes.APP_HOME);
|
||||
|
@ -179,6 +185,7 @@ const SidebarPrimaryHeader: React.FC = () => {
|
|||
return (
|
||||
<div className="flex items-center justify-between">
|
||||
<BusterLogoWithText />
|
||||
{!hideActions && (
|
||||
<div className="flex items-center gap-2">
|
||||
<Tooltip title="Settings">
|
||||
<Link href={createBusterRoute({ route: BusterRoutes.SETTINGS_PROFILE })}>
|
||||
|
@ -199,6 +206,7 @@ const SidebarPrimaryHeader: React.FC = () => {
|
|||
</Link>
|
||||
</Tooltip>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
|
|
@ -15,7 +15,10 @@ export const useUserConfigProvider = () => {
|
|||
const userOrganizations = userResponse?.organizations?.[0];
|
||||
const userRole = userOrganizations?.role;
|
||||
const isUserRegistered =
|
||||
!!userResponse && !!userResponse?.organizations?.[0]?.id && !!userResponse?.user?.name;
|
||||
!!userResponse &&
|
||||
!!userResponse?.organizations?.[0]?.id &&
|
||||
!!userResponse?.user?.name &&
|
||||
!isAnonymousUser;
|
||||
|
||||
const isAdmin = checkIfUserIsAdmin(userResponse);
|
||||
|
||||
|
|
|
@ -1,10 +1,13 @@
|
|||
import { NextResponse, type NextRequest } from 'next/server';
|
||||
import { updateSession } from '@/middleware/supabaseMiddleware';
|
||||
import { isPublicPage, BusterRoutes, createBusterRoute } from './routes';
|
||||
import { pathnameMiddleware } from './middleware/pathnameMiddleware';
|
||||
|
||||
export async function middleware(request: NextRequest) {
|
||||
try {
|
||||
const [supabaseResponse, user] = await updateSession(request);
|
||||
let [response, user] = await updateSession(request);
|
||||
|
||||
response = await pathnameMiddleware(request, response);
|
||||
|
||||
if ((!user || !user.id) && !isPublicPage(request)) {
|
||||
return NextResponse.redirect(
|
||||
|
@ -12,7 +15,7 @@ export async function middleware(request: NextRequest) {
|
|||
);
|
||||
}
|
||||
|
||||
return supabaseResponse;
|
||||
return response;
|
||||
} catch (error) {
|
||||
console.error('Error in middleware:', error);
|
||||
return NextResponse.next();
|
||||
|
|
|
@ -0,0 +1,8 @@
|
|||
import { NextRequest, NextResponse } from 'next/server';
|
||||
|
||||
export async function pathnameMiddleware(request: NextRequest, resonse: NextResponse) {
|
||||
const pathname = request.nextUrl.pathname;
|
||||
const response = NextResponse.next();
|
||||
response.headers.set('x-pathname', pathname);
|
||||
return response;
|
||||
}
|
Loading…
Reference in New Issue