Skip to content

Commit

Permalink
Merge pull request #476 from s-hirano-ist/fix--500-error-on-generate-…
Browse files Browse the repository at this point in the history
…static-params

fix: 500 error on generate static params
  • Loading branch information
s-hirano-ist authored Oct 13, 2024
2 parents a5fec39 + b0f12ff commit 411c29a
Show file tree
Hide file tree
Showing 11 changed files with 149 additions and 119 deletions.
6 changes: 3 additions & 3 deletions src/app/(contents)/books/[slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ export default async function Page({ params }: Props) {
);
}

export function generateStaticParams() {
return getAllSlugs(path);
}
// export function generateStaticParams() {
// return getAllSlugs(path);
// }
6 changes: 3 additions & 3 deletions src/app/(contents)/notes/[slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ export default async function Page({ params }: Props) {
);
}

export function generateStaticParams() {
return getAllSlugs(path);
}
// export function generateStaticParams() {
// return getAllSlugs(path);
// }
76 changes: 6 additions & 70 deletions src/app/dumper/@dump/page.tsx
Original file line number Diff line number Diff line change
@@ -1,73 +1,9 @@
"use client";
import { Button } from "@/components/ui/button";
import { changeContentsStatus } from "@/features/update-status/actions/change-contents-status";
import { changeNewsStatus } from "@/features/update-status/actions/change-news-status";
import { useToast } from "@/hooks/use-toast";
import { useState } from "react";
import { Unauthorized } from "@/components/unauthorized";
import { checkAdminPermission } from "@/features/auth/utils/role";
import { ChangeStatusButtons } from "@/features/dump/components/change-status-buttons";

