diff --git a/apps/nextjs/src/app/workspace/[workspaceId]/layout.tsx b/apps/nextjs/src/app/workspace/[workspaceId]/layout.tsx index 6626f581..aa4bcd5c 100644 --- a/apps/nextjs/src/app/workspace/[workspaceId]/layout.tsx +++ b/apps/nextjs/src/app/workspace/[workspaceId]/layout.tsx @@ -1,23 +1,12 @@ -import { redirect } from "next/navigation"; - -import { auth } from "@acme/auth"; - -import routes from "~/app/_lib/routes"; import { WeekList } from "./_components/WeekList"; import WorkspaceHeader from "./_components/WorkspaceHeader"; import { WeekendProvider } from "./providers"; -export default async function WorkspaceLayout({ +export default function WorkspaceLayout({ children, }: { children: React.ReactNode; }) { - const session = await auth(); - - if (!session) { - redirect(routes.home); - } - return (
diff --git a/apps/nextjs/src/app/workspaces/layout.tsx b/apps/nextjs/src/app/workspaces/layout.tsx index 3a24e743..fe38da7f 100644 --- a/apps/nextjs/src/app/workspaces/layout.tsx +++ b/apps/nextjs/src/app/workspaces/layout.tsx @@ -1,22 +1,11 @@ -import { redirect } from "next/navigation"; - -import { auth } from "@acme/auth"; - import Heading from "../_components/heading"; -import routes from "../_lib/routes"; import WorkspaceModal from "./_components/WorkspaceModal"; -export default async function WorkspaceLayout({ +export default function WorkspaceLayout({ children, }: { children: React.ReactNode; }) { - const session = await auth(); - - if (!session) { - redirect(routes.home); - } - return (
diff --git a/apps/nextjs/src/middleware.ts b/apps/nextjs/src/middleware.ts new file mode 100644 index 00000000..0090849e --- /dev/null +++ b/apps/nextjs/src/middleware.ts @@ -0,0 +1,48 @@ +import { NextResponse } from "next/server"; + +import { auth } from "@acme/auth"; + +export const AUTH_PAGES = ["/sign-in"]; + +const isAuthPages = (url: string) => + AUTH_PAGES.some((page) => page.startsWith(url)); + +export default auth(async (req) => { + const { url, nextUrl } = req; + // const { value: token } = cookies.get("token") ?? { value: null }; + + const session = await auth(); + + // const hasVerifiedToken = token && (await verifyJwtToken(token)); + const isAuthPageRequested = isAuthPages(nextUrl.pathname); + + if (isAuthPageRequested && !session?.user) { + const response = NextResponse.next(); + response.cookies.delete("token"); + return response; + } + + if (isAuthPageRequested) { + const response = NextResponse.redirect(new URL(`/workspaces`, url)); + return response; + } + + if (!session?.user) { + const searchParams = new URLSearchParams(nextUrl.searchParams); + searchParams.set("next", nextUrl.pathname); + + const response = NextResponse.redirect(new URL(`/?${searchParams}`, url)); + + response.cookies.delete("token"); + + return response; + } + + return NextResponse.next(); +}); + +// Optionally, don't invoke Middleware on some paths +// Read more: https://nextjs.org/docs/app/building-your-application/routing/middleware#matcher +export const config = { + matcher: ["/workspaces", "/workspace/:path*"], +}; diff --git a/packages/auth/index.ts b/packages/auth/index.ts index 42e76f59..b488983c 100644 --- a/packages/auth/index.ts +++ b/packages/auth/index.ts @@ -1,3 +1,4 @@ +import Credentials from "@auth/core/providers/credentials"; import Github from "@auth/core/providers/github"; // import Google from "@auth/core/providers/google"; import type { DefaultSession } from "@auth/core/types"; @@ -37,6 +38,39 @@ export const { clientId: env.GITHUB_CLIENT_ID, clientSecret: env.GITHUB_CLIENT_SECRET, }), + Credentials({ + name: "Credentials", + credentials: { + email: { + label: "Email: ", + type: "text", + }, + password: { + label: "Password: ", + type: "password", + }, + }, + async authorize(credentials) { + // This is where you need to retrieve user data + // to verify with credentials + + const user = { + id: "1", + name: "J Smith", + email: "j@j.com", + password: "123", + }; + + if ( + credentials.email === user.email && + credentials.password === user.password + ) { + return user; + } + + return null; + }, + }), ], callbacks: { session: ({ session, user }) => ({