session state simplification
This commit is contained in:
@@ -8,7 +8,7 @@ const checkAdmin = query(async (): Promise<boolean> => {
|
||||
const { getUserState } = await import("~/lib/auth-query");
|
||||
const userState = await getUserState();
|
||||
|
||||
if (userState.privilegeLevel !== "admin") {
|
||||
if (!userState.isAdmin) {
|
||||
console.log("redirect");
|
||||
throw redirect("/");
|
||||
}
|
||||
|
||||
@@ -34,7 +34,8 @@ const getPostByTitle = query(
|
||||
const { getFeatureFlags } = await import("~/server/feature-flags");
|
||||
const event = getRequestEvent()!;
|
||||
const userState = await getUserState();
|
||||
const privilegeLevel = userState.privilegeLevel;
|
||||
const isAuthenticated = userState.isAuthenticated;
|
||||
const isAdmin = userState.isAdmin;
|
||||
const userID = userState.userId;
|
||||
const conn = ConnectionFactory();
|
||||
|
||||
@@ -51,7 +52,8 @@ const getPostByTitle = query(
|
||||
tags: [],
|
||||
userCommentArray: [],
|
||||
reactionArray: [],
|
||||
privilegeLevel: "anonymous" as const,
|
||||
isAuthenticated: false,
|
||||
isAdmin: false,
|
||||
userID: null
|
||||
};
|
||||
}
|
||||
@@ -77,13 +79,14 @@ const getPostByTitle = query(
|
||||
tags: [],
|
||||
userCommentArray: [],
|
||||
reactionArray: [],
|
||||
privilegeLevel: "anonymous" as const,
|
||||
isAuthenticated: false,
|
||||
isAdmin: false,
|
||||
userID: null
|
||||
};
|
||||
}
|
||||
|
||||
let query = "SELECT * FROM Post WHERE title = ?";
|
||||
if (privilegeLevel !== "admin") {
|
||||
if (!isAdmin) {
|
||||
query += ` AND published = TRUE`;
|
||||
}
|
||||
|
||||
@@ -110,7 +113,8 @@ const getPostByTitle = query(
|
||||
tags: [],
|
||||
userCommentArray: [],
|
||||
reactionArray: [],
|
||||
privilegeLevel: "anonymous" as const,
|
||||
isAuthenticated: false,
|
||||
isAdmin: false,
|
||||
userID: null
|
||||
};
|
||||
}
|
||||
@@ -123,14 +127,15 @@ const getPostByTitle = query(
|
||||
tags: [],
|
||||
userCommentArray: [],
|
||||
reactionArray: [],
|
||||
privilegeLevel: "anonymous" as const,
|
||||
isAuthenticated: false,
|
||||
isAdmin: false,
|
||||
userID: null
|
||||
};
|
||||
}
|
||||
|
||||
const conditionalContext = {
|
||||
isAuthenticated: userID !== null,
|
||||
privilegeLevel: privilegeLevel,
|
||||
isAdmin: isAdmin,
|
||||
userId: userID,
|
||||
currentDate: new Date(),
|
||||
featureFlags: getFeatureFlags(),
|
||||
@@ -245,7 +250,8 @@ const getPostByTitle = query(
|
||||
topLevelComments,
|
||||
userCommentArray,
|
||||
reactionArray,
|
||||
privilegeLevel,
|
||||
isAuthenticated,
|
||||
isAdmin,
|
||||
userID,
|
||||
sortBy,
|
||||
reads: post.reads || 0
|
||||
@@ -429,7 +435,7 @@ export default function PostPage() {
|
||||
<div>
|
||||
<SessionDependantLike
|
||||
currentUserID={postData.userID}
|
||||
privilegeLevel={postData.privilegeLevel}
|
||||
isAuthenticated={postData.isAuthenticated}
|
||||
likes={postData.likes as any[]}
|
||||
projectID={p().id}
|
||||
/>
|
||||
@@ -455,7 +461,8 @@ export default function PostPage() {
|
||||
class="mx-4 pt-12 pb-12 md:mx-8 lg:mx-12"
|
||||
>
|
||||
<CommentSectionWrapper
|
||||
privilegeLevel={postData.privilegeLevel}
|
||||
isAuthenticated={postData.isAuthenticated}
|
||||
isAdmin={postData.isAdmin}
|
||||
allComments={postData.comments as Comment[]}
|
||||
topLevelComments={
|
||||
postData.topLevelComments as Comment[]
|
||||
|
||||
@@ -13,7 +13,7 @@ const checkAdminAccess = query(async () => {
|
||||
// Reuse shared auth query for consistency
|
||||
const userState = await getUserState();
|
||||
|
||||
if (userState.privilegeLevel !== "admin") {
|
||||
if (!userState.isAdmin) {
|
||||
throw redirect("/401");
|
||||
}
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@ const getPostForEdit = query(async (id: string) => {
|
||||
const { ConnectionFactory } = await import("~/server/utils");
|
||||
const userState = await getUserState();
|
||||
|
||||
if (userState.privilegeLevel !== "admin") {
|
||||
if (!userState.isAdmin) {
|
||||
throw redirect("/401");
|
||||
}
|
||||
|
||||
@@ -35,7 +35,7 @@ const getPostForEdit = query(async (id: string) => {
|
||||
return {
|
||||
post,
|
||||
tags,
|
||||
privilegeLevel: userState.privilegeLevel,
|
||||
isAdmin: userState.isAdmin,
|
||||
userID: userState.userId
|
||||
};
|
||||
}, "post-for-edit");
|
||||
|
||||
@@ -15,10 +15,10 @@ const getPosts = query(async () => {
|
||||
const { ConnectionFactory } = await import("~/server/utils");
|
||||
const { withCacheAndStale } = await import("~/server/cache");
|
||||
const userState = await getUserState();
|
||||
const privilegeLevel = userState.privilegeLevel;
|
||||
const isAdmin = userState.isAdmin;
|
||||
|
||||
return withCacheAndStale(
|
||||
`posts-${privilegeLevel}`,
|
||||
`posts-${isAdmin ? "admin" : "user"}`,
|
||||
CACHE_CONFIG.BLOG_POSTS_LIST_CACHE_TTL_MS,
|
||||
async () => {
|
||||
const conn = ConnectionFactory();
|
||||
@@ -43,7 +43,7 @@ const getPosts = query(async () => {
|
||||
LEFT JOIN Comment c ON p.id = c.post_id
|
||||
`;
|
||||
|
||||
if (privilegeLevel !== "admin") {
|
||||
if (!isAdmin) {
|
||||
postsQuery += ` WHERE p.published = TRUE`;
|
||||
}
|
||||
|
||||
@@ -57,7 +57,7 @@ const getPosts = query(async () => {
|
||||
SELECT t.value, t.post_id
|
||||
FROM Tag t
|
||||
JOIN Post p ON t.post_id = p.id
|
||||
${privilegeLevel !== "admin" ? "WHERE p.published = TRUE" : ""}
|
||||
${!isAdmin ? "WHERE p.published = TRUE" : ""}
|
||||
ORDER BY t.value ASC
|
||||
`;
|
||||
|
||||
@@ -70,7 +70,7 @@ const getPosts = query(async () => {
|
||||
tagMap[key] = (tagMap[key] || 0) + 1;
|
||||
});
|
||||
|
||||
return { posts, tags, tagMap, privilegeLevel };
|
||||
return { posts, tags, tagMap, isAdmin };
|
||||
}
|
||||
);
|
||||
}, "posts");
|
||||
@@ -106,11 +106,11 @@ export default function BlogIndex() {
|
||||
<TagSelector tagMap={loadedData().tagMap} />
|
||||
</Show>
|
||||
|
||||
<Show when={loadedData().privilegeLevel === "admin"}>
|
||||
<Show when={loadedData().isAdmin}>
|
||||
<PublishStatusToggle />
|
||||
</Show>
|
||||
|
||||
<Show when={loadedData().privilegeLevel === "admin"}>
|
||||
<Show when={loadedData().isAdmin}>
|
||||
<div class="mt-2 flex justify-center md:mt-0 md:justify-end">
|
||||
<A
|
||||
href="/blog/create"
|
||||
@@ -130,7 +130,7 @@ export default function BlogIndex() {
|
||||
<PostSorting
|
||||
posts={loadedData().posts}
|
||||
tags={loadedData().tags}
|
||||
privilegeLevel={loadedData().privilegeLevel}
|
||||
isAdmin={loadedData().isAdmin}
|
||||
filters={filters()}
|
||||
sort={sort()}
|
||||
include={include()}
|
||||
|
||||
@@ -7,7 +7,7 @@ import { getUserState } from "~/lib/auth-query";
|
||||
const checkAdminAccess = query(async () => {
|
||||
"use server";
|
||||
const userState = await getUserState();
|
||||
return { privilegeLevel: userState.privilegeLevel };
|
||||
return { isAdmin: userState.isAdmin };
|
||||
}, "test-auth-state");
|
||||
|
||||
type EndpointTest = {
|
||||
@@ -916,7 +916,7 @@ export default function TestPage() {
|
||||
description="tRPC API testing dashboard for developers to test endpoints and verify functionality."
|
||||
/>
|
||||
<Show
|
||||
when={authState()?.privilegeLevel === "admin"}
|
||||
when={authState()?.isAdmin}
|
||||
fallback={
|
||||
<div class="w-full pt-[30vh] text-center">
|
||||
<div class="text-text text-2xl">Unauthorized</div>
|
||||
|
||||
Reference in New Issue
Block a user