Skip to content

Commit

Permalink
feat: replaces next-auth with clerk & adds privacy policy (#260)
Browse files Browse the repository at this point in the history
  • Loading branch information
ixahmedxi authored Jul 22, 2023
1 parent dd8c21b commit 22eee04
Show file tree
Hide file tree
Showing 25 changed files with 510 additions and 613 deletions.
14 changes: 7 additions & 7 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ DATABASE_URL="postgres://username:password@localhost:5432/db_name"
UPSTASH_REDIS_REST_URL="<redis_url>"
UPSTASH_REDIS_REST_TOKEN="<redis_token>"

# Next Auth
NEXTAUTH_URL="http://localhost:3000"
NEXTAUTH_SECRET="secretsecret"

# Github Auth
GITHUB_CLIENT_ID="<client_id>"
GITHUB_CLIENT_SECRET="<client_secret>"
# Clerk authentication
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY=<publishable-key>
CLERK_SECRET_KEY=<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
1 change: 0 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
4 changes: 2 additions & 2 deletions apps/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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:^",
Expand All @@ -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",
Expand Down
12 changes: 0 additions & 12 deletions apps/web/src/components/Greeting.tsx

This file was deleted.

19 changes: 17 additions & 2 deletions apps/web/src/middleware.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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<Response | undefined> {
const ip = request.ip ?? '127.0.0.1';
Expand All @@ -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)(.*)'],
};
6 changes: 3 additions & 3 deletions apps/web/src/pages/_app.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -32,15 +32,15 @@ const App = ({ Component, pageProps }: AppPropsWithLayout) => {
return (
<div className={`${inter.variable} ${jetBrainsMono.variable} font-sans`}>
<DefaultSeo {...seo} />
<SessionProvider session={pageProps.session}>
<ClerkProvider {...pageProps}>
<ThemeProvider attribute="class">
<WrapBalancerProvider>
{getLayout(<Component {...pageProps} />)}
</WrapBalancerProvider>
<Analytics />
<ReactQueryDevtools initialIsOpen={false} />
</ThemeProvider>
</SessionProvider>
</ClerkProvider>
<style jsx global>{`
:root {
--font-inter: ${inter.variable}, sans-serif;
Expand Down
5 changes: 0 additions & 5 deletions apps/web/src/pages/api/auth/[...nextauth].ts

This file was deleted.

14 changes: 14 additions & 0 deletions apps/web/src/pages/app/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { UserButton } from '@clerk/nextjs';

import { type NextPageWithLayout } from '../../utils/NextPageWithLayout';

const AppPage: NextPageWithLayout = () => {
return (
<main>
<h1>Welcome to Noodle&apos;s application</h1>
<UserButton afterSignOutUrl="/" />
</main>
);
};

export default AppPage;
23 changes: 23 additions & 0 deletions apps/web/src/pages/privacy.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { useEffect } from 'react';
import { type NextPage } from 'next';

const PrivacyPage: NextPage = () => {
useEffect(() => {
const script = document.createElement('script');
script.src = 'https://app.termly.io/embed-policy.min.js';
script.async = true;
document.body.appendChild(script);
}, []);

return (
<div
/*
// @ts-expect-error it needs this attribute */
name="termly-embed"
data-id="bccd9db6-3bed-4b7b-acae-35aecd4db216"
data-type="iframe"
/>
);
};

export default PrivacyPage;
36 changes: 36 additions & 0 deletions apps/web/src/pages/sign-in/[[...index]].tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { SignIn } from '@clerk/nextjs';
import { dark } from '@clerk/themes';
import { useTheme } from 'next-themes';

import { type NextPageWithLayout } from '../../utils/NextPageWithLayout';

const SignInPage: NextPageWithLayout = () => {
const { resolvedTheme } = useTheme();

return (
<main className="container mx-auto flex min-h-screen items-center justify-center">
<SignIn
appearance={{
baseTheme:
resolvedTheme === 'dark' ? dark : { __type: 'prebuilt_appearance' },
elements: {
formButtonPrimary: 'hover:bg-primary-900 transition-colors',
formFieldInput:
'bg-gray-2 dark:bg-graydark-2 border-gray-6 dark:border-graydark-6',
card: 'bg-gray-1 dark:bg-graydark-1 border border-gray-4 dark:border-graydark-4 shadow-none',
},
layout: {
privacyPageUrl: '/privacy',
termsPageUrl: '/terms',
socialButtonsPlacement: 'bottom',
},
variables: {
colorPrimary: '#e64d67',
},
}}
/>
</main>
);
};

export default SignInPage;
36 changes: 36 additions & 0 deletions apps/web/src/pages/sign-up/[[...index]].tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { SignUp } from '@clerk/nextjs';
import { dark } from '@clerk/themes';
import { useTheme } from 'next-themes';

import { type NextPageWithLayout } from '../../utils/NextPageWithLayout';

const SignUpPage: NextPageWithLayout = () => {
const { resolvedTheme } = useTheme();

return (
<main className="container mx-auto flex min-h-screen items-center justify-center">
<SignUp
appearance={{
baseTheme:
resolvedTheme === 'dark' ? dark : { __type: 'prebuilt_appearance' },
elements: {
formButtonPrimary: 'hover:bg-primary-900 transition-colors',
formFieldInput:
'bg-gray-2 dark:bg-graydark-2 border-gray-6 dark:border-graydark-6',
card: 'bg-gray-1 dark:bg-graydark-1 border border-gray-4 dark:border-graydark-4 shadow-none',
},
layout: {
privacyPageUrl: '/privacy',
termsPageUrl: '/terms',
socialButtonsPlacement: 'bottom',
},
variables: {
colorPrimary: '#e64d67',
},
}}
/>
</main>
);
};

export default SignUpPage;
4 changes: 1 addition & 3 deletions apps/web/src/utils/NextPageWithLayout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@ import type { ReactElement, ReactNode } from 'react';
import { type NextPage } from 'next';
import { type AppProps } from 'next/app';

import { type Session } from '@noodle/auth';

export type NextPageWithLayout<P = Record<string, unknown>, IP = P> = NextPage<
P,
IP
> & {
getLayout?: (page: ReactElement) => ReactNode;
};

export type AppPropsWithLayout = AppProps<{ session: Session | null }> & {
export type AppPropsWithLayout = AppProps & {
Component: NextPageWithLayout;
};
2 changes: 1 addition & 1 deletion packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"typecheck": "tsc --noEmit"
},
"dependencies": {
"@noodle/auth": "workspace:^",
"@clerk/nextjs": "^4.23.0",
"@noodle/db": "workspace:^",
"@trpc/next": "^10.35.0",
"@trpc/server": "^10.35.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/api/src/middlewares/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import { TRPCError } from '@trpc/server';
import { t } from '../setup/trpc';

const isAuthenticated = t.middleware(({ ctx, next }) => {
if (!ctx.session?.user) {
if (!ctx.auth.userId) {
throw new TRPCError({ code: 'UNAUTHORIZED' });
}
return next({
ctx: {
session: { ...ctx.session, user: ctx.session.user },
auth: ctx.auth,
},
});
});
Expand Down
4 changes: 0 additions & 4 deletions packages/api/src/root.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import { greetingRouter } from './routers/greeting';
import { userRouter } from './routers/user';
import { waitListRouter } from './routers/wait-list';
import { createRouter } from './setup/trpc';

export const appRouter = createRouter({
greeting: greetingRouter,
user: userRouter,
waitlist: waitListRouter,
});

Expand Down
17 changes: 0 additions & 17 deletions packages/api/src/routers/greeting/index.ts

This file was deleted.

12 changes: 0 additions & 12 deletions packages/api/src/routers/user/index.ts

This file was deleted.

15 changes: 8 additions & 7 deletions packages/api/src/setup/context.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import type { Session } from '@noodle/auth';
import type {
SignedInAuthObject,
SignedOutAuthObject,
} from '@clerk/nextjs/server';
import { getAuth } from '@clerk/nextjs/server';
import { type CreateNextContextOptions } from '@trpc/server/adapters/next';

import { getServerSession } from '@noodle/auth';
import { prisma } from '@noodle/db';

type ContextOptions = {
session: Session | null;
auth: SignedInAuthObject | SignedOutAuthObject;
};

export const createInnerContext = (opts: ContextOptions) => {
Expand All @@ -15,10 +18,8 @@ export const createInnerContext = (opts: ContextOptions) => {
};
};

export const createContext = async ({ req, res }: CreateNextContextOptions) => {
const session = await getServerSession({ req, res });

return createInnerContext({ session });
export const createContext = ({ req }: CreateNextContextOptions) => {
return createInnerContext({ auth: getAuth(req) });
};

export type Context = typeof createContext;
30 changes: 0 additions & 30 deletions packages/auth/package.json

This file was deleted.

Loading

0 comments on commit 22eee04

Please sign in to comment.