From 22eee04fde4796a5915e156715941dbf819ae4ad Mon Sep 17 00:00:00 2001 From: Ahmed Elsakaan Date: Sat, 22 Jul 2023 14:45:48 +0100 Subject: [PATCH] feat: replaces next-auth with clerk & adds privacy policy (#260) --- .env.example | 14 +- CONTRIBUTING.md | 1 - apps/web/package.json | 4 +- apps/web/src/components/Greeting.tsx | 12 - apps/web/src/middleware.ts | 19 +- apps/web/src/pages/_app.tsx | 6 +- apps/web/src/pages/api/auth/[...nextauth].ts | 5 - apps/web/src/pages/app/index.tsx | 14 + apps/web/src/pages/privacy.tsx | 23 + apps/web/src/pages/sign-in/[[...index]].tsx | 36 + apps/web/src/pages/sign-up/[[...index]].tsx | 36 + apps/web/src/utils/NextPageWithLayout.ts | 4 +- packages/api/package.json | 2 +- packages/api/src/middlewares/auth.ts | 4 +- packages/api/src/root.ts | 4 - packages/api/src/routers/greeting/index.ts | 17 - packages/api/src/routers/user/index.ts | 12 - packages/api/src/setup/context.ts | 15 +- packages/auth/package.json | 30 - packages/auth/src/index.ts | 87 --- packages/auth/tsconfig.build.json | 9 - packages/auth/tsconfig.json | 7 - packages/db/prisma/schema.prisma | 44 -- packages/env/src/index.ts | 29 +- pnpm-lock.yaml | 689 ++++++++++--------- 25 files changed, 510 insertions(+), 613 deletions(-) delete mode 100644 apps/web/src/components/Greeting.tsx delete mode 100644 apps/web/src/pages/api/auth/[...nextauth].ts create mode 100644 apps/web/src/pages/app/index.tsx create mode 100644 apps/web/src/pages/privacy.tsx create mode 100644 apps/web/src/pages/sign-in/[[...index]].tsx create mode 100644 apps/web/src/pages/sign-up/[[...index]].tsx delete mode 100644 packages/api/src/routers/greeting/index.ts delete mode 100644 packages/api/src/routers/user/index.ts delete mode 100644 packages/auth/package.json delete mode 100644 packages/auth/src/index.ts delete mode 100644 packages/auth/tsconfig.build.json delete mode 100644 packages/auth/tsconfig.json diff --git a/.env.example b/.env.example index 88e648d9..7cb5e280 100644 --- a/.env.example +++ b/.env.example @@ -8,10 +8,10 @@ DATABASE_URL="postgres://username:password@localhost:5432/db_name" UPSTASH_REDIS_REST_URL="" UPSTASH_REDIS_REST_TOKEN="" -# Next Auth -NEXTAUTH_URL="http://localhost:3000" -NEXTAUTH_SECRET="secretsecret" - -# Github Auth -GITHUB_CLIENT_ID="" -GITHUB_CLIENT_SECRET="" \ No newline at end of file +# Clerk authentication +NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY= +CLERK_SECRET_KEY= +NEXT_PUBLIC_CLERK_SIGN_IN_URL=/sign-in +NEXT_PUBLIC_CLERK_SIGN_UP_URL=/sign-up +NEXT_PUBLIC_CLERK_AFTER_SIGN_IN_URL=/app +NEXT_PUBLIC_CLERK_AFTER_SIGN_UP_URL=/app \ No newline at end of file diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index d55e5d6f..5a31d3cd 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -64,7 +64,6 @@ The actual stack: - [TailwindCSS](https://tailwindcss.com/) - [tRPC](https://trpc.io) - [Prisma](https://www.prisma.io/) -- [Next-auth](https://next-auth.js.org/) Development stuff: diff --git a/apps/web/package.json b/apps/web/package.json index 264c9290..d974831f 100644 --- a/apps/web/package.json +++ b/apps/web/package.json @@ -15,8 +15,9 @@ "typecheck": "tsc --noEmit" }, "dependencies": { + "@clerk/nextjs": "^4.23.0", + "@clerk/themes": "^1.7.5", "@noodle/api": "workspace:^", - "@noodle/auth": "workspace:^", "@noodle/db": "workspace:^", "@noodle/editor": "workspace:^", "@noodle/env": "workspace:^", @@ -34,7 +35,6 @@ "@vercel/analytics": "^1.0.1", "lucide-react": "^0.263.0", "next": "13.4.12", - "next-auth": "^4.22.3", "next-seo": "^6.1.0", "next-themes": "^0.2.1", "react": "18.2.0", diff --git a/apps/web/src/components/Greeting.tsx b/apps/web/src/components/Greeting.tsx deleted file mode 100644 index 9d1ffe79..00000000 --- a/apps/web/src/components/Greeting.tsx +++ /dev/null @@ -1,12 +0,0 @@ -import { api } from '../utils/api'; - -export const Greeting = () => { - const { data, isLoading, error } = api.greeting.hello.useQuery({ - name: 'John Doe', - }); - - if (isLoading) return

Loading...

; - if (error) return

{error.message}

; - - return

{data.message}

; -}; diff --git a/apps/web/src/middleware.ts b/apps/web/src/middleware.ts index 6d4f3391..e7e62bdb 100644 --- a/apps/web/src/middleware.ts +++ b/apps/web/src/middleware.ts @@ -1,4 +1,5 @@ import type { NextRequest } from 'next/server'; +import { authMiddleware } from '@clerk/nextjs'; import { Ratelimit } from '@upstash/ratelimit'; import { Redis } from '@upstash/redis'; import { NextResponse } from 'next/server'; @@ -15,7 +16,7 @@ const ratelimit = new Ratelimit({ limiter: Ratelimit.slidingWindow(5, '10 s'), }); -export default async function middleware( +async function rateLimitMiddleware( request: NextRequest, ): Promise { const ip = request.ip ?? '127.0.0.1'; @@ -25,6 +26,20 @@ export default async function middleware( : NextResponse.redirect(new URL('/blocked', request.url)); } +export default authMiddleware({ + beforeAuth: (req) => { + return rateLimitMiddleware(req); + }, + publicRoutes: [ + '/', + '/blocked', + '/waitlist', + '/editor', + '/privacy', + '/(api|trpc)(.*)', + ], +}); + export const config = { - matcher: '/api/:path*', + matcher: ['/((?!.*\\..*|_next).*)', '/', '/(api|trpc)(.*)'], }; diff --git a/apps/web/src/pages/_app.tsx b/apps/web/src/pages/_app.tsx index 4a17854d..b6edd523 100644 --- a/apps/web/src/pages/_app.tsx +++ b/apps/web/src/pages/_app.tsx @@ -1,9 +1,9 @@ import '../styles/globals.css'; import { Provider as WrapBalancerProvider } from 'react-wrap-balancer'; +import { ClerkProvider } from '@clerk/nextjs'; import { ReactQueryDevtools } from '@tanstack/react-query-devtools'; import { Analytics } from '@vercel/analytics/react'; -import { SessionProvider } from 'next-auth/react'; import { DefaultSeo } from 'next-seo'; import { ThemeProvider } from 'next-themes'; import { Inter, JetBrains_Mono } from 'next/font/google'; @@ -32,7 +32,7 @@ const App = ({ Component, pageProps }: AppPropsWithLayout) => { return (
- + {getLayout()} @@ -40,7 +40,7 @@ const App = ({ Component, pageProps }: AppPropsWithLayout) => { - +