Skip to content

Commit

Permalink
feat: migrate planetscale to supabase (#572)
Browse files Browse the repository at this point in the history
  • Loading branch information
martines3000 authored Mar 2, 2024
1 parent 66bc88b commit d1fb6d3
Show file tree
Hide file tree
Showing 29 changed files with 960 additions and 683 deletions.
5 changes: 5 additions & 0 deletions .changeset/twenty-turtles-approve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@blockchain-lab-um/dapp': minor
---

Migrate Planetscale to Supabase
4 changes: 1 addition & 3 deletions packages/dapp/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ SEPOLIA_RPC_URL=
IPFS_GATEWAY=
POLYGON_RPC_URL=
POLYGON_MUMBAI_RPC_URL=

# Prisma
DATABASE_URL=
=

# Masca version
NEXT_PUBLIC_MASCA_VERSION=v1.1.0
Expand Down
3 changes: 0 additions & 3 deletions packages/dapp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
"dev": "cross-env next dev",
"dev:local": "cross-env USE_LOCAL='true' next dev",
"docker:build": "docker build . -t blockchain-lab-um/dapp:latest",
"postinstall": "pnpm prisma generate --schema=./prisma/schema.prisma",
"lint": "pnpm lint:next && pnpm lint:tsc && pnpm lint:prettier && pnpm lint:stylelint",
"lint:fix": "next lint . --fix && prettier . --write",
"lint:next": "next lint",
Expand All @@ -28,7 +27,6 @@
"@headlessui/react": "^1.7.17",
"@heroicons/react": "^2.0.18",
"@nextui-org/react": "^2.2.9",
"@prisma/client": "^5.7.0",
"@radix-ui/react-toast": "^1.1.5",
"@react-oauth/google": "^0.11.1",
"@supabase/supabase-js": "^2.38.5",
Expand Down Expand Up @@ -65,7 +63,6 @@
"next-sitemap": "^4.2.3",
"next-themes": "^0.2.1",
"pino-pretty": "^10.3.1",
"prisma": "^5.7.0",
"qrcode.react": "^3.1.0",
"qs": "^6.11.2",
"react": "18.2.0",
Expand Down
15 changes: 0 additions & 15 deletions packages/dapp/prisma/schema.prisma

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { Metadata } from 'next';

import QRCodeSessionDisplay from '@/components/QRSessionDisplay';
import EncryptedSessionDisplay from '@/components/EncryptedSessionDisplay';

export const metadata: Metadata = {
title: 'QR Code Session',
description: 'QR Code Session for Masca.',
title: 'Encrypted Session',
description: 'Encrypted Session for Masca.',
};

export default function Page() {
return (
<div className="flex flex-1 items-center justify-center">
<div className="dark:bg-navy-blue-800 dark:text-navy-blue-400 w-full rounded-3xl bg-white shadow-lg md:max-w-4xl">
<QRCodeSessionDisplay />
<EncryptedSessionDisplay />
</div>
</div>
);
Expand Down
4 changes: 2 additions & 2 deletions packages/dapp/src/app/[locale]/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import clsx from 'clsx';
import AppBottomBar from '@/components/AppBottomBar';
import AppNavbar from '@/components/AppNavbar';
import { CookiesProvider } from '@/components/CookiesProvider';
import { EncryptedSessionProvider } from '@/components/EncryptedSessionProvider';
import MascaProvider from '@/components/MascaProvider';
import QRCodeSessionProvider from '@/components/QRCodeSessionProvider';
import { SignInModal } from '@/components/SignInModal';
import ToastWrapper from '@/components/ToastWrapper';
import WagmiProviderWrapper from '@/components/WagmiProvider';
Expand All @@ -30,7 +30,7 @@ export default async function AppLayout({
</div>
</div>
<AppBottomBar />
<QRCodeSessionProvider />
<EncryptedSessionProvider />
<SignInModal />
<CookiesProvider />
</WagmiProviderWrapper>
Expand Down
118 changes: 118 additions & 0 deletions packages/dapp/src/app/api/encrypted-session/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import { NextRequest, NextResponse } from 'next/server';
import { createClient } from '@supabase/supabase-js';
import jwt from 'jsonwebtoken';

import { Database } from '@/utils/supabase/database.types';

const CORS_HEADERS = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type',
};

export async function GET(request: NextRequest) {
try {
const token = request.headers.get('Authorization')?.replace('Bearer ', '');

if (!token) {
return new NextResponse('Unauthorized', {
status: 401,
headers: {
...CORS_HEADERS,
},
});
}

const user = jwt.verify(token, process.env.SUPABASE_JWT_SECRET!) as {
sub: string;
address: string;
aud: string;
role: string;
iat: number;
exp: number;
};

const supabase = createClient<Database>(
process.env.NEXT_PUBLIC_SUPABASE_URL!,
process.env.SUPABASE_SECRET_KEY!
);

const { data: selectData, error: selectError } = await supabase
.from('encrypted_sessions')
.select('id')
.eq('user_id', user.sub);

if (selectError) {
return new NextResponse('Internal Server Error', {
status: 500,
headers: {
...CORS_HEADERS,
},
});
}

// If session is found delete it
if (selectData.length !== 0) {
const { error: deleteError } = await supabase
.from('encrypted_sessions')
.delete()
.eq('user_id', user.sub);

if (deleteError) {
return new NextResponse('Internal Server Error', {
status: 500,
headers: {
...CORS_HEADERS,
},
});
}
}

// Create a new session
const { data: insertData, error: insertError } = await supabase
.from('encrypted_sessions')
.insert({
user_id: user.sub,
})
.select()
.limit(1)
.single();

if (insertError || !insertData) {
return new NextResponse('Internal Server Error', {
status: 500,
headers: {
...CORS_HEADERS,
},
});
}

return NextResponse.json(
{
sessionId: insertData.id,
},
{
status: 201,
headers: {
...CORS_HEADERS,
},
}
);
} catch (error) {
return new NextResponse('Internal Server Error', {
status: 500,
headers: {
...CORS_HEADERS,
},
});
}
}

export async function OPTIONS() {
return new NextResponse(null, {
status: 200,
headers: {
...CORS_HEADERS,
},
});
}
151 changes: 0 additions & 151 deletions packages/dapp/src/app/api/qr-code-session/[id]/route.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/dapp/src/components/AppBottomBar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const OTHER_LINKS = [
},
{
name: 'qr-scanner',
href: '/app/qr-code-session',
href: '/app/encrypted-session',
requiresConnection: false,
},
];
Expand Down
Loading

0 comments on commit d1fb6d3

Please sign in to comment.