added basic token auth draft for any graphql query

This commit is contained in:
Samuel Andert
2023-08-22 09:43:02 +02:00
parent 0a4a793d39
commit d91e0b7b6e
4 changed files with 55 additions and 0 deletions

37
src/envelopePlugin.ts Normal file
View 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