export default function Page() {
const { toast } = useToast();
export default async function Page() {
const hasAdminPermission = await checkAdminPermission();

const [buttonDisabled, setButtonDisabled] = useState(false);

const handleNewsUpdateStatus = async () => {
setButtonDisabled(true);
const response = await changeNewsStatus("UPDATE");
// TODO: revalidate path
toast({
variant: response.success ? "default" : "destructive",
description: response.message,
});
setButtonDisabled(false);
};

const handleNewsRevertStatus = async () => {
setButtonDisabled(true);
const response = await changeNewsStatus("REVERT");
// TODO: revalidate path
toast({
variant: response.success ? "default" : "destructive",
description: response.message,
});
setButtonDisabled(false);
};

const handleContentsUpdateStatus = async () => {
setButtonDisabled(true);
const response = await changeContentsStatus("UPDATE");
// TODO: revalidate path
toast({
variant: response.success ? "default" : "destructive",
description: response.message,
});
setButtonDisabled(false);
};

const handleContentsRevertStatus = async () => {
setButtonDisabled(true);
const response = await changeContentsStatus("REVERT");
// TODO: revalidate path
toast({
variant: response.success ? "default" : "destructive",
description: response.message,
});
setButtonDisabled(false);
};

return (
<div className="grid grid-cols-2 gap-4 p-4">
<Button onClick={handleNewsUpdateStatus} disabled={buttonDisabled}>
NEWS UPDATE
</Button>
<Button onClick={handleNewsRevertStatus} disabled={buttonDisabled}>
NEWS REVERT
</Button>
<Button onClick={handleContentsUpdateStatus} disabled={buttonDisabled}>
CONTENTS UPDATE
</Button>
<Button onClick={handleContentsRevertStatus} disabled={buttonDisabled}>
CONTENTS REVERT
</Button>
</div>
);
return <>{hasAdminPermission ? <ChangeStatusButtons /> : <Unauthorized />}</>;
}
44 changes: 3 additions & 41 deletions src/features/auth/utils/auth.config.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,7 @@
import { env } from "@/env.mjs";
import { signInSchema } from "@/features/auth/schemas/sign-in-schema";
import prisma from "@/prisma";
import bcrypt from "bcryptjs";
import type { NextAuthConfig } from "next-auth";
import Credentials from "next-auth/providers/credentials";
// import GitHubProvider from "next-auth/providers/github";

// TODO: https://qiita.com/daiki7010/items/b15de9ef747f5b23c984
export const authConfig: NextAuthConfig = {
secret: env.AUTH_SECRET,
providers: [
// MEMO: for manual auth
Credentials({
async authorize(credentials) {
const parsedCredentials = signInSchema.safeParse(credentials);
if (parsedCredentials.success) {
const { username, password } = parsedCredentials.data;

const user = await prisma.users.findUnique({
where: { username },
// MEMO: only allowed to select password here (for auth). See `src/prisma.ts` for more.
select: {
id: true,
username: true,
role: true,
password: true,
},
});
if (!user) return null;
const passwordMatch = await bcrypt.compare(password, user.password);
if (!passwordMatch) return null;

return { id: user.id, role: user.role, username: user.username };
}
return null;
},
}),
// MEMO: for GitHub Provider
// GitHubProvider({
// clientId: env.GITHUB_CLIENT_ID,
// clientSecret: env.GITHUB_CLIENT_SECRET,
// }),
],
};
export default {
providers: [Credentials /*GitHubProvider*/],
} satisfies NextAuthConfig;
43 changes: 42 additions & 1 deletion src/features/auth/utils/auth.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { env } from "@/env.mjs";
import { signInSchema } from "@/features/auth/schemas/sign-in-schema";
import prisma from "@/prisma";
import type { Role } from "@prisma/client";
import bcrypt from "bcryptjs";
import NextAuth, { type DefaultSession } from "next-auth";
import { authConfig } from "./auth.config";
import Credentials from "next-auth/providers/credentials";
import authConfig from "./auth.config";

declare module "next-auth" {
// eslint-disable-next-line
Expand All @@ -18,13 +23,49 @@ declare module "next-auth" {
}
}

// TODO: https://qiita.com/daiki7010/items/b15de9ef747f5b23c984

export const {
auth,
signIn,
signOut,
handlers: { GET, POST },
} = NextAuth({
...authConfig,
secret: env.AUTH_SECRET,
providers: [
// MEMO: for manual auth
Credentials({
async authorize(credentials) {
const parsedCredentials = signInSchema.safeParse(credentials);
if (parsedCredentials.success) {
const { username, password } = parsedCredentials.data;

const user = await prisma.users.findUnique({
where: { username },
// MEMO: only allowed to select password here (for auth). See `src/prisma.ts` for more.
select: {
id: true,
username: true,
role: true,
password: true,
},
});
if (!user) return null;
const passwordMatch = await bcrypt.compare(password, user.password);
if (!passwordMatch) return null;

return { id: user.id, role: user.role, username: user.username };
}
return null;
},
}),
// MEMO: for GitHub Provider
// GitHubProvider({
// clientId: env.GITHUB_CLIENT_ID,
// clientSecret: env.GITHUB_CLIENT_SECRET,
// }),
],
session: { strategy: "jwt" },
callbacks: {
jwt({ token, user }) {
Expand Down
6 changes: 5 additions & 1 deletion src/features/auth/utils/get-session.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
"use server";
import "server-only";
import { UnauthorizedError } from "@/error";
// import { redirect } from "next/navigation";
import { auth } from "./auth";

export async function checkSelfAuth() {
const session = await auth();
if (!session) throw new UnauthorizedError();
if (!session) {
throw new UnauthorizedError();
// redirect("/auth"); // WHEN MIDDLEWARE DO NOT WORK
}
return session;
}

Expand Down
6 changes: 6 additions & 0 deletions src/features/auth/utils/role.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export async function checkAdminPermission() {
return false;
case "VIEWER":
return false;
case "UNAUTHORIZED":
return false;
default:
throw new UnexpectedError();
}
Expand Down Expand Up @@ -49,6 +51,8 @@ export async function checkPostPermission() {
return true;
case "VIEWER":
return false;
case "UNAUTHORIZED":
return false;
default:
throw new UnexpectedError();
}
Expand All @@ -65,6 +69,8 @@ export async function checkUpdateStatusPermission() {
return true;
case "VIEWER":
return false;
case "UNAUTHORIZED":
return false;
default:
throw new UnexpectedError();
}
Expand Down
1 change: 1 addition & 0 deletions src/features/contents/components/contents-body.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"use client";
type Props = {
content: JSX.Element | JSX.Element[] | string;
};
Expand Down
1 change: 1 addition & 0 deletions src/features/contents/components/contents-stack.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"use client";
import { ContentsPreview } from "./contents-preview";

type Props = { path: string; data: Record<string, string> };
Expand Down
73 changes: 73 additions & 0 deletions src/features/dump/components/change-status-buttons.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
"use client";
import { Button } from "@/components/ui/button";
import { changeContentsStatus } from "@/features/update-status/actions/change-contents-status";
import { changeNewsStatus } from "@/features/update-status/actions/change-news-status";
import { useToast } from "@/hooks/use-toast";
import { useState } from "react";

export function ChangeStatusButtons() {
const { toast } = useToast();

const [buttonDisabled, setButtonDisabled] = useState(false);

const handleNewsUpdateStatus = async () => {
setButtonDisabled(true);
const response = await changeNewsStatus("UPDATE");
// TODO: revalidate path
toast({
variant: response.success ? "default" : "destructive",
description: response.message,
});
setButtonDisabled(false);
};

const handleNewsRevertStatus = async () => {
setButtonDisabled(true);
const response = await changeNewsStatus("REVERT");
// TODO: revalidate path
toast({
variant: response.success ? "default" : "destructive",
description: response.message,
});
setButtonDisabled(false);
};

const handleContentsUpdateStatus = async () => {
setButtonDisabled(true);
const response = await changeContentsStatus("UPDATE");
// TODO: revalidate path
toast({
variant: response.success ? "default" : "destructive",
description: response.message,
});
setButtonDisabled(false);
};

const handleContentsRevertStatus = async () => {
setButtonDisabled(true);
const response = await changeContentsStatus("REVERT");
// TODO: revalidate path
toast({
variant: response.success ? "default" : "destructive",
description: response.message,
});
setButtonDisabled(false);
};

return (
<div className="grid grid-cols-2 gap-4 p-4">
<Button onClick={handleNewsUpdateStatus} disabled={buttonDisabled}>
NEWS UPDATE
</Button>
<Button onClick={handleNewsRevertStatus} disabled={buttonDisabled}>
NEWS REVERT
</Button>
<Button onClick={handleContentsUpdateStatus} disabled={buttonDisabled}>
CONTENTS UPDATE
</Button>
<Button onClick={handleContentsRevertStatus} disabled={buttonDisabled}>
CONTENTS REVERT
</Button>
</div>
);
}
6 changes: 6 additions & 0 deletions src/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { auth } from "@/features/auth/utils/auth";
import { DEFAULT_SIGN_IN_REDIRECT } from "./constants";

// FIXME: issue with edge compatibility
// https://authjs.dev/guides/edge-compatibility
// import authConfig from "@/features/auth/utils/auth.config";
// import NextAuth from "next-auth";
// export const { auth: middleware } = NextAuth(authConfig);

// MEMO: アクセスが禁止されているパスではなく、アクセスが許可されているパスを記述するべき。なぜなら、アクセスが禁止されているパスのすべてを把握するのは難しいからである。
const publicRoutes: string[] = [];

Expand Down

0 comments on commit 411c29a

Please sign in to comment.