-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathreference.ts
More file actions
65 lines (60 loc) · 1.93 KB
/
reference.ts
File metadata and controls
65 lines (60 loc) · 1.93 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import type { FastifyPluginAsync } from 'fastify'
import { env } from '../lib/env.js'
import { verifyMagicLinkAndIssueToken } from './auth/magiclink/verify.js'
import { getReferenceHtml } from './reference/template.js'
const referenceRoutes: FastifyPluginAsync = async fastify => {
// Serve OpenAPI JSON
fastify.get(
'/openapi.json',
{
schema: {
hide: true,
tags: ['public'],
security: [],
},
},
async (_request, reply) => {
const openApiDoc = fastify.swagger()
return reply.send(openApiDoc)
},
)
// Serve custom HTML page with Scalar UI and login button
fastify.get(
'/',
{
schema: {
hide: true,
tags: ['public'],
security: [],
},
},
async (request, reply) => {
// Use request.headers.host which includes port if present
const host = request.headers.host || `${request.hostname}:${env.PORT}`
const apiUrl = `${request.protocol}://${host}`
const openApiUrl = `${apiUrl}/reference/openapi.json`
const callbackUrl = `${apiUrl}/reference`
// Magic link callback: token+verificationId in URL → verify server-side; verificationId only → code form (client-side)
const query = request.query as { token?: string; verificationId?: string }
const { token: urlToken, verificationId } = query
let jwtToken: string | null = null
if (urlToken && verificationId) {
const result = await verifyMagicLinkAndIssueToken(fastify, request, {
token: urlToken,
verificationId,
})
jwtToken = result?.accessToken ?? null
}
const html = getReferenceHtml({
apiUrl,
openApiUrl,
callbackUrl,
jwtToken,
verificationId: jwtToken ? undefined : (verificationId ?? undefined),
})
return reply.type('text/html').send(html)
},
)
}
export default referenceRoutes
export const prefixOverride = '/reference'