From 12e055ee807ff118e835c607239fd0fc9d7e1d9d Mon Sep 17 00:00:00 2001 From: Barry Low Date: Mon, 10 Sep 2018 22:13:10 -0700 Subject: [PATCH] Server: AuthID passed from decoded token via context --- client/src/App.js | 2 +- client/src/components/LoggedInUser.js | 10 +--------- client/src/components/NavBar.js | 2 +- client/src/queries/queries.js | 4 ++-- server/app.js | 23 ++++++++++++----------- server/schema/schema.js | 17 ++++++++++++----- 6 files changed, 29 insertions(+), 29 deletions(-) diff --git a/client/src/App.js b/client/src/App.js index 03c3cd676..cb2cf9407 100644 --- a/client/src/App.js +++ b/client/src/App.js @@ -34,7 +34,7 @@ const errorLink= onError(({ graphQLErrors, networkError }) => { graphQLErrors.map(({ message, locations, path }) => console.log( `[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`, - ), + ) ); if (networkError) console.log(`[Network error]: ${networkError}`); }) diff --git a/client/src/components/LoggedInUser.js b/client/src/components/LoggedInUser.js index 7b6137d6d..fedf2724f 100644 --- a/client/src/components/LoggedInUser.js +++ b/client/src/components/LoggedInUser.js @@ -52,12 +52,4 @@ class LoggedInUser extends Component { } } -export default graphql(getUserByAuthIdQuery, { // What follows is the way to pass a parameter to a query. - options: (props) => { - return { - variables: { - authId: props.authId - } - } - } -})(LoggedInUser); // This binds the querty to the component +export default graphql(getUserByAuthIdQuery)(LoggedInUser); // This binds the querty to the component diff --git a/client/src/components/NavBar.js b/client/src/components/NavBar.js index 8d210e23b..9ca38de84 100644 --- a/client/src/components/NavBar.js +++ b/client/src/components/NavBar.js @@ -53,7 +53,7 @@ class NavBar extends Component { const userStored = localStorage.getItem('user'); if (userStored !== null && userStored !== undefined && userStored !== 'undefined') { - const user = JSON.parse(userStored); + const user = userStored; /* For some reason, the result of GraphQL some times varies, that is why I had to do the following hack. */ diff --git a/client/src/queries/queries.js b/client/src/queries/queries.js index 45b2c38ce..67a82c958 100644 --- a/client/src/queries/queries.js +++ b/client/src/queries/queries.js @@ -35,8 +35,8 @@ query($id: ID){ ` const getUserByAuthIdQuery = gql` -query($authId: String){ - userByAuthId (authId: $authId){ +query { + userByAuthId{ id alias firstName diff --git a/server/app.js b/server/app.js index aa7b6c3e3..627010212 100644 --- a/server/app.js +++ b/server/app.js @@ -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'); diff --git a/server/schema/schema.js b/server/schema/schema.js index 75a35857c..584e64a78 100644 --- a/server/schema/schema.js +++ b/server/schema/schema.js @@ -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 }); - } }