Use Fastify Mercurius and GraphQL contracts
Build Docker Image / build (push) Has been cancelled

This commit is contained in:
Ruslan Bakiev
2026-06-01 00:25:27 +05:00
parent b076343c57
commit 7fc16bb333
6 changed files with 1265 additions and 1567 deletions
+8 -6
View File
@@ -1,6 +1,6 @@
import { createRemoteJWKSet, jwtVerify, type JWTPayload } from "jose";
import { GraphQLError } from "graphql";
import type { Request } from "express";
import type { FastifyRequest } from "fastify";
import { prisma } from "./db.js";
const LOGTO_JWKS_URL =
@@ -21,7 +21,7 @@ export interface AuthContext {
export const SESSION_TOKEN_PREFIX = "optovia-session:";
function getBearerToken(req: Request): string {
function getBearerToken(req: FastifyRequest): string {
const auth = req.headers.authorization || "";
if (!auth.startsWith("Bearer "))
throw new GraphQLError("Missing Bearer token", {
@@ -35,7 +35,7 @@ function getBearerToken(req: Request): string {
return token;
}
function optionalBearerToken(req: Request): string | null {
function optionalBearerToken(req: FastifyRequest): string | null {
const auth = req.headers.authorization || "";
if (!auth.startsWith("Bearer ")) return null;
const token = auth.slice(7);
@@ -72,7 +72,7 @@ function hasManagerClaim(payload: JWTPayload): boolean {
);
}
export async function userContext(req: Request): Promise<AuthContext> {
export async function userContext(req: FastifyRequest): Promise<AuthContext> {
const token = optionalBearerToken(req);
if (token === null) return { scopes: [] };
if (token.startsWith(SESSION_TOKEN_PREFIX)) {
@@ -99,7 +99,9 @@ export async function userContext(req: Request): Promise<AuthContext> {
return { userId: payload.sub, scopes: scopesFromPayload(payload) };
}
export async function managerContext(req: Request): Promise<AuthContext> {
export async function managerContext(
req: FastifyRequest,
): Promise<AuthContext> {
const token = getBearerToken(req);
const { payload } = await jwtVerify(token, jwks, {
issuer: LOGTO_ISSUER,
@@ -119,7 +121,7 @@ export async function managerContext(req: Request): Promise<AuthContext> {
};
}
export async function teamContext(req: Request): Promise<AuthContext> {
export async function teamContext(req: FastifyRequest): Promise<AuthContext> {
const token = getBearerToken(req);
const { payload } = await jwtVerify(token, jwks, {
issuer: LOGTO_ISSUER,