added basic token auth draft for any graphql query
This commit is contained in:
37
src/envelopePlugin.ts
Normal file
37
src/envelopePlugin.ts
Normal file
@ -0,0 +1,37 @@
|
||||
import { useGenericAuth } from '@envelop/generic-auth'
|
||||
import { MeshPlugin } from '@graphql-mesh/types'
|
||||
|
||||
const path = require('path');
|
||||
require('dotenv').config({ path: path.resolve(__dirname, '../.env.local') });
|
||||
|
||||
|
||||
type UserType = {
|
||||
id: string
|
||||
}
|
||||
|
||||
const resolveUserFn = async context => {
|
||||
const authHeader = context.req.headers.authorization;
|
||||
if (authHeader !== "token123") {
|
||||
console.error('Failed to validate token')
|
||||
return null
|
||||
}
|
||||
// If the token is valid, return the user
|
||||
// Replace this with your actual user fetching logic
|
||||
return { id: 'user1' };
|
||||
}
|
||||
|
||||
const validateUser = params => {
|
||||
if (!params.user) {
|
||||
return new Error(`Unauthenticated!`)
|
||||
}
|
||||
}
|
||||
|
||||
const plugins: MeshPlugin = [
|
||||
useGenericAuth({
|
||||
resolveUserFn,
|
||||
validateUser,
|
||||
mode: 'protect-all'
|
||||
})
|
||||
]
|
||||
|
||||
export default plugins
|
Reference in New Issue
Block a user