-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a0302a6
commit f26ebe8
Showing
11 changed files
with
2,147 additions
and
1,634 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import gql from 'graphql-tag' | ||
|
||
import { createValidatorDirective } from '@redwoodjs/graphql-server' | ||
|
||
import { requireAuth as applicationRequireAuth } from 'src/lib/auth' | ||
|
||
export const schema = gql` | ||
""" | ||
Use to check whether or not a user is authenticated and is associated | ||
with an optional set of roles. | ||
""" | ||
directive @requireAuth(roles: [String]) on FIELD_DEFINITION | ||
` | ||
|
||
const validate = ({ directiveArgs }) => { | ||
const { roles } = directiveArgs | ||
applicationRequireAuth({ roles }) | ||
} | ||
|
||
const requireAuth = createValidatorDirective(schema, validate) | ||
|
||
export default requireAuth |
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,18 @@ | ||
import { mockRedwoodDirective, getDirectiveName } from '@redwoodjs/testing/api' | ||
|
||
import requireAuth from './requireAuth' | ||
|
||
describe('requireAuth directive', () => { | ||
it('declares the directive sdl as schema, with the correct name', () => { | ||
expect(requireAuth.schema).toBeTruthy() | ||
expect(getDirectiveName(requireAuth.schema)).toBe('requireAuth') | ||
}) | ||
|
||
it('requireAuth has stub implementation. Should not throw when current user', () => { | ||
// If you want to set values in context, pass it through e.g. | ||
// mockRedwoodDirective(requireAuth, { context: { currentUser: { id: 1, name: 'Lebron McGretzky' } }}) | ||
const mockExecution = mockRedwoodDirective(requireAuth, { context: {} }) | ||
|
||
expect(mockExecution).not.toThrowError() | ||
}) | ||
}) |
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,16 @@ | ||
import gql from 'graphql-tag' | ||
|
||
import { createValidatorDirective } from '@redwoodjs/graphql-server' | ||
|
||
export const schema = gql` | ||
""" | ||
Use to skip authentication checks and allow public access. | ||
""" | ||
directive @skipAuth on FIELD_DEFINITION | ||
` | ||
|
||
const skipAuth = createValidatorDirective(schema, () => { | ||
return | ||
}) | ||
|
||
export default skipAuth |
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 { getDirectiveName } from '@redwoodjs/testing/api' | ||
|
||
import skipAuth from './skipAuth' | ||
|
||
describe('skipAuth directive', () => { | ||
it('declares the directive sdl as schema, with the correct name', () => { | ||
expect(skipAuth.schema).toBeTruthy() | ||
expect(getDirectiveName(skipAuth.schema)).toBe('skipAuth') | ||
}) | ||
}) |
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,22 +1,18 @@ | ||
import { | ||
createGraphQLHandler, | ||
makeMergedSchema, | ||
makeServices, | ||
} from '@redwoodjs/api' | ||
import { createGraphQLHandler } from '@redwoodjs/graphql-server' | ||
import { logger } from 'src/lib/logger' | ||
|
||
import schemas from 'src/graphql/**/*.{js,ts}' | ||
import directives from 'src/directives/**/*.{js,ts}' | ||
import sdls from 'src/graphql/**/*.sdl.{js,ts}' | ||
import services from 'src/services/**/*.{js,ts}' | ||
import { db } from 'src/lib/db' | ||
|
||
export const handler = createGraphQLHandler({ | ||
loggerConfig: { logger, options: {} }, | ||
schema: makeMergedSchema({ | ||
schemas, | ||
services: makeServices({ services }), | ||
}), | ||
loggerConfig: { logger, options: { operationName: true, query: true } }, | ||
directives, | ||
sdls, | ||
services, | ||
onException: () => { | ||
// Disconnect from your database with an unhandled exception. | ||
db.$disconnect() | ||
} | ||
}, | ||
}) |
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,25 @@ | ||
/** | ||
* Once you are ready to add authentication to your application | ||
* you'll build out requireAuth() with real functionality. For | ||
* now we just return `true` so that the calls in services | ||
* have something to check against, simulating a logged | ||
* in user that is allowed to access that service. | ||
* | ||
* See https://redwoodjs.com/docs/authentication for more info. | ||
*/ | ||
export const isAuthenticated = () => { | ||
return true | ||
} | ||
|
||
export const hasRole = ({ roles }) => { | ||
return roles !== undefined | ||
} | ||
|
||
// This is used by the redwood directive | ||
// in ./api/src/directives/requireAuth | ||
|
||
// Roles are passed in by the requireAuth directive if you have auth setup | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
export const requireAuth = ({ roles }) => { | ||
return isAuthenticated() | ||
} |
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.