-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: migrate planetscale to supabase (#572)
- Loading branch information
1 parent
66bc88b
commit d1fb6d3
Showing
29 changed files
with
960 additions
and
683 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@blockchain-lab-um/dapp': minor | ||
--- | ||
|
||
Migrate Planetscale to Supabase |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
8 changes: 4 additions & 4 deletions
8
...le]/app/(public)/qr-code-session/page.tsx → ...]/app/(public)/encrypted-session/page.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
151
packages/dapp/src/app/api/qr-code-session/[id]/route.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.