-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from Mintbean/feat/jimmy-test-cookies
Feat/jimmy test cookies
- Loading branch information
Showing
18 changed files
with
504 additions
and
304 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { ApolloServer } from "apollo-server-express"; | ||
import express, { Application } from "express"; | ||
import cookieParser from "cookie-parser"; | ||
|
||
export default function buildExpressServer(apolloServer: ApolloServer): Application { | ||
const app = express(); | ||
app.use(cookieParser()); | ||
apolloServer.applyMiddleware({ app }); | ||
return app; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { AuthenticationError } from "apollo-server-express"; | ||
import { Request, Response } from "express"; | ||
import { setCookie, clearCookie } from "./util/cookieUtils"; | ||
import { parseJwt } from "./util/jwtUtils"; | ||
|
||
export interface ExpressContext { | ||
req: Request; | ||
res: Response; | ||
} | ||
|
||
export interface ServerContext { | ||
getUserId(): string; | ||
setJwt(token: string): void; | ||
clearJwt(): void; | ||
// TODO: include userId and maybe auth scope, which will be parsed from req cookie | ||
} | ||
|
||
export type BuildExpressServerContext = (expressContext: ExpressContext) => ServerContext; | ||
|
||
export const buildExpressServerContext: BuildExpressServerContext = function ({ | ||
req, | ||
res, | ||
}: { | ||
req: Request; | ||
res: Response; | ||
}): ServerContext { | ||
// TODO: Clean up later | ||
let userId: string; | ||
const jwt: string = req.cookies.jwt; | ||
|
||
if (jwt) { | ||
try { | ||
const parsedToken = parseJwt(jwt); | ||
userId = parsedToken.sub; | ||
} catch (e) { | ||
// parseJwt throws an error in case of signature mismatch or jwt is expired | ||
res.clearCookie("jwt"); // We need to do this otherwise it will be an infinite loop | ||
throw new AuthenticationError(e.message); | ||
} | ||
} | ||
|
||
return { | ||
getUserId: () => userId, | ||
setJwt: setCookie(res), | ||
clearJwt: clearCookie(res), | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,13 @@ | ||
import express from "express"; | ||
import { buildPersistenceContext, buildResolverContext, buildExpressServerContext } from "./buildContext"; | ||
import { buildPersistenceContext, buildResolverContext } from "./buildContext"; | ||
import { buildExpressServerContext } from "./buildServerContext"; | ||
import buildSchema from "./buildSchema"; | ||
import buildServer from "./buildServer"; | ||
import cookieParser from "cookie-parser"; | ||
import buildApolloServer from "./buildApolloServer"; | ||
import buildExpressServer from "./buildExpressServer"; | ||
|
||
const persistenceContext = buildPersistenceContext(); | ||
const resolverContext = buildResolverContext(persistenceContext); | ||
const schema = buildSchema(resolverContext); | ||
const server = buildServer(schema, buildExpressServerContext); | ||
const app = express(); | ||
app.use(cookieParser()); | ||
const apolloServer = buildApolloServer(schema, buildExpressServerContext); | ||
const app = buildExpressServer(apolloServer); | ||
|
||
server.applyMiddleware({ app }); | ||
|
||
app.listen({ port: 4000 }, () => console.log(`Server ready at http://localhost:4000${server.graphqlPath}`)); | ||
app.listen({ port: 4000 }, () => console.log(`Server ready at http://localhost:4000${apolloServer.graphqlPath}`)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.