Skip to content

Commit

Permalink
fix: feeds collectionSlug through me auth for graphql resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobsfletch committed Apr 14, 2021
1 parent 204b755 commit 9ee2f9c
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
13 changes: 11 additions & 2 deletions src/auth/graphql/resolvers/me.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
async function me(_, __, context) {
return this.operations.collections.auth.me({ req: context.req });
function me(collectionSlug: string): any {
async function resolver(_, __, context) {
return this.operations.collections.auth.me({
req: context.req,
collectionSlug,
});
}

const meResolver = resolver.bind(this);

return meResolver;
}

export default me;
11 changes: 7 additions & 4 deletions src/auth/operations/me.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,20 @@ export type Result = {
}

export type Arguments = {
req: PayloadRequest
req: PayloadRequest,
collectionSlug: string
}

async function me({ req }: Arguments): Promise<Result> {
async function me({
req,
collectionSlug,
}: Arguments): Promise<Result> {
const extractJWT = getExtractJWT(this.config);

if (req.user) {
const requestedSlug = req.route.path.split('/').filter((r) => r !== '')[0];
const user = { ...req.user };

if (user.collection !== requestedSlug) {
if (user.collection !== collectionSlug) {
return {
user: null,
};
Expand Down
6 changes: 5 additions & 1 deletion src/auth/requestHandlers/me.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import { PayloadRequest } from '../../express/types';

export default async function me(req: PayloadRequest, res: Response, next: NextFunction): Promise<any> {
try {
const response = await this.operations.collections.auth.me({ req });
const collectionSlug = req.route.path.split('/').filter((r) => r !== '')[0];
const response = await this.operations.collections.auth.me({
req,
collectionSlug,
});
return res.status(200).json(response);
} catch (err) {
return next(err);
Expand Down
2 changes: 1 addition & 1 deletion src/collections/graphql/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ function registerCollections(): void {
},
},
}),
resolve: me,
resolve: me(slug),
};

if (collection.config.auth.maxLoginAttempts > 0) {
Expand Down

0 comments on commit 9ee2f9c

Please sign in to comment.