This commit is contained in:
Your Name
2026-07-08 15:10:29 +00:00
parent d63ed3e7ef
commit 8f0f0cd36e
22 changed files with 1129 additions and 1032 deletions
+57 -21
View File
@@ -1,6 +1,15 @@
import type { FastifyInstance } from 'fastify'
import { getAuthKey, signIn, clearToken, isAuthenticated } from '../services/auth-service.js'
import { getTokenTTL } from '../services/token-cache.js'
import type { TrueApiEnv } from '../config.js'
import { getAuthKey, signIn, clearToken, getAuthStatus } from '../services/auth-service.js'
import { AppError, extractDebugInfo } from '../services/cises-service.js'
const VALID_ENVS = ['sandbox', 'prod']
function parseEnv(query: Record<string, string>): TrueApiEnv {
const env = query.env
if (env && VALID_ENVS.includes(env)) return env as TrueApiEnv
return 'sandbox'
}
const signInSchema = {
body: {
@@ -14,28 +23,55 @@ const signInSchema = {
}
export async function authRoutes(app: FastifyInstance): Promise<void> {
app.get('/auth/key', async () => {
const authKey = await getAuthKey()
return { uuid: authKey.uuid, data: authKey.data }
})
app.post<{ Body: { uuid: string; data: string } }>('/auth/simpleSignIn', { schema: signInSchema }, async (request) => {
const { uuid, data } = request.body
const result = await signIn({ uuid, data })
return result
})
app.get('/auth/status', async () => {
const authenticated = isAuthenticated()
const ttl = getTokenTTL()
return {
authenticated,
tokenExpiresAt: ttl ? new Date(ttl).toISOString() : null,
app.get('/auth/key', async (request, reply) => {
const env = parseEnv(request.query as Record<string, string>)
try {
const authKey = await getAuthKey(env)
return { uuid: authKey.uuid, data: authKey.data }
} catch (err) {
if (err instanceof AppError) {
return reply.status(err.statusCode).send({
error: err.message,
debugInfo: err.debugInfo,
})
}
const debugInfo = extractDebugInfo(err)
return reply.status(502).send({
error: 'Failed to get auth key from True API',
debugInfo,
})
}
})
app.post('/auth/logout', async () => {
clearToken()
app.post<{ Body: { uuid: string; data: string } }>('/auth/simpleSignIn', { schema: signInSchema }, async (request, reply) => {
const env = parseEnv(request.query as Record<string, string>)
try {
const { uuid, data } = request.body
const result = await signIn(env, { uuid, data })
return result
} catch (err) {
if (err instanceof AppError) {
return reply.status(err.statusCode).send({
error: err.message,
debugInfo: err.debugInfo,
})
}
const debugInfo = extractDebugInfo(err)
return reply.status(502).send({
error: 'Failed to sign in to True API',
debugInfo,
})
}
})
app.get('/auth/status', async (request) => {
const env = parseEnv(request.query as Record<string, string>)
return getAuthStatus(env)
})
app.post('/auth/logout', async (request) => {
const env = parseEnv(request.query as Record<string, string>)
clearToken(env)
return { success: true }
})
}