forked from rmanguinho/clean-ts-api
-
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.
feat: add auth directive to surveys query
- Loading branch information
1 parent
8b19fcc
commit 5a30d7f
Showing
5 changed files
with
33 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { makeAuthMiddleware } from '@/main/factories' | ||
|
||
import { SchemaDirectiveVisitor, ForbiddenError } from 'apollo-server-express' | ||
import { GraphQLField, defaultFieldResolver } from 'graphql' | ||
|
||
export class AuthDirective extends SchemaDirectiveVisitor { | ||
visitFieldDefinition (field: GraphQLField<any, any>): any { | ||
const { resolve = defaultFieldResolver } = field | ||
field.resolve = async (parent, args, context, info) => { | ||
const request = { | ||
accessToken: context?.req?.headers?.['x-access-token'] | ||
} | ||
const httpResponse = await makeAuthMiddleware().handle(request) | ||
if (httpResponse.statusCode === 200) { | ||
Object.assign(context?.req, httpResponse.body) | ||
return resolve.call(this, parent, args, context, info) | ||
} else { | ||
throw new ForbiddenError(httpResponse.body.message) | ||
} | ||
} | ||
} | ||
} |
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,5 @@ | ||
import { AuthDirective } from './auth-directive' | ||
|
||
export default { | ||
auth: AuthDirective | ||
} |
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