Skip to content

Commit d189f99

Browse files
feat: support dynamic client URI for preview deployments (npmx-dev#739)
Co-authored-by: Daniel Roe <daniel@roe.dev>
1 parent 2ba9247 commit d189f99

3 files changed

Lines changed: 25 additions & 4 deletions

File tree

knip.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
"vite-plugin-pwa",
3737
"vue-router"
3838
],
39-
"ignoreUnresolved": ["#components"]
39+
"ignoreUnresolved": ["#components", "#oauth/config"]
4040
},
4141
"cli": {
4242
"project": ["src/**/*.ts"]

modules/oauth.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { defineNuxtModule, useNuxt } from 'nuxt/kit'
1+
import { defineNuxtModule, useNuxt, addServerTemplate } from 'nuxt/kit'
22
import { join } from 'node:path'
33
import { appendFileSync, existsSync, readFileSync } from 'node:fs'
44
import { randomUUID } from 'node:crypto'
@@ -9,6 +9,26 @@ export default defineNuxtModule({
99
},
1010
setup() {
1111
const nuxt = useNuxt()
12+
13+
const env = process.env.NUXT_ENV_VERCEL_ENV
14+
const previewUrl = process.env.NUXT_ENV_VERCEL_URL
15+
const prodUrl = process.env.NUXT_ENV_VERCEL_PROJECT_PRODUCTION_URL
16+
17+
let clientUri: string
18+
if (env === 'preview' && previewUrl) {
19+
clientUri = `https://${previewUrl}`
20+
} else if (env === 'production' && prodUrl) {
21+
clientUri = `https://${prodUrl}`
22+
} else {
23+
clientUri = 'http://127.0.0.1:3000'
24+
}
25+
26+
// bake it into a virtual file
27+
addServerTemplate({
28+
filename: '#oauth/config',
29+
getContents: () => `export const clientUri = ${JSON.stringify(clientUri)};`,
30+
})
31+
1232
if (nuxt.options._prepare || process.env.NUXT_SESSION_PASSWORD) {
1333
return
1434
}

server/utils/atproto/oauth.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@ import { getOAuthLock } from '#server/utils/atproto/lock'
66
import { useOAuthStorage } from '#server/utils/atproto/storage'
77
import { UNSET_NUXT_SESSION_PASSWORD } from '#shared/utils/constants'
88
import { OAuthMetadataSchema } from '#shared/schemas/oauth'
9+
// @ts-expect-error virtual file from oauth module
10+
import { clientUri } from '#oauth/config'
911
// TODO: limit scope as features gets added. atproto just allows login so no scary login screen till we have scopes
1012
export const scope = 'atproto'
1113

1214
export function getOauthClientMetadata() {
1315
const dev = import.meta.dev
1416

15-
// on dev, match in nuxt.config.ts devServer: { host: "127.0.0.1" }
16-
const client_uri = dev ? `http://127.0.0.1:3000` : 'https://npmx.dev'
17+
const client_uri = clientUri
1718
const redirect_uri = `${client_uri}/api/auth/atproto`
1819

1920
const client_id = dev

0 commit comments

Comments
 (0)