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 { prefetchGetMyUserInfo } from '@/api/buster_rest';
|
||||||
import { getSupabaseUserContext } from '@/lib/supabase';
|
import { getSupabaseUserContext } from '@/lib/supabase';
|
||||||
import { AppProviders } from '@/context/AppProviders';
|
import { AppProviders } from '@/context/AppProviders';
|
||||||
|
import { headers } from 'next/headers';
|
||||||
|
|
||||||
export default async function Layout({
|
export default async function Layout({
|
||||||
children
|
children
|
||||||
}: Readonly<{
|
}: Readonly<{
|
||||||
children: React.ReactNode;
|
children: React.ReactNode;
|
||||||
}>) {
|
}>) {
|
||||||
|
const headersList = headers();
|
||||||
|
const pathname = headersList.get('x-pathname');
|
||||||
const supabaseContext = await getSupabaseUserContext();
|
const supabaseContext = await getSupabaseUserContext();
|
||||||
const { accessToken } = supabaseContext;
|
const { accessToken } = supabaseContext;
|
||||||
const { initialData: userInfo, queryClient } = await prefetchGetMyUserInfo({
|
const { initialData: userInfo, queryClient } = await prefetchGetMyUserInfo({
|
||||||
|
@ -23,7 +26,8 @@ export default async function Layout({
|
||||||
|
|
||||||
if (
|
if (
|
||||||
(!userInfo?.organizations?.[0]?.id || !userInfo?.user?.name) &&
|
(!userInfo?.organizations?.[0]?.id || !userInfo?.user?.name) &&
|
||||||
!supabaseContext.user?.is_anonymous
|
!supabaseContext.user?.is_anonymous &&
|
||||||
|
pathname !== newUserRoute
|
||||||
) {
|
) {
|
||||||
return <ClientRedirect to={newUserRoute} />;
|
return <ClientRedirect to={newUserRoute} />;
|
||||||
}
|
}
|
||||||
|
|
|
@ -120,6 +120,7 @@ const tryGroup = (
|
||||||
|
|
||||||
export const SidebarPrimary = React.memo(() => {
|
export const SidebarPrimary = React.memo(() => {
|
||||||
const isAdmin = useUserConfigContextSelector((x) => x.isAdmin);
|
const isAdmin = useUserConfigContextSelector((x) => x.isAdmin);
|
||||||
|
const isUserRegistered = useUserConfigContextSelector((x) => x.isUserRegistered);
|
||||||
const { data: favorites } = useGetUserFavorites();
|
const { data: favorites } = useGetUserFavorites();
|
||||||
const currentRoute = useAppLayoutContextSelector((x) => x.currentRoute);
|
const currentRoute = useAppLayoutContextSelector((x) => x.currentRoute);
|
||||||
const onToggleInviteModal = useInviteModalStore((s) => s.onToggleInviteModal);
|
const onToggleInviteModal = useInviteModalStore((s) => s.onToggleInviteModal);
|
||||||
|
@ -132,6 +133,8 @@ export const SidebarPrimary = React.memo(() => {
|
||||||
});
|
});
|
||||||
|
|
||||||
const sidebarItems: SidebarProps['content'] = useMemo(() => {
|
const sidebarItems: SidebarProps['content'] = useMemo(() => {
|
||||||
|
if (!isUserRegistered) return [];
|
||||||
|
|
||||||
const items = [topItems];
|
const items = [topItems];
|
||||||
|
|
||||||
if (isAdmin) {
|
if (isAdmin) {
|
||||||
|
@ -147,11 +150,14 @@ export const SidebarPrimary = React.memo(() => {
|
||||||
items.push(tryGroup(onToggleInviteModal, () => setOpenSupportModal(true)));
|
items.push(tryGroup(onToggleInviteModal, () => setOpenSupportModal(true)));
|
||||||
|
|
||||||
return items;
|
return items;
|
||||||
}, [isAdmin, favorites, currentRoute, onFavoritesReorder]);
|
}, [isAdmin, isUserRegistered, favorites, currentRoute, onFavoritesReorder]);
|
||||||
|
|
||||||
const onCloseSupportModal = useMemoizedFn(() => setOpenSupportModal(false));
|
const onCloseSupportModal = useMemoizedFn(() => setOpenSupportModal(false));
|
||||||
|
|
||||||
const HeaderMemoized = useMemo(() => <SidebarPrimaryHeader />, []);
|
const HeaderMemoized = useMemo(
|
||||||
|
() => <SidebarPrimaryHeader hideActions={!isUserRegistered} />,
|
||||||
|
[isUserRegistered]
|
||||||
|
);
|
||||||
const FooterMemoized = useMemo(() => <SidebarUserFooter />, []);
|
const FooterMemoized = useMemo(() => <SidebarUserFooter />, []);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
@ -170,7 +176,7 @@ export const SidebarPrimary = React.memo(() => {
|
||||||
|
|
||||||
SidebarPrimary.displayName = 'SidebarPrimary';
|
SidebarPrimary.displayName = 'SidebarPrimary';
|
||||||
|
|
||||||
const SidebarPrimaryHeader: React.FC = () => {
|
const SidebarPrimaryHeader: React.FC<{ hideActions?: boolean }> = ({ hideActions = false }) => {
|
||||||
const onChangePage = useAppLayoutContextSelector((s) => s.onChangePage);
|
const onChangePage = useAppLayoutContextSelector((s) => s.onChangePage);
|
||||||
useHotkeys('C', () => {
|
useHotkeys('C', () => {
|
||||||
onChangePage(BusterRoutes.APP_HOME);
|
onChangePage(BusterRoutes.APP_HOME);
|
||||||
|
@ -179,26 +185,28 @@ const SidebarPrimaryHeader: React.FC = () => {
|
||||||
return (
|
return (
|
||||||
<div className="flex items-center justify-between">
|
<div className="flex items-center justify-between">
|
||||||
<BusterLogoWithText />
|
<BusterLogoWithText />
|
||||||
<div className="flex items-center gap-2">
|
{!hideActions && (
|
||||||
<Tooltip title="Settings">
|
<div className="flex items-center gap-2">
|
||||||
<Link href={createBusterRoute({ route: BusterRoutes.SETTINGS_PROFILE })}>
|
<Tooltip title="Settings">
|
||||||
<Button prefix={<Gear />} variant="ghost" />
|
<Link href={createBusterRoute({ route: BusterRoutes.SETTINGS_PROFILE })}>
|
||||||
</Link>
|
<Button prefix={<Gear />} variant="ghost" />
|
||||||
</Tooltip>
|
</Link>
|
||||||
<Tooltip title="Start a chat" shortcuts={['C']}>
|
</Tooltip>
|
||||||
<Link href={createBusterRoute({ route: BusterRoutes.APP_HOME })}>
|
<Tooltip title="Start a chat" shortcuts={['C']}>
|
||||||
<Button
|
<Link href={createBusterRoute({ route: BusterRoutes.APP_HOME })}>
|
||||||
size="tall"
|
<Button
|
||||||
rounding={'large'}
|
size="tall"
|
||||||
prefix={
|
rounding={'large'}
|
||||||
<div className="flex items-center justify-center">
|
prefix={
|
||||||
<PencilSquareIcon />
|
<div className="flex items-center justify-center">
|
||||||
</div>
|
<PencilSquareIcon />
|
||||||
}
|
</div>
|
||||||
/>
|
}
|
||||||
</Link>
|
/>
|
||||||
</Tooltip>
|
</Link>
|
||||||
</div>
|
</Tooltip>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
|
@ -15,7 +15,10 @@ export const useUserConfigProvider = () => {
|
||||||
const userOrganizations = userResponse?.organizations?.[0];
|
const userOrganizations = userResponse?.organizations?.[0];
|
||||||
const userRole = userOrganizations?.role;
|
const userRole = userOrganizations?.role;
|
||||||
const isUserRegistered =
|
const isUserRegistered =
|
||||||
!!userResponse && !!userResponse?.organizations?.[0]?.id && !!userResponse?.user?.name;
|
!!userResponse &&
|
||||||
|
!!userResponse?.organizations?.[0]?.id &&
|
||||||
|
!!userResponse?.user?.name &&
|
||||||
|
!isAnonymousUser;
|
||||||
|
|
||||||
const isAdmin = checkIfUserIsAdmin(userResponse);
|
const isAdmin = checkIfUserIsAdmin(userResponse);
|
||||||
|
|
||||||
|
|
|
@ -1,10 +1,13 @@
|
||||||
import { NextResponse, type NextRequest } from 'next/server';
|
import { NextResponse, type NextRequest } from 'next/server';
|
||||||
import { updateSession } from '@/middleware/supabaseMiddleware';
|
import { updateSession } from '@/middleware/supabaseMiddleware';
|
||||||
import { isPublicPage, BusterRoutes, createBusterRoute } from './routes';
|
import { isPublicPage, BusterRoutes, createBusterRoute } from './routes';
|
||||||
|
import { pathnameMiddleware } from './middleware/pathnameMiddleware';
|
||||||
|
|
||||||
export async function middleware(request: NextRequest) {
|
export async function middleware(request: NextRequest) {
|
||||||
try {
|
try {
|
||||||
const [supabaseResponse, user] = await updateSession(request);
|
let [response, user] = await updateSession(request);
|
||||||
|
|
||||||
|
response = await pathnameMiddleware(request, response);
|
||||||
|
|
||||||
if ((!user || !user.id) && !isPublicPage(request)) {
|
if ((!user || !user.id) && !isPublicPage(request)) {
|
||||||
return NextResponse.redirect(
|
return NextResponse.redirect(
|
||||||
|
@ -12,7 +15,7 @@ export async function middleware(request: NextRequest) {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return supabaseResponse;
|
return response;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error in middleware:', error);
|
console.error('Error in middleware:', error);
|
||||||
return NextResponse.next();
|
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