Skip to content

Commit 1320467

Browse files
feat(oauth): support dynamic client URI for Vercel preview deployments
1 parent 32e1408 commit 1320467

2 files changed

Lines changed: 27 additions & 3 deletions

File tree

modules/oauth.ts

Lines changed: 24 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, addTemplate } 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,29 @@ 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+
const template = addTemplate({
28+
filename: 'oauth-config.mjs',
29+
getContents: () => `export const clientUri = ${JSON.stringify(clientUri)};`,
30+
write: true,
31+
})
32+
33+
nuxt.options.alias['#oauth/config'] = template.dst
34+
1235
if (nuxt.options._prepare || process.env.NUXT_SESSION_PASSWORD) {
1336
return
1437
}

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-ignore: 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)