re-implemented JWT auth
This commit is contained in:
parent
c190d368de
commit
b47b0f3752
18 changed files with 390 additions and 174 deletions
|
|
@ -1,6 +1,7 @@
|
|||
import { ApolloServer } from '@apollo/server';
|
||||
import { expressMiddleware } from '@apollo/server/express4';
|
||||
import { ApolloServerPluginDrainHttpServer } from '@apollo/server/plugin/drainHttpServer';
|
||||
import cookies from 'cookie-parser';
|
||||
import cors from 'cors';
|
||||
import 'dotenv/config';
|
||||
import express from 'express';
|
||||
|
|
@ -8,17 +9,14 @@ import fs from 'fs';
|
|||
import https from 'https';
|
||||
import path from 'path';
|
||||
import ViteExpress from 'vite-express';
|
||||
import { toAppConfig } from './config';
|
||||
import { refreshTokenController } from './express/auth/refreshToken';
|
||||
import { Mutation, Query } from './graphql/graphql';
|
||||
import { getIdentityContext, IdentityContext } from './identity';
|
||||
|
||||
const MODE = process.env.VITE_MODE || 'prod';
|
||||
const isProd = MODE === 'prod';
|
||||
export const config = toAppConfig();
|
||||
|
||||
const __dirname = import.meta.dirname;
|
||||
const HOST = process.env.VITE_HOST || 'localhost';
|
||||
const PORT = parseInt(process.env.VITE_PORT || '3000');
|
||||
const MOTD = `\n\n\n\x1b[35m-server is listening at...-\x1b[0m\n\nhttp://${HOST}:${PORT}\nhttp://localhost:${PORT}\n\n\n`;
|
||||
|
||||
const key = fs.readFileSync(path.join(__dirname + '../../../certs/selfsigned.key'));
|
||||
const cert = fs.readFileSync(path.join(__dirname + '../../../certs/selfsigned.crt'));
|
||||
|
||||
|
|
@ -36,7 +34,7 @@ const resolvers = {
|
|||
const apollo = new ApolloServer<IdentityContext>({
|
||||
typeDefs,
|
||||
resolvers,
|
||||
introspection: !isProd,
|
||||
introspection: !config.isProd,
|
||||
plugins: [ApolloServerPluginDrainHttpServer({ httpServer: server })],
|
||||
formatError: formattedError => {
|
||||
process.stdout.write('\n```\n');
|
||||
|
|
@ -48,17 +46,21 @@ const apollo = new ApolloServer<IdentityContext>({
|
|||
|
||||
await apollo.start();
|
||||
|
||||
app.use(cors<cors.CorsRequest>(config.corsOptions));
|
||||
app.use(express.json(config.limit));
|
||||
app.use(cookies());
|
||||
app.get('/api/refreshAccessToken', refreshTokenController);
|
||||
app.use(
|
||||
'/graphql',
|
||||
cors<cors.CorsRequest>(),
|
||||
express.json(),
|
||||
expressMiddleware(apollo, {
|
||||
context: getIdentityContext
|
||||
})
|
||||
);
|
||||
|
||||
await new Promise<void>(resolve => server.listen({ hostname: HOST, port: PORT }, resolve));
|
||||
await new Promise<void>(resolve =>
|
||||
server.listen({ hostname: config.host, port: config.port }, resolve)
|
||||
);
|
||||
|
||||
process.stdout.write(MOTD);
|
||||
process.stdout.write(config.greeting);
|
||||
|
||||
ViteExpress.bind(app, server);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue