Skip to content

Commit

Permalink
feat: Preserve auth header for playground (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
KirillDogadin-std authored Apr 17, 2023
1 parent 52a1e29 commit 54d2014
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 4 deletions.
54 changes: 54 additions & 0 deletions api/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
"dependencies": {
"@apollo/server": "^4.5.0",
"@prisma/client": "^4.11.0",
"@types/cookie-parser": "^1.4.3",
"bcrypt": "^5.1.0",
"body-parser": "^1.20.2",
"cookie-parser": "^1.4.6",
"cors": "^2.8.5",
"dotenv": "^16.0.3",
"express": "^4.18.2",
Expand Down
1 change: 1 addition & 0 deletions api/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export const createApp = (): Express => {
endpoint: '/api/graphql',
settings: {
'editor.theme': 'light',
'request.credentials': 'include',
},
}),
);
Expand Down
3 changes: 2 additions & 1 deletion api/src/graphql/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@ export function createContext(params: CreateContextParams): Context {
logger.trace('Creating context with params: %o', params);
const { req } = params;
const authorizationHeader = req.get('Authorization');
const cookieAuthHeader = req.cookies['gql:default'];
const token = authorizationHeader?.replace('Bearer ', '');

return {
request: params,
prisma,
apolloLogger,
getSession: async () => prisma.session.getSessionByToken(token),
getSession: async () => prisma.session.getSessionByToken(token || cookieAuthHeader),
};
}
13 changes: 10 additions & 3 deletions api/src/graphql/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { ApolloServerPlugin, ApolloServer } from '@apollo/server';
import { expressMiddleware } from '@apollo/server/express4';
import { ApolloServerPluginLandingPageDisabled } from '@apollo/server/plugin/disabled';
import bodyParser from 'body-parser';
import cookierParser from 'cookie-parser';
import cors from 'cors';
import { PORT } from '../env';
import { schemaWithMiddleware } from './schema';
Expand Down Expand Up @@ -41,9 +42,15 @@ export const startServer = async (
const apollo = createApolloServer();

await apollo.start();
app.use('/graphql', cors<cors.CorsRequest>(), bodyParser.json(), expressMiddleware(apollo, {
context: async (params) => (createContext(params)),
}));
app.use(
'/graphql',
cors<cors.CorsRequest>(),
cookierParser(undefined, { decode: (value: string) => value }),
bodyParser.json(),
expressMiddleware(apollo, {
context: async (params) => (createContext(params)),
}),
);

return httpServer.listen({ port: PORT }, () => {
logger.info(`Running on ${PORT}`);
Expand Down

0 comments on commit 54d2014

Please sign in to comment.