Skip to content

Commit c79ac34

Browse files
committed
wip
1 parent 526a091 commit c79ac34

File tree

6 files changed

+548
-50
lines changed

6 files changed

+548
-50
lines changed

apps/postgres-new/app/api/deploy/route.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { NextResponse } from 'next/server'
33

44
const supabase = createClient()
55

6-
export async function POST(req: Request) {
6+
export async function GET(req: Request) {
77
const { data, error } = await supabase.auth.getUser()
88

99
// We have middleware, so this should never happen (used for type narrowing)
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { createClient } from '~/utils/supabase/server'
2+
import { NextRequest, NextResponse } from 'next/server'
3+
import { env } from 'process'
4+
5+
const supabase = createClient()
6+
7+
export async function GET(req: NextRequest) {
8+
const { data, error } = await supabase.auth.getUser()
9+
console.log({ data, error })
10+
// We have middleware, so this should never happen (used for type narrowing)
11+
if (error) {
12+
return new Response('Unauthorized', { status: 401 })
13+
}
14+
15+
const { user } = data
16+
17+
console.log(req.nextUrl.searchParams)
18+
19+
const code = req.nextUrl.searchParams.get('code') as string | null
20+
21+
if (!code) {
22+
return new Response('No code provided', { status: 400 })
23+
}
24+
25+
const tokensResponse = await fetch('https://api.supabase.com/v1/oauth/token', {
26+
method: 'POST',
27+
headers: {
28+
'Content-Type': 'application/x-www-form-urlencoded',
29+
Accept: 'application/json',
30+
Authorization: `Basic ${btoa(`${process.env.NEXT_PUBLIC_SUPABASE_OAUTH_CLIENT_ID}:${process.env.SUPABASE_OAUTH_SECRET}`)}`,
31+
},
32+
body: new URLSearchParams({
33+
grant_type: 'authorization_code',
34+
code,
35+
redirect_uri: req.nextUrl.origin + '/api/oauth/callback',
36+
}),
37+
})
38+
39+
const tokens = (await tokensResponse.json()) as {
40+
access_token: string
41+
refresh_token: string
42+
expires_in: number
43+
token_type: 'Bearer'
44+
}
45+
46+
return NextResponse.json({ user, tokens })
47+
}

apps/postgres-new/components/sidebar.tsx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -489,9 +489,16 @@ function DatabaseMenuItem({ database, isActive }: DatabaseMenuItemProps) {
489489
className="bg-inherit justify-start hover:bg-neutral-200 flex gap-3"
490490
onClick={async (e) => {
491491
e.preventDefault()
492-
// check is user has a Supabase token, if not do OAuth flow
493-
// initiate Supabase Oauth flow
494-
setIsPopoverOpen(false)
492+
const params = new URLSearchParams({
493+
client_id: process.env.NEXT_PUBLIC_SUPABASE_OAUTH_CLIENT_ID!,
494+
redirect_uri: `${window.location.origin}/api/oauth/callback`,
495+
response_type: 'code',
496+
state: JSON.stringify({
497+
databaseId: database.id,
498+
}),
499+
})
500+
window.location.href =
501+
'https://api.supabase.com/v1/oauth/authorize' + '?' + params.toString()
495502
}}
496503
disabled={user === undefined}
497504
>

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
"scripts": {
44
"dev": "npm run dev --workspace postgres-new"
55
},
6-
"workspaces": ["apps/*"],
6+
"workspaces": [
7+
"apps/*"
8+
],
79
"devDependencies": {
810
"supabase": "^1.191.3"
9-
}
11+
},
12+
"packageManager": "[email protected]+sha512.e5a7e52a4183a02d5931057f7a0dbff9d5e9ce3161e33fa68ae392125b79282a8a8a470a51dfc8a0ed86221442eb2fb57019b0990ed24fab519bf0e1bc5ccfc4"
1013
}

0 commit comments

Comments
 (0)