Skip to content

Commit

Permalink
Hide sign out when proxy user
Browse files Browse the repository at this point in the history
  • Loading branch information
albinmedoc committed Dec 4, 2024
1 parent 3bed34c commit 6ed7fcd
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 27 deletions.
1 change: 1 addition & 0 deletions src/app.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ declare global {
// Locals must be an interface and not a type
interface Locals {
user: import("lucia").User | null;
isProxyUser: boolean;
session: import("lucia").Session | null;
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/hooks.server.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { env } from "$env/dynamic/private";
import { auth } from "$lib/server/auth";
import type { Handle } from "@sveltejs/kit";

Expand All @@ -24,6 +25,11 @@ export const handle: Handle = async ({ event, resolve }) => {
...sessionCookie.attributes
});
}
const isProxyUser = (env.HEADER_AUTH_ENABLED ?? "false") == "true" &&
!!env.HEADER_USERNAME &&
!!event.request.headers.get(env.HEADER_USERNAME);

event.locals.isProxyUser = isProxyUser;
event.locals.user = user;
event.locals.session = session;
return resolve(event);
Expand Down
5 changes: 3 additions & 2 deletions src/lib/components/navigation/NavBar.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
interface Props {
navItems: NavItem[];
user: User | null;
isProxyUser: boolean;
}
let { navItems, user }: Props = $props();
let { navItems, user, isProxyUser }: Props = $props();
const drawerStore = getDrawerStore();
const drawerSettings: DrawerSettings = {
Expand Down Expand Up @@ -61,6 +62,6 @@
{/if}

{#snippet trail()}
<NavMenu {user} />
<NavMenu {isProxyUser} {user} />
{/snippet}
</AppBar>
32 changes: 17 additions & 15 deletions src/lib/components/navigation/NavMenu/NavMenu.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
interface Props {
user: User | null;
isProxyUser: boolean;
}
let { user }: Props = $props();
let { user, isProxyUser }: Props = $props();
const menuSettings: PopupSettings = {
event: "click",
Expand Down Expand Up @@ -43,20 +44,21 @@

<hr />
<GroupSubMenu {user} />
<hr />

<li>
<button
class="list-option w-full"
onclick={async () => {
await fetch("/logout", { method: "POST" });
invalidateAll();
}}
>
<iconify-icon icon="ion:log-out"></iconify-icon>
<p>Sign Out</p>
</button>
</li>
{#if !isProxyUser}
<hr />
<li>
<button
class="list-option w-full"
onclick={async () => {
await fetch("/logout", { method: "POST" });
invalidateAll();
}}
>
<iconify-icon icon="ion:log-out"></iconify-icon>
<p>Sign Out</p>
</button>
</li>
{/if}
<hr class="pb-1" />
<li>
<div class="flex w-full justify-around">
Expand Down
2 changes: 1 addition & 1 deletion src/routes/+layout.server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { LayoutServerLoad } from "./$types";

export const load = (async ({ locals }) => {
return { user: locals.user };
return { user: locals.user, isProxyUser: locals.isProxyUser };
}) satisfies LayoutServerLoad;
2 changes: 1 addition & 1 deletion src/routes/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@
{#if showNavigationLoadingBar}
<NavigationLoadingBar />
{/if}
<NavBar {navItems} user={data.user} />
<NavBar isProxyUser={data.isProxyUser} {navItems} user={data.user} />
</header>

<main id="main" class="h-full min-h-screen px-4 py-4 md:px-12 lg:px-32 xl:px-56">
Expand Down
10 changes: 2 additions & 8 deletions src/routes/account/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { env } from "$env/dynamic/private";
import { auth } from "$lib/server/auth";
import { client } from "$lib/server/prisma";
import { getResetPasswordSchema } from "$lib/validations";
Expand All @@ -9,20 +8,15 @@ import type { PrismaClientKnownRequestError } from "@prisma/client/runtime/libra
import { createImage, tryDeleteImage } from "$lib/server/image-util";
import { LegacyScrypt } from "lucia";

export const load: PageServerLoad = async ({ locals, request }) => {
export const load: PageServerLoad = async ({ locals }) => {
const user = locals.user;
if (!user) {
redirect(302, `/login?ref=/account`);
}

const isProxyUser =
(env.HEADER_AUTH_ENABLED ?? "false") == "true" &&
!!env.HEADER_USERNAME &&
!!request.headers.get(env.HEADER_USERNAME);

return {
user,
isProxyUser
isProxyUser: locals.isProxyUser
};
};

Expand Down

0 comments on commit 6ed7fcd

Please sign in to comment.