Я получил решение после некоторого обходного пути.
Код серверной стороны
const bodyParser = require('body-parser');
const cors = require('cors');
const express = require('express');
const expressJwt = require('express-jwt'); //auth
const jwt = require('jsonwebtoken'); //auth
const db = require('./db');
const port = process.env.PORT || 9000;
const jwtSecret = Buffer.from('Zn8Q5tyZ/G1MHltc4F/gTkVJMlrbKiZt', 'base64');
const app = express();
const fs = require('fs')
const typeDefs = fs.readFileSync('./schema.graphql',{encoding:'utf-8'})
const resolvers = require('./resolvers')
const {makeExecutableSchema} = require('graphql-tools')
const schema = makeExecutableSchema({typeDefs, resolvers})
// authentication middleware
const authMiddleware = expressJwt({
secret: jwtSecret,
credentialsRequired: false
})
app.use(cors(), bodyParser.json(), authMiddleware);
//Setup endpoint routes
var router = express.Router();
//Basic Router Config
router.use(function(req, res, next){
var nodeSSPI = require('node-sspi')
//Integrated Authentication for Windows
var nodeSSPIObj = new nodeSSPI({
retrieveGroups: true
});
try{
nodeSSPIObj.authenticate(req, res, function(err){
res.finished || next();
});
}
catch(err)
{
res.status(500).send(JSON.stringify({status: 500, message: "Something went wrong", detail: err.message}));
}
});
// windows
router.get('/', function(req, res){
// Do LDAP authentication or whatever
const token = jwt.sign({authUser: req.connection.user}, jwtSecret);
res.send({token});
});
// Regular login
router.post('/login', (req, res) => {
const email = req.body.email;
const password = req.body.password;
const user = authenticateUser // SQL/mongo/etc..
const token = jwt.sign({authUser: user.id}, jwtSecret);
res.send({token});
});
app.use('/api', router);
const {graphiqlExpress,graphqlExpress} = require('apollo-server-express')
app.use('/graphql', graphqlExpress((req) => ({
schema,
context: {user: req.user && req.user.authUser}
})));
app.use('/graphiql',graphiqlExpress({endpointURL:'/graphql'}))
app.listen(
port, () => console.info(
`Server started on port ${port}. use http://localhost:${port}/graphiql`
)
);
На стороне клиента я использовал вызов Ajax Jquery для POC.
Для Windows Логин
$.ajax({
url: "http://localhost:9000/api",
contentType: "application/json",
xhrFields: {
withCredentials: true
},
type: "GET",
data: '',
success: function (response) {
loginToken = response.token;
},
error: (xhr, err) => alert('error')
})
с учетными данными
var email = $("#txtEmail").val();
var password = $("#txtPwd").val();
if(email && password) {
$.ajax({
url:"http://localhost:9000/api/login",
contentType:"application/json",
type:"POST",
xhrFields: {
withCredentials: true
},
data:JSON.stringify({email,password}),
success:function(response) {
loginToken = response.token;
},
error:(xhr,err) => alert('error')
})
}else alert("email and pwd empty")
Для запроса GraphQL
$.ajax({url: "http://localhost:9000/graphql",
contentType: "application/json",
headers: {"Authorization": 'bearer '+ loginToken},
type:'POST',
data: JSON.stringify({
query:`{greetingWithAuth}` }),
success: function(result) {
console.log(result.data.greetingWithAuth")
},
error: func
}