Skip to content

Commit

Permalink
getMe as middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
rwieruch committed Apr 16, 2020
1 parent 6503542 commit c9adeaa
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 22 deletions.
14 changes: 6 additions & 8 deletions pages/api/graphql.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { ApolloServer } from 'apollo-server-micro';
import cors from 'micro-cors';
import { buildSchema } from 'type-graphql';
import { applyMiddleware } from 'graphql-middleware';

import cors from 'micro-cors';

import 'reflect-metadata';

import getConnection from '@models/index';
Expand All @@ -13,8 +15,9 @@ import { ServerRequest, ServerResponse } from '@typeDefs/server';
import { ResolverContext } from '@typeDefs/resolver';

import resolvers from '@api/resolvers';
import getMe from '@api/middleware/me';
import meMiddleware from '@api/middleware/me';
import sentryMiddleware from '@api/middleware/sentry';

import firebaseAdmin from '@services/firebase/admin';

if (process.env.FIREBASE_ADMIN_UID) {
Expand Down Expand Up @@ -52,11 +55,9 @@ export default async (req: ServerRequest, res: ServerResponse) => {
});

const server = new ApolloServer({
schema: applyMiddleware(schema, sentryMiddleware),
schema: applyMiddleware(schema, sentryMiddleware, meMiddleware),

context: async ({ req, res }): Promise<ResolverContext> => {
const me = await getMe(req, res);

const adminConnector = new AdminConnector();
const partnerConnector = new PartnerConnector(connection!);
const courseConnector = new CourseConnector(connection!);
Expand All @@ -65,7 +66,6 @@ export default async (req: ServerRequest, res: ServerResponse) => {
return {
req,
res,
me,
adminConnector,
courseConnector,
partnerConnector,
Expand All @@ -78,7 +78,5 @@ export default async (req: ServerRequest, res: ServerResponse) => {
server.createHandler({ path: '/api/graphql' })
);

// await connection.close();

return handler(req, res);
};
17 changes: 13 additions & 4 deletions src/api/middleware/me.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,24 @@ import { AuthenticationError } from 'apollo-server-micro';
import firebaseAdmin from '@services/firebase/admin';

import { ResolverContext } from '@typeDefs/resolver';
import { ServerResponse, ServerRequest } from '@typeDefs/server';
import { User } from '@typeDefs/user';

export default async (req: ServerRequest, res: ServerResponse) => {
const { session } = req.cookies;
export default async (
resolve: Function,
root: any,
args: any,
context: ResolverContext,
info: any
) => {
const { session } = context.req.cookies;

if (!session) {
return undefined;
}

const CHECK_REVOKED = true;

return await firebaseAdmin
const me = await firebaseAdmin
.auth()
.verifySessionCookie(session, CHECK_REVOKED)
.then(async claims => {
Expand All @@ -24,4 +29,8 @@ export default async (req: ServerRequest, res: ServerResponse) => {
.catch(error => {
throw new AuthenticationError(error.message);
});

context.me = me;

return await resolve(root, args, context, info);
};
6 changes: 3 additions & 3 deletions src/api/resolvers/course/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ export default class CourseResolver {
async unlockedCourse(
@Arg('courseId') courseId: string,
@Ctx() ctx: ResolverContext
) {
): Promise<UnlockedCourse | null> {
if (!ctx.me) {
return null;
}
Expand All @@ -257,7 +257,7 @@ export default class CourseResolver {
@Arg('courseId') courseId: string,
@Arg('bundleId') bundleId: string,
@Ctx() ctx: ResolverContext
) {
): Promise<boolean> {
if (!ctx.me) {
return false;
}
Expand Down Expand Up @@ -292,7 +292,7 @@ export default class CourseResolver {
@Arg('bundleId') bundleId: string,
@Arg('uid') uid: string,
@Ctx() ctx: ResolverContext
) {
): Promise<boolean> {
await ctx.courseConnector.createCourse({
userId: uid,
courseId: courseId as COURSE,
Expand Down
4 changes: 3 additions & 1 deletion src/api/resolvers/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { NonEmptyArray } from 'type-graphql';

import MigrationResolvers from './migration';
import SessionResolver from './session';
import UserResolvers from './user';
Expand All @@ -24,4 +26,4 @@ export default [
CouponResolver,
PartnerResolver,
CommunityResolvers,
];
] as NonEmptyArray<Function>;
8 changes: 2 additions & 6 deletions src/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ export default async function() {
if (connection.isConnected) {
await connection.close();
}
} catch (error) {
console.log(error);
}
} catch (error) {}

try {
connection = await createConnection({
Expand All @@ -38,9 +36,7 @@ export default async function() {
},
}),
});
} catch (error) {
console.log(error);
}
} catch (error) {}

return connection;
}

0 comments on commit c9adeaa

Please sign in to comment.