Server: AuthID passed from decoded token via context
This commit is contained in:
1 parent
0669921d0e
commit
12e055ee80
6 files changed
+29
-29
No files matched your search
+12
-11
@@ -26,25 +26,26 @@ mongoose.connection.once('open', () => {
|
||||
|
||||
/* Here we bind all requests to this endpoint to be procecced by the GraphQL Library. */
|
||||
|
||||
app.post('/graphql', checkJwt, (err, req, res, next) => {
|
||||
console.log('checkJwt middleware req.user: ', req.user);
|
||||
if (err) return res.status(401).send({error: `[Authenticate Token Error] ${err.message}`});
|
||||
next();
|
||||
}
|
||||
);
|
||||
|
||||
app.get('/graphql', graphqlHTTP({
|
||||
schema,
|
||||
graphiql: true
|
||||
}));
|
||||
|
||||
app.post('/graphql', graphqlHTTP({
|
||||
app.post('/graphql', graphqlHTTP(req => ({
|
||||
schema,
|
||||
graphiql: false,
|
||||
context: req => ({ ...req })
|
||||
}));
|
||||
context: { user: req.user }
|
||||
})
|
||||
));
|
||||
|
||||
app.post('/graphql', checkJwt, (err, req, res, next) => {
|
||||
console.log(req);
|
||||
if (err) return res.status(401).send(`[Authenticate Token Error] ${err.message}`);
|
||||
next();
|
||||
}
|
||||
);
|
||||
|
||||
app.post('/graphql', (req, res, done) => getUser(req, res, done));
|
||||
app.post('/graphql', (err, req, res, done) => getUser(req, res, done));
|
||||
|
||||
app.listen(4000,() => {
|
||||
console.log('Now listening for requests on port 4000');
|
||||
|
||||
+12
-5
@@ -77,20 +77,28 @@ const RootQuery = new GraphQLObjectType({
|
||||
},
|
||||
userByAuthId: {
|
||||
type: UserType,
|
||||
args: {authId: {type: GraphQLString}},
|
||||
resolve(parent,args) {
|
||||
// args: {authId: {type: GraphQLString}}, // Get AuthId from context rather than send from client.
|
||||
resolve(parent, args, context) {
|
||||
// Code to get data from data source.
|
||||
console.log('userByAuthId: ', parent, args, context);
|
||||
|
||||
if (INFO_LOG === true) { console.log("[INFO] " + MODULE_NAME + " -> RootQuery -> userByAuthId -> resolve -> Entering function."); }
|
||||
if (INFO_LOG === true) { console.log("[INFO] " + MODULE_NAME + " -> RootQuery -> userByAuthId -> context.user = " + context.user); }
|
||||
|
||||
if (context.user === undefined || context.user.sub === undefined || context.user.sub === null) {
|
||||
if (global.ERROR_LOG === true) { console.log("[ERROR] " + MODULE_NAME + " -> RootQuery -> userByAuthId -> noContextUser -> err.message = Not Logged In"); }
|
||||
return null;
|
||||
}
|
||||
|
||||
const authId = context.user.sub;
|
||||
/* In order to be able to wait for asyc calls to the database, we need to return a promise to GraphQL. */
|
||||
|
||||
const promiseToGraphQL = new Promise((resolve, reject) => {
|
||||
|
||||
if (INFO_LOG === true) { console.log("[INFO] " + MODULE_NAME + " -> RootQuery -> userByAuthId -> resolve -> Promise -> Entering function."); }
|
||||
if (INFO_LOG === true) { console.log("[INFO] " + MODULE_NAME + " -> RootQuery -> userByAuthId -> resolve -> Promise -> args.authId = " + args.authId); }
|
||||
if (INFO_LOG === true) { console.log("[INFO] " + MODULE_NAME + " -> RootQuery -> userByAuthId -> resolve -> Promise -> args.authId = " + authId); }
|
||||
|
||||
findUserByAuthId(args.authId, onUserFound);
|
||||
findUserByAuthId(authId, onUserFound);
|
||||
|
||||
function onUserFound(err, responseToGraphQL) {
|
||||
|
||||
@@ -274,7 +282,6 @@ function findUserByAuthId(authId, callBackFunction) {
|
||||
|
||||
if (global.ERROR_LOG === true) { console.log("[ERROR] " + MODULE_NAME + " -> findUserByAuthId -> err.message = " + err.message); }
|
||||
callBackFunction(global.DEFAULT_FAIL_RESPONSE, { error: err });
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user