Skip to content

Connect, collaborate, and grow with a community of TypeScript developers

uh oh

Chris Bautistaa84ede048615
diff --git a/src/pages/[username].tsx b/src/app/[username].tsx
similarity index 100%
rename from src/pages/[username].tsx
rename to src/app/[username].tsx
diff --git a/src/app/api/auth/[...nextauth]/route.ts b/src/app/api/auth/[...nextauth]/route.ts
new file mode 100644
index 00000000..72fec3b8
--- /dev/null
+++ b/src/app/api/auth/[...nextauth]/route.ts
@@ -0,0 +1,7 @@
+import NextAuth from 'next-auth';
+import { authOptions } from '~/server/auth';
+
+/* eslint-disable-next-line */
+const handler = NextAuth(authOptions);
+
+export { handler as GET, handler as POST };
diff --git a/src/app/layout.tsx b/src/app/layout.tsx
index da476f47..5bd7cfb3 100644
--- a/src/app/layout.tsx
+++ b/src/app/layout.tsx
@@ -2,6 +2,9 @@ import { Providers } from './providers';
import '../styles/globals.css';
+import { Inter } from 'next/font/google';
+
+const inter = Inter({ subsets: ['latin'] });
export const metadata = {
title: 'Next.js',
description: 'Generated by Next.js',
@@ -12,7 +15,9 @@ export default function RootLayout({ children }: { children: React.ReactNode })
<html suppressHydrationWarning>
<head />
<body>
- <Providers>{children}</Providers>
+ <Providers>
+ <main className={`${inter.className} bg-white dark:bg-neutral-900`}>{children}</main>
+ </Providers>
</body>
</html>
);
diff --git a/src/app/page.tsx b/src/app/page.tsx
index 954ec61b..6d1e87bd 100644
--- a/src/app/page.tsx
+++ b/src/app/page.tsx
@@ -1,12 +1,17 @@
import { getServerSession } from 'next-auth';
import Head from 'next/head';
+import { redirect } from 'next/navigation';
import { CodePanel } from '~/components/ui/editor';
import { TypographyH1 } from '~/components/ui/h1';
import { Navigation } from '~/components/ui/navigation';
+import { SignInOutButton } from '~/components/ui/signInOutButton';
import { authOptions } from '~/server/auth';
export default async function Index() {
const session = await getServerSession(authOptions);
+ if (!session) {
+ redirect('/api/auth/signin');
+ }
return (
<>
<Head>
@@ -27,6 +32,7 @@ export default async function Index() {
{session && <span>Logged in as {session.user?.name}</span>}
</p>
</div>
+ <SignInOutButton />
<CodePanel />
</main>
</>
diff --git a/src/app/providers.tsx b/src/app/providers.tsx
index eac070f3..1b6e9a09 100644
--- a/src/app/providers.tsx
+++ b/src/app/providers.tsx
@@ -1,10 +1,15 @@
'use client';
+import { SessionProvider } from 'next-auth/react';
import { ThemeProvider } from 'next-themes';
interface Props {
children: React.ReactNode;
}
export function Providers({ children }: Props) {
- return <ThemeProvider attribute="class">{children}</ThemeProvider>;
+ return (
+ <SessionProvider>
+ <ThemeProvider attribute="class">{children}</ThemeProvider>;
+ </SessionProvider>
+ );
}
diff --git a/src/components/ui/editor/index.tsx b/src/components/ui/editor/index.tsx
index 7df53231..a28906a5 100644
--- a/src/components/ui/editor/index.tsx
+++ b/src/components/ui/editor/index.tsx
@@ -57,43 +57,6 @@ export function equal<A, B>(...args: Alike<A, B> extends true ? [] : [a: A, b: B
const libUri = 'ts:filename/checking.d.ts';
export const CodePanel = () => {
const onMount = async (editor: monaco.editor.IStandaloneCodeEditor, monaco: Monaco) => {
- // const checkErrorsCmd = editor.addCommand(
- // 0,
- // function () {
- // if (runCommand) {
- // runCommand();
- // } else {
- // alert('still loading');
- // }
- // },
- // 'checkErrorsCmd',
- // );
- // monaco.languages.registerCodeLensProvider('typescript', {
- // provideCodeLenses: function (model, token) {
- // return {
- // lenses: [
- // {
- // range: {
- // startLineNumber: 1,
- // startColumn: 1,
- // endLineNumber: 2,
- // endColumn: 1,
- // },
- // id: 'Log TypeScript Errors to Console',
- // command: {
- // id: checkErrorsCmd,
- // title: 'Log TypeScript Errors to Console',
- // },
- // },
- // ],
- // dispose: () => {},
- // };
- // },
- // resolveCodeLens: function (model, codeLens, token) {
- // return codeLens;
- // },
- // });
-
monaco.languages.typescript.javascriptDefaults.addExtraLib(libSource, libUri);
monaco.editor.createModel(libSource, 'typescript', monaco.Uri.parse(libUri));
diff --git a/src/components/ui/signInOutButton.tsx b/src/components/ui/signInOutButton.tsx
new file mode 100644
index 00000000..557810ed
--- /dev/null
+++ b/src/components/ui/signInOutButton.tsx
@@ -0,0 +1,15 @@
+'use client';
+
+import { signIn, signOut, useSession } from 'next-auth/react';
+
+export function SignInOutButton() {
+ const session = useSession();
+ return (
+ <button
+ className="rounded-full bg-white/10 px-10 py-3 font-semibold text-white no-underline transition hover:bg-white/20"
+ onClick={session ? () => void signOut() : () => void signIn()}
+ >
+ {session ? 'Sign out' : 'Sign in'}
+ </button>
+ );
+}
diff --git a/src/middleware.ts b/src/middleware.ts
new file mode 100644
index 00000000..a4317c6f
--- /dev/null
+++ b/src/middleware.ts
@@ -0,0 +1,43 @@
+import { getToken } from 'next-auth/jwt';
+import { withAuth } from 'next-auth/middleware';
+import { NextResponse } from 'next/server';
+
+export default withAuth(
+ async function middleware(req) {
+ const token = await getToken({ req });
+ const isAuth = !!token;
+ const isAuthPage =
+ req.nextUrl.pathname.startsWith('/login') || req.nextUrl.pathname.startsWith('/register');
+
+ if (isAuthPage) {
+ if (isAuth) {
+ return NextResponse.redirect(new URL('/', req.url));
+ }
+
+ return null;
+ }
+
+ if (!isAuth) {
+ let from = req.nextUrl.pathname;
+ if (req.nextUrl.search) {
+ from += req.nextUrl.search;
+ }
+
+ return NextResponse.redirect(new URL(`/login?from=${encodeURIComponent(from)}`, req.url));
+ }
+ },
+ {
+ callbacks: {
+ async authorized() {
+ // This is a work-around for handling redirect on auth pages.
+ // We return true here so that the middleware function above
+ // is always called.
+ return true;
+ },
+ },
+ },
+);
+
+export const config = {
+ matcher: ['/[username]', '/login', '/register'],
+};
diff --git a/src/pages/_app.tsx b/src/pages/_app.tsx
deleted file mode 100644
index 3531238f..00000000
--- a/src/pages/_app.tsx
+++ /dev/null
@@ -1,31 +0,0 @@
-import { type Session } from 'next-auth';
-
-import { SessionProvider } from 'next-auth/react';
-
-import { ThemeProvider } from 'next-themes';
-
-import { Inter } from 'next/font/google';
-
-import { type AppType } from 'next/app';
-import '~/styles/globals.css';
-import { api } from '~/utils/api';
-
-// If loading a variable font, you don't need to specify the font weight
-const inter = Inter({ subsets: ['latin'] });
-
-const MyApp: AppType<{ session: Session | null }> = ({
- Component,
- pageProps: { session, ...pageProps },
-}) => {
- return (
- <ThemeProvider attribute="class">
- <SessionProvider session={session}>
- <main className={`${inter.className} bg-white dark:bg-neutral-900`}>
- <Component {...pageProps} />
- </main>
- </SessionProvider>
- </ThemeProvider>
- );
-};
-
-export default api.withTRPC(MyApp);
diff --git a/src/pages/api/auth/[...nextauth].ts b/src/pages/api/auth/[...nextauth].ts
deleted file mode 100644
index 9dabfb5a..00000000
--- a/src/pages/api/auth/[...nextauth].ts
+++ /dev/null
@@ -1,4 +0,0 @@
-import NextAuth from 'next-auth';
-import { authOptions } from '~/server/auth';
-
-export default NextAuth(authOptions);
diff --git a/src/pages/api/trpc/[trpc].ts b/src/pages/api/trpc/[trpc].ts
deleted file mode 100644
index dc36ccf7..00000000
--- a/src/pages/api/trpc/[trpc].ts
+++ /dev/null
@@ -1,18 +0,0 @@
-import { createNextApiHandler } from "@trpc/server/adapters/next";
-import { env } from "~/env.mjs";
-import { appRouter } from "~/server/api/root";
-import { createTRPCContext } from "~/server/api/trpc";
-
-// export API handler
-export default createNextApiHandler({
- router: appRouter,
- createContext: createTRPCContext,
- onError:
- env.NODE_ENV === "development"
- ? ({ path, error }) => {
- console.error(
- `❌ tRPC failed on ${path ?? "<no-path>"}: ${error.message}`,
- );
- }
- : undefined,
-});
diff --git a/src/pages/home/index.tsx b/src/pages/home/index.tsx
deleted file mode 100644
index 73cbf7be..00000000
--- a/src/pages/home/index.tsx
+++ /dev/null
@@ -1,8 +0,0 @@
-import Home from '~/components/home';
-
-// export const getServerSideProps = compose(authenticatedRoute, async (ctx) => {
-// });
-
-const HomePage = () => <Home />;
-
-export default HomePage;