-
-
Notifications
You must be signed in to change notification settings - Fork 424
Expand file tree
/
Copy pathoauth.ts
More file actions
49 lines (41 loc) · 1.52 KB
/
oauth.ts
File metadata and controls
49 lines (41 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import { defineNuxtModule, useNuxt, addServerTemplate } from 'nuxt/kit'
import process from 'node:process'
import { join } from 'node:path'
import { appendFileSync, existsSync, readFileSync } from 'node:fs'
import { randomUUID } from 'node:crypto'
import { getEnv } from '../config/env.ts'
export default defineNuxtModule({
meta: {
name: 'oauth',
},
async setup() {
const nuxt = useNuxt()
const { env, previewUrl, productionUrl } = await getEnv(nuxt.options.dev)
let clientUri: string
if (env === 'preview' && previewUrl) {
clientUri = previewUrl
} else if (env === 'release' && productionUrl) {
clientUri = productionUrl
} else {
clientUri = 'http://127.0.0.1:3000'
}
// bake it into a virtual file
addServerTemplate({
filename: '#oauth/config',
getContents: () => `export const clientUri = ${JSON.stringify(clientUri)};`,
})
if (nuxt.options._prepare || process.env.NUXT_SESSION_PASSWORD) {
return
}
const envPath = join(nuxt.options.rootDir, '.env')
const hasPassword =
existsSync(envPath) && /^NUXT_SESSION_PASSWORD=/m.test(readFileSync(envPath, 'utf-8'))
if (!hasPassword) {
// eslint-disable-next-line no-console
console.info('Generating NUXT_SESSION_PASSWORD for development environment.')
const password = randomUUID().replace(/-/g, '')
nuxt.options.runtimeConfig.sessionPassword = password
appendFileSync(envPath, `# generated by dev module\nNUXT_SESSION_PASSWORD=${password}\n`)
}
},
})