updated context
This commit is contained in:
parent
04b606b7fa
commit
40c35bda13
7 changed files with 67 additions and 25 deletions
|
|
@ -3,15 +3,14 @@ import { expressMiddleware } from '@apollo/server/express4';
|
|||
import { ApolloServerPluginDrainHttpServer } from '@apollo/server/plugin/drainHttpServer';
|
||||
import cors from 'cors';
|
||||
import 'dotenv/config';
|
||||
import express, { Request, Response } from 'express';
|
||||
import express from 'express';
|
||||
import fs from 'fs';
|
||||
import http from 'http';
|
||||
import jwt from 'jsonwebtoken';
|
||||
import path from 'path';
|
||||
import ViteExpress from 'vite-express';
|
||||
import { getIdentityContext, IdentityContext } from './identity';
|
||||
|
||||
import { Mutation, Query } from './graphql/graphql';
|
||||
import { prisma } from './prisma/client';
|
||||
|
||||
const __dirname = import.meta.dirname;
|
||||
const HOST = process.env.VITE_HOST || 'localhost';
|
||||
|
|
@ -29,10 +28,6 @@ const resolvers = {
|
|||
// Subscription,
|
||||
};
|
||||
|
||||
interface IdentityContext {
|
||||
authScope?: String;
|
||||
}
|
||||
|
||||
const apollo = new ApolloServer<IdentityContext>({
|
||||
typeDefs,
|
||||
resolvers,
|
||||
|
|
@ -46,7 +41,7 @@ app.use(
|
|||
cors<cors.CorsRequest>(),
|
||||
express.json(),
|
||||
expressMiddleware(apollo, {
|
||||
context: getUser
|
||||
context: getIdentityContext
|
||||
})
|
||||
);
|
||||
|
||||
|
|
@ -55,16 +50,3 @@ await new Promise<void>(resolve => server.listen({ hostname: HOST, port: PORT },
|
|||
process.stdout.write(MOTD);
|
||||
|
||||
ViteExpress.bind(app, server);
|
||||
|
||||
async function getUser({ req, res }: { req: Request; res: Response }) {
|
||||
const authHeader = req.headers.authorization;
|
||||
if (!authHeader) return { identity: null };
|
||||
const token = authHeader.split(' ')[1];
|
||||
|
||||
const accessTokenSecret = process.env.ACCESS_TOKEN_SECRET;
|
||||
if (accessTokenSecret == null) throw Error('No `accessToken`');
|
||||
|
||||
const decoded = jwt.verify(token, accessTokenSecret) as { userId: string };
|
||||
const identity = await prisma.user.findUnique({ where: { id: decoded.userId } });
|
||||
return { identity };
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue